Made the agent conversational
Browse files
agent.py
CHANGED
@@ -1,28 +1,76 @@
|
|
1 |
import os
|
2 |
-
from
|
3 |
-
from
|
|
|
|
|
4 |
|
5 |
-
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
class BasicAgent:
|
11 |
def __init__(self):
|
12 |
-
print("BasicAgent initialized.")
|
13 |
-
|
14 |
-
#model = HfApiModel(model_id="https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud", provider="together", use_auth_token=True)
|
15 |
|
16 |
-
agent = CodeAgent(
|
17 |
tools=[tls.search_tool, tls.calculate_cargo_travel_time],
|
18 |
-
model=
|
19 |
additional_authorized_imports=["pandas"],
|
20 |
max_steps=20,
|
21 |
)
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
return str(fixed_answer)
|
|
|
1 |
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
from smolagents import CodeAgent, Model, ChatMessage
|
5 |
+
import tools.tools as tls # Your tool definitions
|
6 |
|
7 |
+
load_dotenv()
|
8 |
|
9 |
+
# β
Utility: Ensure role alternation (user/assistant/user...)
|
10 |
+
def enforce_strict_role_alternation(messages):
|
11 |
+
"""
|
12 |
+
Fixes message history to enforce 'user/assistant/user/assistant' alternation.
|
13 |
+
Keeps first 'system' if present.
|
14 |
+
"""
|
15 |
+
cleaned = []
|
16 |
+
last_role = None
|
17 |
|
18 |
+
for msg in messages:
|
19 |
+
role = msg["role"]
|
20 |
+
if role not in ("user", "assistant", "system"):
|
21 |
+
continue # skip invalid roles
|
22 |
+
|
23 |
+
if role == "system" and not cleaned:
|
24 |
+
cleaned.append(msg)
|
25 |
+
continue
|
26 |
+
|
27 |
+
if role == last_role:
|
28 |
+
continue # skip consecutive same-role messages
|
29 |
+
|
30 |
+
cleaned.append(msg)
|
31 |
+
last_role = role
|
32 |
+
|
33 |
+
return cleaned
|
34 |
+
|
35 |
+
|
36 |
+
# β
Custom Model Wrapper using InferenceClient
|
37 |
+
class HuggingFaceChatModel(Model):
|
38 |
+
def __init__(self):
|
39 |
+
model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
40 |
+
self.client = InferenceClient(model=model_id, token=os.getenv("HF_TOKEN"))
|
41 |
+
|
42 |
+
def generate(self, messages, stop_sequences=None):
|
43 |
+
if stop_sequences is None:
|
44 |
+
stop_sequences = ["Task"]
|
45 |
+
|
46 |
+
# π‘ Enforce correct message order
|
47 |
+
cleaned_messages = enforce_strict_role_alternation(messages)
|
48 |
+
|
49 |
+
# π§ Hugging Face call
|
50 |
+
response = self.client.chat_completion(
|
51 |
+
messages=cleaned_messages,
|
52 |
+
stop=stop_sequences,
|
53 |
+
max_tokens=1024
|
54 |
+
)
|
55 |
+
content = response.choices[0].message["content"]
|
56 |
+
return ChatMessage(role="assistant", content=content)
|
57 |
+
|
58 |
+
|
59 |
+
# β
Basic Agent with SmolAgents
|
60 |
class BasicAgent:
|
61 |
def __init__(self):
|
62 |
+
print("β
BasicAgent initialized with Hugging Face chat model.")
|
63 |
+
self.model = HuggingFaceChatModel()
|
|
|
64 |
|
65 |
+
self.agent = CodeAgent(
|
66 |
tools=[tls.search_tool, tls.calculate_cargo_travel_time],
|
67 |
+
model=self.model,
|
68 |
additional_authorized_imports=["pandas"],
|
69 |
max_steps=20,
|
70 |
)
|
71 |
|
72 |
+
def __call__(self, question: str) -> str:
|
73 |
+
print(f"π₯ Received question: {question[:60]}...")
|
74 |
+
response = self.agent.run(question)
|
75 |
+
print(f"π€ Response generated: {response[:60]}...")
|
76 |
+
return response
|
|
app.py
CHANGED
@@ -6,7 +6,7 @@ import pandas as pd
|
|
6 |
from huggingface_hub import login
|
7 |
from dotenv import load_dotenv
|
8 |
|
9 |
-
import
|
10 |
|
11 |
# (Keep Constants as is)
|
12 |
# --- Constants ---
|
@@ -134,7 +134,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
134 |
results_df = pd.DataFrame(results_log)
|
135 |
return status_message, results_df
|
136 |
'''
|
137 |
-
|
138 |
def test_init_agent_for_chat(text_input, history):
|
139 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
140 |
try:
|
@@ -146,6 +146,18 @@ def test_init_agent_for_chat(text_input, history):
|
|
146 |
submitted_answer = basicAgent(text_input)
|
147 |
|
148 |
return submitted_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
# --- Build Gradio Interface using Blocks ---
|
151 |
with gr.Blocks() as demo:
|
|
|
6 |
from huggingface_hub import login
|
7 |
from dotenv import load_dotenv
|
8 |
|
9 |
+
from agent import BasicAgent
|
10 |
|
11 |
# (Keep Constants as is)
|
12 |
# --- Constants ---
|
|
|
134 |
results_df = pd.DataFrame(results_log)
|
135 |
return status_message, results_df
|
136 |
'''
|
137 |
+
'''
|
138 |
def test_init_agent_for_chat(text_input, history):
|
139 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
140 |
try:
|
|
|
146 |
submitted_answer = basicAgent(text_input)
|
147 |
|
148 |
return submitted_answer
|
149 |
+
'''
|
150 |
+
def test_init_agent_for_chat(text_input, history):
|
151 |
+
try:
|
152 |
+
basicAgent = BasicAgent()
|
153 |
+
except Exception as e:
|
154 |
+
return f"[Error initializing agent]: {e}"
|
155 |
+
|
156 |
+
return basicAgent(text_input)
|
157 |
+
|
158 |
+
with gr.Blocks() as demo:
|
159 |
+
gr.Markdown("## π€ Conversational Cargo Agent")
|
160 |
+
gr.ChatInterface(test_init_agent_for_chat, type="messages")
|
161 |
|
162 |
# --- Build Gradio Interface using Blocks ---
|
163 |
with gr.Blocks() as demo:
|