Arhashmi commited on
Commit
ef53482
1 Parent(s): 5e8cc46

Update function.py

Browse files
Files changed (1) hide show
  1. function.py +39 -47
function.py CHANGED
@@ -1,60 +1,52 @@
1
  from langchain.prompts import PromptTemplate
2
- from langchain.llms import CTransformers
3
- from langchain.chains import LLMChain
4
- from langchain.chains import SequentialChain
5
  from langchain.llms import HuggingFaceHub
 
6
  from dotenv import load_dotenv
 
7
 
 
 
8
 
9
- # load_dotenv();
10
- config = {'max_new_tokens': 512, 'temperature': 0.6}
11
 
12
- # Create function for app
13
- def GetLLMResponse(selected_topic_level,
14
- selected_topic,
15
- num_quizzes):
16
-
17
- # Calling llama model
18
- # llm = CTransformers(model="D:\Code Workspace\DL Model\llama-2-7b-chat.ggmlv3.q8_0.bin",
19
- # model_type = 'llama',
20
- # config = config)
21
-
22
- # llm = CTransformers(model='TheBloke/Llama-2-7B-Chat-GGML',
23
- # model_file = 'llama-2-7b-chat.ggmlv3.q8_0.bin',
24
- # model_type = 'llama',
25
- # config = config)
26
 
 
 
27
  llm = HuggingFaceHub(
28
- repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1",
29
- model_kwargs = config
 
30
  )
31
-
32
- ## Create LLM Chaining
33
- questions_template = "I want you to just generate question with this specification: Generate a {selected_topic_level} math quiz on the topic of {selected_topic}. Generate only {num_quizzes} questions not more and without providing answers. The Question should not in image format/link"
34
  questions_prompt = PromptTemplate(input_variables=["selected_topic_level", "selected_topic", "num_quizzes"],
35
  template=questions_template)
36
- questions_chain = LLMChain(llm= llm,
37
- prompt = questions_prompt,
38
- output_key = "questions")
39
-
40
-
41
- answer_template = "I want you to become a teacher answer this specific Question:\n {questions}\n\n. You should gave me a straightforward and consise explanation and answer to each one of them"
42
- answer_prompt = PromptTemplate(input_variables = ["questions"],
43
- template = answer_template)
44
- answer_chain = LLMChain(llm = llm,
45
- prompt = answer_prompt,
46
- output_key = "answer")
47
-
48
- ## Create Sequential Chaining
49
- seq_chain = SequentialChain(chains = [questions_chain, answer_chain],
50
- input_variables = ['selected_topic_level', 'selected_topic', 'num_quizzes'],
51
- output_variables = ['questions', 'answer'])
52
-
53
- response = seq_chain({'selected_topic_level': selected_topic_level,
54
- 'selected_topic': selected_topic,
55
- 'num_quizzes' : num_quizzes})
56
-
57
- ## Generate the response from the llama 2 model
58
  print(response)
 
 
59
  return response
60
-
 
1
  from langchain.prompts import PromptTemplate
 
 
 
2
  from langchain.llms import HuggingFaceHub
3
+ from langchain.chains import LLMChain, SequentialChain
4
  from dotenv import load_dotenv
5
+ import os
6
 
7
+ # Load environment variables from .env file
8
+ load_dotenv()
9
 
10
+ # Hugging Face Hub API token
11
+ huggingfacehub_api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
12
 
13
+ # Configuration for language model
14
+ config = {'max_new_tokens': 512, 'temperature': 0.6}
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ def GetLLMResponse(selected_topic_level, selected_topic, num_quizzes):
17
+ # Initialize Hugging Face Hub with API token
18
  llm = HuggingFaceHub(
19
+ repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
20
+ model_kwargs=config,
21
+ huggingfacehub_api_token=huggingfacehub_api_token
22
  )
23
+
24
+ # Create LLM Chaining for generating questions
25
+ questions_template = "Generate a {selected_topic_level} math quiz on the topic of {selected_topic}. Generate only {num_quizzes} questions not more and without providing answers. The Question should not be in image format/link"
26
  questions_prompt = PromptTemplate(input_variables=["selected_topic_level", "selected_topic", "num_quizzes"],
27
  template=questions_template)
28
+ questions_chain = LLMChain(llm=llm, prompt=questions_prompt, output_key="questions")
29
+
30
+ # Create LLM Chaining for generating answers
31
+ answer_template = "I want you to become a teacher and answer this specific Question:\n{questions}\n\nYou should give me a straightforward and concise explanation and answer to each one of them."
32
+ answer_prompt = PromptTemplate(input_variables=["questions"], template=answer_template)
33
+ answer_chain = LLMChain(llm=llm, prompt=answer_prompt, output_key="answer")
34
+
35
+ # Create Sequential Chaining
36
+ seq_chain = SequentialChain(chains=[questions_chain, answer_chain],
37
+ input_variables=['selected_topic_level', 'selected_topic', 'num_quizzes'],
38
+ output_variables=['questions', 'answer'])
39
+
40
+ # Execute the chained prompts
41
+ response = seq_chain({
42
+ 'selected_topic_level': selected_topic_level,
43
+ 'selected_topic': selected_topic,
44
+ 'num_quizzes': num_quizzes
45
+ })
46
+
47
+ # Print the response for debugging purposes
 
 
48
  print(response)
49
+
50
+ # Return the response
51
  return response
52
+