[Python] Autogen: How to get a simple csv from the search instead of using

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to get data from the web. Unfortunately, I get this error. I suppose that it identifies a big file using a search and then tries to have a conversation about that entire file. Is there any way to navigate him to download the file, check the headers, and then process it? [​IMG]

    import os
    import requests
    from typing import Dict
    from autogen import AssistantAgent, UserProxyAgent, register_function
    from dotenv import load_dotenv

    # Load environment variables from a .env file
    load_dotenv()

    # Define the web search function
    def web_search(query: str) -> Dict:
    """Perform a web search and return the top result."""
    subscription_key = os.getenv('BING_SEARCH_API_KEY')
    if not subscription_key:
    raise ValueError("Bing Search API key not found. Please set the 'BING_SEARCH_API_KEY' environment variable.")
    search_url = "https://api.bing.microsoft.com/v7.0/search"
    headers = {"Ocp-Apim-Subscription-Key": subscription_key}
    params = {"q": query, "textDecorations": True, "textFormat": "HTML"}
    response = requests.get(search_url, headers=headers, params=params)
    response.raise_for_status()
    search_results = response.json()
    return search_results

    # Define the function to determine if a message is a termination message
    def is_termination_message(message):
    return message.get("content", "").strip().lower() == "goodbye!"

    # Define the LLM configuration
    llm_config = {
    "model": "gpt-3.5-turbo",
    "api_key": os.getenv("OPENAI_API_KEY"),
    }

    # Initialize the assistant agent
    assistant = AssistantAgent(
    name='assistant',
    llm_config=llm_config,
    is_termination_msg=is_termination_message
    )

    # Initialize the user proxy agent
    user_proxy = UserProxyAgent(
    name="user_proxy",
    llm_config=llm_config,
    code_execution_config={
    "work_dir": "code_execution",
    "use_docker": False
    },
    human_input_mode="ALWAYS"
    )

    # Register the web_search function with both agents
    register_function(
    f=web_search,
    name="web_search",
    description="Perform a web search and return the top result based on the query.",
    caller=assistant, # The assistant agent can suggest calls to the web_search function.
    executor=user_proxy # The user proxy agent can execute the web_search function.
    )

    # Initiate the chat between the user proxy and the assistant
    user_proxy.initiate_chat(
    assistant,
    message="Plot the GDP growth vs. unemployment in Czechia for the years 1993-2024."
    )


    simple_code_executor copy.py 3 KB

    Continue reading...

Compartilhe esta Página