Spaces:
Running
Running
Commit
·
95fcc62
1
Parent(s):
c8d6d95
Update langgraph deployment
Browse files
app.py
CHANGED
@@ -2,10 +2,29 @@ import os
|
|
2 |
import gradio as gr
|
3 |
from langgraph_sdk import get_client
|
4 |
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
|
|
|
|
|
|
|
5 |
|
6 |
-
LANGGRAPH_DEPLOYMENT = "https://
|
7 |
|
8 |
client = get_client(url=LANGGRAPH_DEPLOYMENT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
async def respond(message, history, thread_state):
|
11 |
assistants = await client.assistants.search(
|
@@ -17,6 +36,7 @@ async def respond(message, history, thread_state):
|
|
17 |
thread_state = thread["thread_id"]
|
18 |
|
19 |
response = ""
|
|
|
20 |
|
21 |
async for chunk in client.runs.stream(
|
22 |
thread_id=thread_state,
|
@@ -26,13 +46,28 @@ async def respond(message, history, thread_state):
|
|
26 |
):
|
27 |
if chunk.event == "events":
|
28 |
if chunk.data["event"] == "on_chat_model_stream":
|
|
|
|
|
29 |
token = chunk.data["data"]["chunk"]["content"]
|
30 |
response += token
|
31 |
-
yield history + [
|
|
|
32 |
|
33 |
def clear_conversation():
|
34 |
return [], None
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
37 |
with gr.Column(scale=3):
|
38 |
gr.Markdown("### Assistant R&D Agricole")
|
@@ -44,7 +79,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
44 |
),
|
45 |
container=True,
|
46 |
show_label=False,
|
|
|
47 |
)
|
|
|
48 |
with gr.Row():
|
49 |
txt = gr.Textbox(
|
50 |
placeholder="Posez votre question ici concernant les données R&D agricoles...",
|
@@ -56,13 +93,16 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
56 |
|
57 |
with gr.Row():
|
58 |
clear_btn = gr.Button("Effacer la conversation")
|
|
|
|
|
59 |
|
60 |
-
thread_state = gr.State()
|
|
|
61 |
|
62 |
txt.submit(
|
63 |
respond,
|
64 |
[txt, chatbot, thread_state],
|
65 |
-
[chatbot, thread_state],
|
66 |
api_name=False
|
67 |
).then(
|
68 |
lambda: "",
|
@@ -73,7 +113,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
73 |
submit_btn.click(
|
74 |
respond,
|
75 |
[txt, chatbot, thread_state],
|
76 |
-
[chatbot, thread_state],
|
77 |
api_name=False
|
78 |
).then(
|
79 |
lambda: "",
|
@@ -81,6 +121,20 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
81 |
[txt]
|
82 |
)
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
clear_btn.click(
|
85 |
clear_conversation,
|
86 |
None,
|
|
|
2 |
import gradio as gr
|
3 |
from langgraph_sdk import get_client
|
4 |
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
|
5 |
+
from langsmith import Client
|
6 |
+
import asyncio
|
7 |
+
import logging
|
8 |
|
9 |
+
LANGGRAPH_DEPLOYMENT = "https://le-chat-bottes-08847786c41355da87302fa1e0f41f4a.us.langgraph.app/"
|
10 |
|
11 |
client = get_client(url=LANGGRAPH_DEPLOYMENT)
|
12 |
+
langsmith_client = Client()
|
13 |
+
|
14 |
+
async def log_feedback(run_id, score, comment=""):
|
15 |
+
"""Log feedback to LangSmith"""
|
16 |
+
try:
|
17 |
+
langsmith_client.create_feedback(
|
18 |
+
run_id=run_id,
|
19 |
+
key="user_feedback",
|
20 |
+
score=score,
|
21 |
+
comment=comment
|
22 |
+
)
|
23 |
+
logging.info(f"Successfully logged feedback for run_id: {run_id} with score: {score}")
|
24 |
+
return True
|
25 |
+
except Exception as e:
|
26 |
+
logging.error(f"Error logging feedback for run_id: {run_id}: {e}")
|
27 |
+
return False
|
28 |
|
29 |
async def respond(message, history, thread_state):
|
30 |
assistants = await client.assistants.search(
|
|
|
36 |
thread_state = thread["thread_id"]
|
37 |
|
38 |
response = ""
|
39 |
+
run_id = None
|
40 |
|
41 |
async for chunk in client.runs.stream(
|
42 |
thread_id=thread_state,
|
|
|
46 |
):
|
47 |
if chunk.event == "events":
|
48 |
if chunk.data["event"] == "on_chat_model_stream":
|
49 |
+
if run_id is None and "run_id" in chunk.data:
|
50 |
+
run_id = chunk.data["run_id"]
|
51 |
token = chunk.data["data"]["chunk"]["content"]
|
52 |
response += token
|
53 |
+
yield history + [{"role": "user", "content": message},
|
54 |
+
{"role": "assistant", "content": response}], thread_state, run_id
|
55 |
|
56 |
def clear_conversation():
|
57 |
return [], None
|
58 |
|
59 |
+
async def give_positive_feedback(run_id):
|
60 |
+
if run_id is not None:
|
61 |
+
await log_feedback(run_id, 1.0)
|
62 |
+
else:
|
63 |
+
logging.warning("Attempted to give positive feedback but run_id was None")
|
64 |
+
|
65 |
+
async def give_negative_feedback(run_id):
|
66 |
+
if run_id is not None:
|
67 |
+
await log_feedback(run_id, 0.0)
|
68 |
+
else:
|
69 |
+
logging.warning("Attempted to give negative feedback but run_id was None")
|
70 |
+
|
71 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
72 |
with gr.Column(scale=3):
|
73 |
gr.Markdown("### Assistant R&D Agricole")
|
|
|
79 |
),
|
80 |
container=True,
|
81 |
show_label=False,
|
82 |
+
type="messages"
|
83 |
)
|
84 |
+
|
85 |
with gr.Row():
|
86 |
txt = gr.Textbox(
|
87 |
placeholder="Posez votre question ici concernant les données R&D agricoles...",
|
|
|
93 |
|
94 |
with gr.Row():
|
95 |
clear_btn = gr.Button("Effacer la conversation")
|
96 |
+
thumbs_up = gr.Button("👍")
|
97 |
+
thumbs_down = gr.Button("👎")
|
98 |
|
99 |
+
thread_state = gr.State()
|
100 |
+
current_run_id = gr.State()
|
101 |
|
102 |
txt.submit(
|
103 |
respond,
|
104 |
[txt, chatbot, thread_state],
|
105 |
+
[chatbot, thread_state, current_run_id],
|
106 |
api_name=False
|
107 |
).then(
|
108 |
lambda: "",
|
|
|
113 |
submit_btn.click(
|
114 |
respond,
|
115 |
[txt, chatbot, thread_state],
|
116 |
+
[chatbot, thread_state, current_run_id],
|
117 |
api_name=False
|
118 |
).then(
|
119 |
lambda: "",
|
|
|
121 |
[txt]
|
122 |
)
|
123 |
|
124 |
+
thumbs_up.click(
|
125 |
+
lambda x: asyncio.run(give_positive_feedback(x)),
|
126 |
+
[current_run_id],
|
127 |
+
None,
|
128 |
+
api_name=False
|
129 |
+
)
|
130 |
+
|
131 |
+
thumbs_down.click(
|
132 |
+
lambda x: asyncio.run(give_negative_feedback(x)),
|
133 |
+
[current_run_id],
|
134 |
+
None,
|
135 |
+
api_name=False
|
136 |
+
)
|
137 |
+
|
138 |
clear_btn.click(
|
139 |
clear_conversation,
|
140 |
None,
|