davidlopez's picture
Update app.py
8a15b2c verified
Raw History Blame
1.07 kB
import json
import os
import gradio as gr
from langchain_openai import ChatOpenAI
from langchain.schema import AIMessage, HumanMessage
os.environ['OPENAI_API_KEY']=os.getenv('OpenAI_KEY')
os.environ["LANGCHAIN_API_KEY"]=os.getenv('LANGCHAIN_API_KEY')
os.environ["LANGCHAIN_TRACING_V2"]="true"
os.environ["LANGCHAIN_ENDPOINT"]="https://api.smith.langchain.com"
os.environ["LANGCHAIN_PROJECT"]=os.getenv('LANGCHAIN_PROJECT')
model = ChatOpenAI(model="gpt-4o-mini")
def predict(message, history):
history_langchain_format = []
for msg in history:
if msg['role'] == "user":
history_langchain_format.append(HumanMessage(content=msg['content']))
elif msg['role'] == "assistant":
history_langchain_format.append(AIMessage(content=msg['content']))
history_langchain_format.append(HumanMessage(content=message))
gpt_response = model.invoke(history_langchain_format)
return gpt_response.content
demo = gr.ChatInterface(
predict,
type="messages",
height=800
)
demo.launch(share=False)