agent_course_final / agent.py
George Sergia
Use llama_index as agent framework
bbfc23a
raw
history blame
1.58 kB
from llama_index.core.agent.workflow import AgentWorkflow
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
from llama_index.core.tools import FunctionTool
from llama_index.core.workflow import Context
import os
import yaml
async def main(query: str, hf_token: str) -> str:
hugging_face_llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct", token=hf_token)
ddg = DuckDuckGoSearchToolSpec()
search_tool = FunctionTool.from_defaults(ddg.duckduckgo_full_search)
agent = AgentWorkflow.from_tools_or_functions(
[search_tool],
llm=hugging_face_llm,
system_prompt="""
You are a general AI assistant. I will ask you a question.
Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
"""
)
ctx = Context(agent)
response = await agent.run(query, ctx=ctx)
return response