Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,17 +9,16 @@ from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
|
| 9 |
# 1. SETUP LLM
|
| 10 |
hf_token = os.getenv("HF_TOKEN")
|
| 11 |
|
| 12 |
-
#
|
| 13 |
llm = HuggingFaceInferenceAPI(
|
| 14 |
model_name="Qwen/Qwen2.5-7B-Instruct",
|
| 15 |
token=hf_token,
|
| 16 |
-
task="
|
| 17 |
provider="together",
|
| 18 |
is_function_calling_model=False
|
| 19 |
)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
# 2. DEFINE YOUR TOOLS
|
| 23 |
def get_tokyo_time() -> str:
|
| 24 |
"""Returns the current time in Tokyo, Japan."""
|
| 25 |
tz = pytz.timezone('Asia/Tokyo')
|
|
@@ -29,13 +28,13 @@ def multiply(a: float, b: float) -> float:
|
|
| 29 |
"""Multiplies two numbers (a and b) and returns the result."""
|
| 30 |
return a * b
|
| 31 |
|
|
|
|
| 32 |
tools = [
|
| 33 |
FunctionTool.from_defaults(fn=get_tokyo_time),
|
| 34 |
FunctionTool.from_defaults(fn=multiply)
|
| 35 |
]
|
| 36 |
|
| 37 |
# 3. THE "STABILITY" PROMPT
|
| 38 |
-
# This prevents the AI from freezing by giving it a clear pattern to follow.
|
| 39 |
RE_ACT_PROMPT = """You are a helpful assistant.
|
| 40 |
For every query, you MUST follow this sequence:
|
| 41 |
Thought: <your reasoning>
|
|
@@ -48,6 +47,7 @@ Answer: <your final response to the user>
|
|
| 48 |
"""
|
| 49 |
|
| 50 |
# 4. CREATE THE AGENT
|
|
|
|
| 51 |
agent = ReActAgent.from_tools(
|
| 52 |
tools,
|
| 53 |
llm=llm,
|
|
@@ -58,11 +58,9 @@ agent = ReActAgent.from_tools(
|
|
| 58 |
# 5. GRADIO INTERFACE
|
| 59 |
def chat(message, history):
|
| 60 |
try:
|
| 61 |
-
# Use .chat() to maintain the conversation flow
|
| 62 |
response = agent.chat(message)
|
| 63 |
return str(response)
|
| 64 |
except Exception as e:
|
| 65 |
-
# If it still fails, this will tell us WHY in the chat window
|
| 66 |
return f"System Error: {str(e)}"
|
| 67 |
|
| 68 |
gr.ChatInterface(chat, title="Unit 2: LlamaIndex Agent (Fixed)").launch()
|
|
|
|
| 9 |
# 1. SETUP LLM
|
| 10 |
hf_token = os.getenv("HF_TOKEN")
|
| 11 |
|
| 12 |
+
# Fix: Change task to "conversational" as required by the provider
|
| 13 |
llm = HuggingFaceInferenceAPI(
|
| 14 |
model_name="Qwen/Qwen2.5-7B-Instruct",
|
| 15 |
token=hf_token,
|
| 16 |
+
task="conversational",
|
| 17 |
provider="together",
|
| 18 |
is_function_calling_model=False
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# 2. DEFINE YOUR TOOLS (Must be defined before the Agent)
|
|
|
|
| 22 |
def get_tokyo_time() -> str:
|
| 23 |
"""Returns the current time in Tokyo, Japan."""
|
| 24 |
tz = pytz.timezone('Asia/Tokyo')
|
|
|
|
| 28 |
"""Multiplies two numbers (a and b) and returns the result."""
|
| 29 |
return a * b
|
| 30 |
|
| 31 |
+
# Wrap them in LlamaIndex Tool objects
|
| 32 |
tools = [
|
| 33 |
FunctionTool.from_defaults(fn=get_tokyo_time),
|
| 34 |
FunctionTool.from_defaults(fn=multiply)
|
| 35 |
]
|
| 36 |
|
| 37 |
# 3. THE "STABILITY" PROMPT
|
|
|
|
| 38 |
RE_ACT_PROMPT = """You are a helpful assistant.
|
| 39 |
For every query, you MUST follow this sequence:
|
| 40 |
Thought: <your reasoning>
|
|
|
|
| 47 |
"""
|
| 48 |
|
| 49 |
# 4. CREATE THE AGENT
|
| 50 |
+
# Now 'tools' and 'llm' are both correctly defined and configured
|
| 51 |
agent = ReActAgent.from_tools(
|
| 52 |
tools,
|
| 53 |
llm=llm,
|
|
|
|
| 58 |
# 5. GRADIO INTERFACE
|
| 59 |
def chat(message, history):
|
| 60 |
try:
|
|
|
|
| 61 |
response = agent.chat(message)
|
| 62 |
return str(response)
|
| 63 |
except Exception as e:
|
|
|
|
| 64 |
return f"System Error: {str(e)}"
|
| 65 |
|
| 66 |
gr.ChatInterface(chat, title="Unit 2: LlamaIndex Agent (Fixed)").launch()
|