Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,53 @@
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
| 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=
|
| 22 |
prompt=prompt,
|
| 23 |
verbose=True,
|
| 24 |
memory=memory,
|
| 25 |
)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
return response
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
demo.launch(share=True) #To create a public link, set `share=True` in `launch()`. To enable errors and logs, set `debug=True` in `launch()`.
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
import gradio as gr
|
| 5 |
from langchain.chat_models import ChatOpenAI
|
| 6 |
from langchain import LLMChain, PromptTemplate
|
| 7 |
from langchain.memory import ConversationBufferMemory
|
| 8 |
|
| 9 |
+
# β
Correct way to fetch the API key
|
| 10 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
|
| 12 |
+
# β
This MUST be passed explicitly if running outside OpenAI-hosted envs
|
| 13 |
+
if not OPENAI_API_KEY:
|
| 14 |
+
raise ValueError("OPENAI_API_KEY is not set in the environment variables.")
|
| 15 |
|
| 16 |
+
# β
Template
|
| 17 |
+
template = """You are a helpful assistant to answer user queries and coding questions.
|
| 18 |
+
Act like the best programmer and explain everything like explaining it to a 5-year-old.
|
| 19 |
{chat_history}
|
| 20 |
User: {user_message}
|
| 21 |
Chatbot:"""
|
| 22 |
|
| 23 |
+
# β
Prompt
|
| 24 |
prompt = PromptTemplate(
|
| 25 |
input_variables=["chat_history", "user_message"], template=template
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# β
Memory
|
| 29 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 30 |
|
| 31 |
+
# β FIXED: temperature must be a float, not a string
|
| 32 |
llm_chain = LLMChain(
|
| 33 |
+
llm=ChatOpenAI(temperature=0.5, model_name="gpt-3.5-turbo", openai_api_key=OPENAI_API_KEY),
|
| 34 |
prompt=prompt,
|
| 35 |
verbose=True,
|
| 36 |
memory=memory,
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# β
Function needs to return a response AND history (for gr.ChatInterface)
|
| 40 |
+
def get_text_response(user_message, history):
|
| 41 |
+
response = llm_chain.predict(user_message=user_message)
|
| 42 |
+
# History must be returned for the Gradio chat interface to maintain context
|
| 43 |
return response
|
| 44 |
|
| 45 |
+
# β
Create interface
|
| 46 |
+
demo = gr.ChatInterface(fn=get_text_response)
|
| 47 |
+
|
| 48 |
+
# β
Required for Hugging Face Spaces (they call this file)
|
| 49 |
+
demo.launch()
|
| 50 |
+
|
| 51 |
+
|
| 52 |
|
| 53 |
+
|
|
|