davidlopez commited on
Commit
22d19e7
·
verified ·
1 Parent(s): f4667e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -53
app.py CHANGED
@@ -2,6 +2,10 @@ import json
2
  import os
3
  import gradio as gr
4
 
 
 
 
 
5
 
6
 
7
 
@@ -16,56 +20,22 @@ os.environ["LANGCHAIN_TRACING_V2"]="true"
16
  os.environ["LANGCHAIN_ENDPOINT"]="https://api.smith.langchain.com"
17
  os.environ["LANGCHAIN_PROJECT"]=os.getenv('LANGCHAIN_PROJECT')
18
 
19
-
20
- import openai
21
- from langsmith.wrappers import wrap_openai
22
- from langsmith import traceable
23
-
24
- # Auto-trace LLM calls in-context
25
- client = wrap_openai(openai.Client())
26
-
27
-
28
- FakeNewsAggregator = client.beta.assistants.retrieve(os.getenv('OPENAI_ASSISTANT_ID'))
29
-
30
- thread= client.beta.threads.create()
31
-
32
- @traceable # Auto-trace this function
33
- def IntroPythonTutorRequest(text):
34
-
35
- global FakeNewsAggregator
36
- global thread
37
- message = client.beta.threads.messages.create(
38
- thread_id=thread.id,
39
- role="user",
40
- content=text
41
- )
42
-
43
-
44
- run = client.beta.threads.runs.create_and_poll(
45
- thread_id=thread.id,
46
- assistant_id=FakeNewsAggregator.id
47
-
48
- )
49
-
50
-
51
-
52
- if run.status == 'completed':
53
- messages = client.beta.threads.messages.list(
54
- thread_id=thread.id
55
- )
56
- gpt_response=messages.data[0].content[0].text.value
57
- return gpt_response
58
- else:
59
- return run.status
60
-
61
-
62
-
63
- gr.Interface(fn=IntroPythonTutorRequest,
64
- inputs=["text"],
65
- outputs=["text"],
66
- flagging_mode='never',
67
- title="Intro to Python Tutor",
68
- description="I am virtual assistant to help you practice basic Python ",
69
- theme="soft",
70
- examples=['Brief intro to Python','Variables in Python', '5 Coding exercises', '3 Coding exercises involving booleans']
71
- ).launch(share=False)
 
2
  import os
3
  import gradio as gr
4
 
5
+ from langchain_openai import ChatOpenAI
6
+ from langchain.schema import AIMessage, HumanMessage
7
+
8
+
9
 
10
 
11
 
 
20
  os.environ["LANGCHAIN_ENDPOINT"]="https://api.smith.langchain.com"
21
  os.environ["LANGCHAIN_PROJECT"]=os.getenv('LANGCHAIN_PROJECT')
22
 
23
+ model = ChatOpenAI(model="gpt-4o-mini")
24
+
25
+ def predict(message, history):
26
+ history_langchain_format = []
27
+ for msg in history:
28
+ if msg['role'] == "user":
29
+ history_langchain_format.append(HumanMessage(content=msg['content']))
30
+ elif msg['role'] == "assistant":
31
+ history_langchain_format.append(AIMessage(content=msg['content']))
32
+ history_langchain_format.append(HumanMessage(content=message))
33
+ gpt_response = model.invoke(history_langchain_format)
34
+ return gpt_response.content
35
+
36
+ demo = gr.ChatInterface(
37
+ predict,
38
+ type="messages"
39
+ )
40
+
41
+ demo.launch(share=True)