Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
-
from llama_index.core.agent import ReActAgent
|
| 3 |
-
from llama_index.core.tools import FunctionTool
|
| 4 |
-
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 5 |
-
from llama_index.core import Settings
|
| 6 |
import gradio as gr
|
| 7 |
import datetime
|
| 8 |
import pytz
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# 1. Setup LLM
|
| 11 |
-
#
|
| 12 |
hf_token = os.getenv("HF_TOKEN")
|
| 13 |
|
| 14 |
llm = HuggingFaceInferenceAPI(
|
|
@@ -16,7 +16,7 @@ llm = HuggingFaceInferenceAPI(
|
|
| 16 |
token=hf_token
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# 2. Define your Tools
|
| 20 |
def get_tokyo_time() -> str:
|
| 21 |
"""Returns the current time in Tokyo, Japan."""
|
| 22 |
tz = pytz.timezone('Asia/Tokyo')
|
|
@@ -26,18 +26,31 @@ def multiply(a: float, b: float) -> float:
|
|
| 26 |
"""Multiplies two numbers and returns the result."""
|
| 27 |
return a * b
|
| 28 |
|
| 29 |
-
# Wrap
|
| 30 |
tools = [
|
| 31 |
FunctionTool.from_defaults(fn=get_tokyo_time),
|
| 32 |
FunctionTool.from_defaults(fn=multiply)
|
| 33 |
]
|
| 34 |
|
| 35 |
-
# 3. Create the Agent
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# 4. Gradio Interface
|
| 39 |
def chat(message, history):
|
|
|
|
| 40 |
response = agent.chat(message)
|
| 41 |
return str(response)
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import datetime
|
| 4 |
import pytz
|
| 5 |
+
# Ensure we are using the core agent class
|
| 6 |
+
from llama_index.core.agent import ReActAgent
|
| 7 |
+
from llama_index.core.tools import FunctionTool
|
| 8 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 9 |
|
| 10 |
+
# 1. Setup LLM
|
| 11 |
+
# Reminder: Add HF_TOKEN to your Space "Secrets" in Settings
|
| 12 |
hf_token = os.getenv("HF_TOKEN")
|
| 13 |
|
| 14 |
llm = HuggingFaceInferenceAPI(
|
|
|
|
| 16 |
token=hf_token
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# 2. Define your Tools
|
| 20 |
def get_tokyo_time() -> str:
|
| 21 |
"""Returns the current time in Tokyo, Japan."""
|
| 22 |
tz = pytz.timezone('Asia/Tokyo')
|
|
|
|
| 26 |
"""Multiplies two numbers and returns the result."""
|
| 27 |
return a * b
|
| 28 |
|
| 29 |
+
# Wrap tools
|
| 30 |
tools = [
|
| 31 |
FunctionTool.from_defaults(fn=get_tokyo_time),
|
| 32 |
FunctionTool.from_defaults(fn=multiply)
|
| 33 |
]
|
| 34 |
|
| 35 |
+
# 3. Create the Agent
|
| 36 |
+
# Note: Using ReActAgent.from_tools is correct for the core class
|
| 37 |
+
agent = ReActAgent.from_tools(
|
| 38 |
+
tools,
|
| 39 |
+
llm=llm,
|
| 40 |
+
verbose=True
|
| 41 |
+
)
|
| 42 |
|
| 43 |
# 4. Gradio Interface
|
| 44 |
def chat(message, history):
|
| 45 |
+
# The agent handles the conversation state internally
|
| 46 |
response = agent.chat(message)
|
| 47 |
return str(response)
|
| 48 |
|
| 49 |
+
demo = gr.ChatInterface(
|
| 50 |
+
fn=chat,
|
| 51 |
+
title="Unit 2: LlamaIndex Agent",
|
| 52 |
+
description="I am a LlamaIndex agent with access to Tokyo time and multiplication tools."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
demo.launch()
|