Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,41 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import datetime
|
| 4 |
-
import pytz
|
| 5 |
from llama_index.core.agent import ReActAgent
|
| 6 |
from llama_index.core.tools import FunctionTool
|
| 7 |
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# 1.
|
| 10 |
hf_token = os.getenv("HF_TOKEN")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
llm = HuggingFaceInferenceAPI(
|
| 13 |
model_name="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 14 |
-
token=hf_token
|
|
|
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# 2.
|
| 18 |
def get_tokyo_time() -> str:
|
| 19 |
-
"""
|
| 20 |
tz = pytz.timezone('Asia/Tokyo')
|
| 21 |
return f"The current time in Tokyo is {datetime.datetime.now(tz).strftime('%H:%M:%S')}"
|
| 22 |
|
| 23 |
def multiply(a: float, b: float) -> float:
|
| 24 |
-
"""Multiplies two numbers and returns the result."""
|
| 25 |
return a * b
|
| 26 |
|
| 27 |
tools = [
|
|
@@ -29,18 +43,20 @@ tools = [
|
|
| 29 |
FunctionTool.from_defaults(fn=multiply)
|
| 30 |
]
|
| 31 |
|
| 32 |
-
# 3.
|
| 33 |
-
# This will now work correctly with .from_tools()
|
| 34 |
agent = ReActAgent.from_tools(
|
| 35 |
tools,
|
| 36 |
llm=llm,
|
| 37 |
-
verbose=True
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
-
# 4.
|
| 41 |
def chat(message, history):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
gr.ChatInterface(chat, title="Unit 2
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
from llama_index.core.agent import ReActAgent
|
| 4 |
from llama_index.core.tools import FunctionTool
|
| 5 |
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 6 |
+
import datetime
|
| 7 |
+
import pytz
|
| 8 |
|
| 9 |
+
# 1. SETUP LLM
|
| 10 |
hf_token = os.getenv("HF_TOKEN")
|
| 11 |
|
| 12 |
+
# We use a system prompt to tell the model EXACTLY how to use tools
|
| 13 |
+
SYSTEM_PROMPT = """You are a helpful assistant that can use tools.
|
| 14 |
+
For any tool call, you MUST use this format:
|
| 15 |
+
Thought: I need to use a tool to answer this.
|
| 16 |
+
Action: tool_name
|
| 17 |
+
Action Input: {"arg1": "value"}
|
| 18 |
+
Observation: tool_result
|
| 19 |
+
... (repeat if needed)
|
| 20 |
+
Thought: I now know the final answer
|
| 21 |
+
Answer: your final response
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
llm = HuggingFaceInferenceAPI(
|
| 25 |
model_name="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 26 |
+
token=hf_token,
|
| 27 |
+
# This ensures the model doesn't try to use 'native' tools that HF might not support
|
| 28 |
+
is_function_calling_model=False
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# 2. TOOLS
|
| 32 |
def get_tokyo_time() -> str:
|
| 33 |
+
"""Returns the current time in Tokyo, Japan."""
|
| 34 |
tz = pytz.timezone('Asia/Tokyo')
|
| 35 |
return f"The current time in Tokyo is {datetime.datetime.now(tz).strftime('%H:%M:%S')}"
|
| 36 |
|
| 37 |
def multiply(a: float, b: float) -> float:
|
| 38 |
+
"""Multiplies two numbers (a and b) and returns the result."""
|
| 39 |
return a * b
|
| 40 |
|
| 41 |
tools = [
|
|
|
|
| 43 |
FunctionTool.from_defaults(fn=multiply)
|
| 44 |
]
|
| 45 |
|
| 46 |
+
# 3. AGENT
|
|
|
|
| 47 |
agent = ReActAgent.from_tools(
|
| 48 |
tools,
|
| 49 |
llm=llm,
|
| 50 |
+
verbose=True,
|
| 51 |
+
context=SYSTEM_PROMPT # This keeps the agent from crashing
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# 4. GRADIO
|
| 55 |
def chat(message, history):
|
| 56 |
+
try:
|
| 57 |
+
response = agent.chat(message)
|
| 58 |
+
return str(response)
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return f"Error: {str(e)}. Please try asking again."
|
| 61 |
|
| 62 |
+
demo = gr.ChatInterface(chat, title="LlamaIndex Unit 2 Fix").launch()
|