Commit
·
0c209c1
1
Parent(s):
ebf517e
add config file + HF provider support
Browse files- app.py +10 -5
- config.py +2 -0
- requirements.txt +1 -1
app.py
CHANGED
@@ -6,11 +6,12 @@ import pandas as pd
|
|
6 |
from llama_index.core import PromptTemplate
|
7 |
from llama_index.core.workflow import Context
|
8 |
from llama_index.core.agent.workflow import ReActAgent, AgentStream, ToolCallResult
|
9 |
-
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
10 |
from llama_index.tools.wikipedia import WikipediaToolSpec
|
11 |
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
|
12 |
|
13 |
from prompt import custom_react_system_header_str
|
|
|
14 |
|
15 |
# (Keep Constants as is)
|
16 |
# --- Constants ---
|
@@ -30,16 +31,18 @@ class BasicAgent:
|
|
30 |
class LLamaIndexAgent:
|
31 |
def __init__(self,
|
32 |
model_name="Qwen/Qwen2.5-Coder-32B-Instruct",
|
|
|
33 |
show_tools_desc=True,
|
34 |
show_prompt=True):
|
35 |
|
36 |
# LLM definition
|
37 |
-
llm = HuggingFaceInferenceAPI(model_name=model_name
|
|
|
38 |
print(f"LLamaIndexAgent initialized with model \"{model_name}\"")
|
39 |
|
40 |
# tools definition
|
41 |
tool_spec_list = []
|
42 |
-
tool_spec_list += WikipediaToolSpec().to_tool_list()
|
43 |
tool_spec_list += DuckDuckGoSearchToolSpec().to_tool_list()
|
44 |
|
45 |
# agent definition
|
@@ -63,7 +66,6 @@ class LLamaIndexAgent:
|
|
63 |
print("\n" + "="*30 + f" Prompt: {k} " + "="*30)
|
64 |
print(v.template)
|
65 |
|
66 |
-
|
67 |
async def __call__(self, question: str) -> str:
|
68 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
69 |
|
@@ -101,7 +103,8 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
101 |
# 1. Instantiate Agent (modify this part to create your agent)
|
102 |
try:
|
103 |
# agent = BasicAgent()
|
104 |
-
agent = LLamaIndexAgent(
|
|
|
105 |
except Exception as e:
|
106 |
print(f"Error instantiating agent: {e}")
|
107 |
return f"Error initializing agent: {e}", None
|
@@ -143,8 +146,10 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
143 |
try:
|
144 |
# submitted_answer = agent(question_text)
|
145 |
submitted_answer = await agent(question_text)
|
|
|
146 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
147 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
148 |
except Exception as e:
|
149 |
print(f"Error running agent on task {task_id}: {e}")
|
150 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
6 |
from llama_index.core import PromptTemplate
|
7 |
from llama_index.core.workflow import Context
|
8 |
from llama_index.core.agent.workflow import ReActAgent, AgentStream, ToolCallResult
|
9 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI # customized to support different provider
|
10 |
from llama_index.tools.wikipedia import WikipediaToolSpec
|
11 |
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
|
12 |
|
13 |
from prompt import custom_react_system_header_str
|
14 |
+
from config import HF_MODEL_NAME, HF_PROVIDER
|
15 |
|
16 |
# (Keep Constants as is)
|
17 |
# --- Constants ---
|
|
|
31 |
class LLamaIndexAgent:
|
32 |
def __init__(self,
|
33 |
model_name="Qwen/Qwen2.5-Coder-32B-Instruct",
|
34 |
+
provider="hf-inference",
|
35 |
show_tools_desc=True,
|
36 |
show_prompt=True):
|
37 |
|
38 |
# LLM definition
|
39 |
+
llm = HuggingFaceInferenceAPI(model_name=model_name,
|
40 |
+
provider=provider)
|
41 |
print(f"LLamaIndexAgent initialized with model \"{model_name}\"")
|
42 |
|
43 |
# tools definition
|
44 |
tool_spec_list = []
|
45 |
+
# tool_spec_list += WikipediaToolSpec().to_tool_list()
|
46 |
tool_spec_list += DuckDuckGoSearchToolSpec().to_tool_list()
|
47 |
|
48 |
# agent definition
|
|
|
66 |
print("\n" + "="*30 + f" Prompt: {k} " + "="*30)
|
67 |
print(v.template)
|
68 |
|
|
|
69 |
async def __call__(self, question: str) -> str:
|
70 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
71 |
|
|
|
103 |
# 1. Instantiate Agent (modify this part to create your agent)
|
104 |
try:
|
105 |
# agent = BasicAgent()
|
106 |
+
agent = LLamaIndexAgent(model_name=HF_MODEL_NAME,
|
107 |
+
provider=HF_PROVIDER)
|
108 |
except Exception as e:
|
109 |
print(f"Error instantiating agent: {e}")
|
110 |
return f"Error initializing agent: {e}", None
|
|
|
146 |
try:
|
147 |
# submitted_answer = agent(question_text)
|
148 |
submitted_answer = await agent(question_text)
|
149 |
+
submitted_answer = str(submitted_answer) # cast AgentOutput to str
|
150 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
151 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
152 |
+
agent.ctx.clear() # clear context for next question
|
153 |
except Exception as e:
|
154 |
print(f"Error running agent on task {task_id}: {e}")
|
155 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
config.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
HF_MODEL_NAME = "google/gemma-3-27b-it"
|
2 |
+
HF_PROVIDER = "nebius"
|
requirements.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
gradio
|
2 |
requests
|
3 |
llama-index
|
4 |
-
llama-index-llms-huggingface-api
|
5 |
llama_index.tools.wikipedia
|
6 |
llama_index.tools.duckduckgo
|
|
|
1 |
gradio
|
2 |
requests
|
3 |
llama-index
|
4 |
+
llama-index-llms-huggingface-api @ git+https://github.com/guillaumefrd/llama_index.git@add-provider-HF-API#subdirectory=llama-index-integrations/llms"
|
5 |
llama_index.tools.wikipedia
|
6 |
llama_index.tools.duckduckgo
|