Update app.py
Browse files
app.py
CHANGED
@@ -136,23 +136,24 @@
|
|
136 |
|
137 |
|
138 |
# new code
|
|
|
|
|
|
|
139 |
import gradio as gr
|
140 |
-
import time
|
141 |
|
142 |
-
|
143 |
-
response = f"System prompt: {system_prompt}\n Message: {message}."
|
144 |
-
for i in range(min(len(response), int(tokens))):
|
145 |
-
time.sleep(0.05)
|
146 |
-
yield response[: i + 1]
|
147 |
|
148 |
-
|
149 |
-
echo,
|
150 |
-
|
151 |
-
additional_inputs=[
|
152 |
-
gr.Textbox("You are helpful AI.", label="System Prompt"),
|
153 |
-
gr.Slider(10, 100),
|
154 |
-
],
|
155 |
-
)
|
156 |
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
|
|
|
|
136 |
|
137 |
|
138 |
# new code
|
139 |
+
from langchain.chat_models import ChatOpenAI
|
140 |
+
from langchain.schema import AIMessage, HumanMessage
|
141 |
+
import openai
|
142 |
import gradio as gr
|
|
|
143 |
|
144 |
+
os.environ["OPENAI_API_KEY"] = "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" # Replace with your key
|
|
|
|
|
|
|
|
|
145 |
|
146 |
+
llm = ChatOpenAI(temperature=1.0, model='gpt-3.5-turbo-0613')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
|
148 |
+
def predict(message, history):
|
149 |
+
history_langchain_format = []
|
150 |
+
for msg in history:
|
151 |
+
if msg['role'] == "user":
|
152 |
+
history_langchain_format.append(HumanMessage(content=msg['content']))
|
153 |
+
elif msg['role'] == "assistant":
|
154 |
+
history_langchain_format.append(AIMessage(content=msg['content']))
|
155 |
+
history_langchain_format.append(HumanMessage(content=message))
|
156 |
+
gpt_response = llm(history_langchain_format)
|
157 |
+
return gpt_response.content
|
158 |
|
159 |
+
gr.ChatInterface(predict).launch()
|