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