Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,31 +4,39 @@ from langchain.chat_models import ChatOpenAI
|
|
| 4 |
from langchain import LLMChain, PromptTemplate
|
| 5 |
from langchain.memory import ConversationBufferMemory
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 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"],
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 19 |
|
| 20 |
llm_chain = LLMChain(
|
| 21 |
-
llm=ChatOpenAI(temperature=
|
| 22 |
prompt=prompt,
|
| 23 |
verbose=True,
|
| 24 |
memory=memory,
|
| 25 |
)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
return response
|
| 30 |
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
| 4 |
from langchain import LLMChain, PromptTemplate
|
| 5 |
from langchain.memory import ConversationBufferMemory
|
| 6 |
|
| 7 |
+
# Set OpenAI API Key
|
| 8 |
+
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
| 9 |
+
if not OPENAI_API_KEY:
|
| 10 |
+
raise ValueError("OpenAI API Key is not set. Please set the 'OPENAI_API_KEY' environment variable.")
|
| 11 |
|
| 12 |
+
# Define the template for the assistant
|
| 13 |
template = """You are a helpful assistant to answer all user queries.
|
| 14 |
{chat_history}
|
| 15 |
User: {user_message}
|
| 16 |
Chatbot:"""
|
| 17 |
|
| 18 |
prompt = PromptTemplate(
|
| 19 |
+
input_variables=["chat_history", "user_message"],
|
| 20 |
+
template=template
|
| 21 |
)
|
| 22 |
|
| 23 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 24 |
|
| 25 |
llm_chain = LLMChain(
|
| 26 |
+
llm=ChatOpenAI(temperature=0.5, model_name="gpt-3.5-turbo"),
|
| 27 |
prompt=prompt,
|
| 28 |
verbose=True,
|
| 29 |
memory=memory,
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# Function to get response
|
| 33 |
+
def get_text_response(user_message, history=None):
|
| 34 |
+
response = llm_chain.predict(user_message=user_message)
|
| 35 |
return response
|
| 36 |
|
| 37 |
+
# Gradio Chat Interface
|
| 38 |
+
demo = gr.ChatInterface(get_text_response, type="messages")
|
| 39 |
|
| 40 |
+
# Launch the Gradio app
|
| 41 |
+
if _name_ == "_main_":
|
| 42 |
+
demo.launch(share=True, debug=True)
|