Nabil Thange commited on
Commit
2ace1d9
·
verified ·
1 Parent(s): f1cdeff

Updated to match groq

Browse files
Files changed (1) hide show
  1. app.py +37 -13
app.py CHANGED
@@ -1,34 +1,58 @@
1
  import os
2
  import gradio as gr
3
- from langchain.chat_models import ChatOpenAI
4
- from langchain import LLMChain, PromptTemplate
5
  from langchain.memory import ConversationBufferMemory
6
-
7
- OPENAI_API_KEY=os.getenv('OPENAI_API_KEY')
8
-
9
- template = """You are a helpful assistant to answer all user queries.
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  {chat_history}
11
  User: {user_message}
12
  Chatbot:"""
13
 
14
  prompt = PromptTemplate(
15
- input_variables=["chat_history", "user_message"], template=template
 
16
  )
17
 
18
  memory = ConversationBufferMemory(memory_key="chat_history")
19
 
 
20
  llm_chain = LLMChain(
21
- llm=ChatOpenAI(temperature='0.5', model_name="gpt-3.5-turbo"),
22
  prompt=prompt,
23
- verbose=True,
24
  memory=memory,
 
25
  )
26
 
27
- def get_text_response(user_message,history):
28
- response = llm_chain.predict(user_message = user_message)
29
  return response
30
 
31
- demo = gr.ChatInterface(get_text_response)
 
 
 
 
 
 
 
 
 
32
 
33
  if __name__ == "__main__":
34
- demo.launch() #To create a public link, set `share=True` in `launch()`. To enable errors and logs, set `debug=True` in `launch()`.
 
1
  import os
2
  import gradio as gr
3
+ from langchain.chains import LLMChain
4
+ from langchain.prompts import PromptTemplate
5
  from langchain.memory import ConversationBufferMemory
6
+ from langchain_groq import ChatGroq
7
+
8
+ # Set your Groq API Key from Hugging Face secret
9
+ GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
10
+ os.environ["GROQ_API_KEY"] = GROQ_API_KEY
11
+
12
+ # Prompt Template
13
+ template = """You are a highly intelligent, professional, and friendly science professor with an IQ of 300.
14
+ You possess expertise across:
15
+ - Mathematics
16
+ - Physics
17
+ - Chemistry
18
+ - Biology
19
+ - Database Management
20
+ - Artificial Intelligence & Machine Learning
21
+
22
+ Use structured and clear language. Break down concepts intuitively.
23
  {chat_history}
24
  User: {user_message}
25
  Chatbot:"""
26
 
27
  prompt = PromptTemplate(
28
+ input_variables=["chat_history", "user_message"],
29
+ template=template
30
  )
31
 
32
  memory = ConversationBufferMemory(memory_key="chat_history")
33
 
34
+ # Load Groq LLM
35
  llm_chain = LLMChain(
36
+ llm=ChatGroq(temperature=0.6, model_name="mixtral-8x7b-32768"),
37
  prompt=prompt,
 
38
  memory=memory,
39
+ verbose=True
40
  )
41
 
42
+ def get_text_response(user_message, history):
43
+ response = llm_chain.predict(user_message=user_message, chat_history=memory.buffer)
44
  return response
45
 
46
+ demo = gr.ChatInterface(
47
+ fn=get_text_response,
48
+ title="Ask Professor Nabil",
49
+ description="Chat with a genius science professor on Math, Physics, Chemistry, AI, etc.",
50
+ examples=[
51
+ "Explain DNA vs RNA in simple terms",
52
+ "How does a neural network work?",
53
+ "What is entropy in physics?"
54
+ ]
55
+ )
56
 
57
  if __name__ == "__main__":
58
+ demo.launch()