import osimport agentopsfrom dotenv import load_dotenvfrom swarmzero import Agentfrom tavily import TavilyClientload_dotenv()agentops.init(os.getenv("AGENTOPS_API_KEY"))tavily_client = TavilyClient(api_key=os.getenv("TAVILY_API_KEY"))asyncdefweb_search(query:str)->dict: response = tavily_client.search(query) results =[]for result in response["results"][:3]: results.append({"title": result["title"],"url": result["url"],"content": result["content"]})return resultsasyncdefextract_from_urls(urls:list[str])->dict: response = tavily_client.extract(urls=urls)if response["failed_results"]:print(f"Failed to extract from {response['failed_results']}") results =[]for result in response["results"]: results.append({"url": result["url"],"raw_content": result["raw_content"]})return resultsmy_agent = Agent( name="workflow-assistant", functions=[ web_search, extract_from_urls,], config_path="./swarmzero_config.toml",# see https://github.com/swarmzero/swarmzero/blob/main/swarmzero_config_example.toml instruction="You are a helpful assistant that can search the web and extract information from a given URL.",# chat_only_mode=True # remove comment only if using `my_agent.chat()`)my_agent.run()# see agent API at localhost:8000/docs"""# chat directly without starting the agent's serverimport asyncioresponse = asyncio.run(my_agent.chat(prompt="what is Decentralized-AI about about?"))print(response)"""
Once your agent is running, you can interact with it using HTTP requests:
curl-X'POST'\'http://localhost:8000/api/v1/chat'\-H'accept: application/json'\-H'Content-Type: multipart/form-data'\-F'user_id=test_user'\-F'session_id=test_web_search_agent'\-F'chat_data={"messages":[{"role":"user","content":"what is Decentralized-AI about about?"}]}'
This example can be found in this notebook
This full code for this example can be found in this repository.