Duke-911's picture
Refactor async interaction with AgentWorkflow to include context parameter
ea9aafe
Raw
History Blame Contribute Delete
1.4 kB
from llama_index.core.agent.workflow import AgentWorkflow
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
from llama_index.core.workflow import Context
# Import our custom tools from their modules
from tools import get_weather_info_tool, get_hub_stats_tool, search_tool
from retriever import guest_info_tool
import gradio as gr
import asyncio
llm = HuggingFaceInferenceAPI(model_name='Qwen/Qwen2.5-Coder-32B-Instruct')
alfred = AgentWorkflow.from_tools_or_functions(
tools_or_functions=[
get_weather_info_tool,
get_hub_stats_tool,
search_tool,
guest_info_tool
],
llm=llm,
verbose=True,
)
ctx = Context(alfred)
# Define a function to interact with the AgentWorkflow
async def async_interact_with_agent(input_text, ctx=ctx):
response = await alfred.run(input_text, ctx=ctx)
return response
def interact_with_agent(input_text):
# Ensure the async function is run in an event loop
return asyncio.run(async_interact_with_agent(input_text))
if __name__ == "__main__":
# Create a Gradio interface
interface = gr.Interface(
fn=interact_with_agent,
inputs=gr.Textbox(label="Enter your query"),
outputs=gr.Textbox(label="Response"),
title="Agent Workflow Interface",
description="Interact with the AgentWorkflow using this Gradio interface."
)
interface.launch()