YouTip LogoYouTip

Ai Agent Answer Demo

AI Agent Q&A Example

This chapter we will build our first real AI Agent.

We start with a simple Q&A Agent and gradually add more features.

Project Structure Preparation

First create the directory tutorial-ai-agent:

mkdir tutorial-ai-agent

Enter the directory tutorial-ai-agent, use the uv command to create a virtual environment:

cd tutorial-ai-agent # Create a virtual environment named .venv (default) uv venv # Activate environment (macOS/Linux) source .venv/bin/activate # Activate environment (Windows).venvScriptsactivate

Use uv to create and activate the virtual environment, for more related content you can read uv Tutorial.

Create the project directory structure:

tutorial-ai-agent/β”œβ”€β”€ .env β”œβ”€β”€ .gitignore β”œβ”€β”€ requirements.txt β”œβ”€β”€ test_basic_agent.py β”œβ”€β”€ README.md β”œβ”€β”€ src/β”‚ β”œβ”€β”€ __init__.py β”‚ └── simple_agent.py └── tests/ └── __init__.py

Image 1

Apply for API Key on Alibaba Bailian

This chapter we need to use the search function, we use the Alibaba Qwen model because it has built-in search functionality.

Alibaba Cloud Bailian's Tongyi Qwen model supports OpenAI-compatible interface, you only need to adjust the API Key, BASE_URL and model name to migrate existing OpenAI code to Alibaba Cloud Bailian service.

  • base_url: Replace with https://dashscope.aliyuncs.com/compatible-mode/v1
  • api_key: Replace with Alibaba Cloud Bailian API Key
  • model: Replace with qwen3-max

Reference: https://help.aliyun.com/zh/model-studio/compatibility-of-openai-with-dashscope

We need to activate Alibaba Cloud Bailian model service and obtain an API-KEY.

We can first use the Alibaba Cloud main account to access the Bailian model service platform: https://bailian.console.aliyun.com/, then click login in the upper right corner, after successful login click the gear βš™οΈ icon in the upper right corner, select API key, then copy the API key, if there is none you can also create an API key:

Image 2

Image 3

Activating Alibaba Cloud Bailian does not incur fees, only model calls (after exceeding free quota), model deployment, and model fine-tuning will incur corresponding charges.

Now to use the API, all need to be billed by token, fortunately they are not expensive, we can first purchase the cheapest package: Alibaba Cloud Bailian Large Model Service Platform.

Image 4

You can also directly use the Bailian and Ark Coding Plan package: .

Create Basic Agent Class:

Example

# src/simple_agent.py

import os

from typing import List, Dict, Any

from openai import OpenAI

from dotenv import load_dotenv

# Load environment variables

load_dotenv()

class SimpleQAAgent:

    """Simple Q&A Agent"""

    def __init__ (self, model: str="qwen3-max"): # Default to DeepSeek model

        """

        Initialize Agent
        Args:

            model: Name of the model to use, default is DeepSeek

        """

        # api_key = os.getenv("OPENAI_API_KEY")

        api_key ="sk-xxx"# Your applied API key

        base_url ="https://dashscope.aliyuncs.com/compatible-mode/v1"

        if not api_key:

            raise ValueError("Please set OPENAI_API_KEY environment variable")

        self.client= OpenAI(

            api_key = api_key,

            base_url = base_url)

        self.model= model

        self.conversation_history: List[Dict[str,str]]=[]

        self.system_prompt="You are a helpful AI assistant, please answer user questions politely and accurately."

    def add_to_history(self, role: str, content: str):

        """Add message to conversation history"""

        self.conversation_history.append({

            "role": role,

            "content": content

        })

        # Keep history length within 10 rounds (prevent token overflow)

        if len(self.conversation_history)>10:

            self.conversation_history=self.conversation_history[-10:]

    def ask(self, question: str) ->str:

        """

        Ask the Agent a question
        Args:

            question: User's question

        Returns:

            Agent's answer

        """

        # Add user question to history

        self.add_to_history("user", question)

        # Prepare message list

        messages =[

            {"role": "system","content": self.system_prompt}

        ]

        messages.extend(self.conversation_history)

        try:

            # Call OpenAI API

            response =self.client.chat.completions.create(

                model=self.model,

                messages=messages,

                temperature=0.7,

                max_tokens=500

            )

            # Extract answer

            answer = response.choices.message.content

            # Add assistant answer to history

            self.add_to_history("assistant", answer)

            return answer

        except Exception as e:

            error_msg = f"Error calling API: {str(e)}"

            print(error_msg)

            return error_msg

    def clear_history(self):

        """Clear conversation history"""

        self.conversation_history.clear()

Create a test script to test the basic Agent:

Example

# test_basic_agent.py

from src.simple_agent import SimpleQAAgent

def test_basic_agent():

    """Test basic Q&A Agent"""

    print("=== Testing Basic Q&A Agent ===")

    # Create Agent

    agent = SimpleQAAgent()

    # Test Q&A

    questions =[

        "Hello, please introduce yourself",

        "What is Python?",

        "What is the difference between machine learning and artificial intelligence?"

    ]

    for question in questions:

        print(f"n User: {question}")

        answer = agent.ask(question)

        print(f"Assistant: {answer}")

    # Test conversation coherence

    print("n=== Testing Conversation Coherence ===")

    agent.clear_history()

    # Round 1

    q1 ="My favorite color is blue"

    a1 = agent.ask(q1)

    print(f"User: {q1}")

    print(f"Assistant: {a1}")

    # Round 2 (should remember previous conversation)

    q2 ="What color did I just say I like most?"

    a2 = agent.ask(q2)

    print(f"n User: {q2}")

    print(f"Assistant: {a2}")

    print("n Test completed!")

if __name__ =="__main__":

    test_basic_agent()

Run test:

python test_basic_agent.py === Testing Basic Q&A Agent ===User: Hello, please introduce yourself tutorial Assistant: Hello! I am Tongyi Qianwen (Qwen), a super-large-scale language model independently developed by Alibaba Group. I can help you answer questions, create text, such as writing stories, official documents, emails, scripts, logical reasoning, programming, etc., and can also express opinions, play games, etc. If you have any questions or need help, feel free to let me know!User: What is Python?Assistant: Hello! Python is a **high-level, interpreted, general-purpose programming language**, first released by Dutch programmer **Guido van Rossum** in 1991. Its design philosophy emphasizes **readability** and **simplicity** of code, with clear and concise syntax that allows beginners to get started quickly....

This basic Agent has several obvious limitations:

  • Can only answer knowledge from training data
  • Unable to obtain real-time information
  • Cannot perform calculations
  • Cannot operate external systems

This is exactly why we need to add tools to the Agent.


Add Search Tool

Qwen has built-in search function, passing the enable_search: true parameter enables internet search functionality.

# Import dependencies and create client... completion = client.chat.completions.create( # Need to use model that supports internet search model="qwen3-max", messages=[{"role": "user", "content": "What is the weather in Hangzhou tomorrow?"}], # Since enable_search is not an standard OpenAI parameter, using Python SDK requires passing through extra_body (using Node.js SDK requires passing as top-level parameter) extra_body={"enable_search": True})

Documentation: https://help.aliyun.com/zh/model-studio/web-search

Agent with Integrated Search Tool

Now we integrate the search tool into the Agent, create agent_with_search.py file under the src directory:

Example

# src/agent_with_search.py

import os

from openai import OpenAI

from dotenv import load_dotenv

load_dotenv()

class AgentWithSearch:

    """Agent based on Qwen's built-in internet search capability"""

    def __init__ (self, model: str="qwen3-max"):

        api_key ="sk-xxx"

        base_url ="https://dashscope.aliyuncs.com/compatible-mode/v1"

        if not api_key:

            raise ValueError("Please set OPENAI_API_KEY environment variable (DashScope Key)")

        self.client= OpenAI(

            api_key=api_key,

            base_url=base_url

        )

        self.model= model

        self.system_prompt="""

        You are a helpful AI assistant.
        When questions involve real-time information, latest events, or content requiring fact-checking,

        you can obtain the latest information through internet search, and indicate in your answer that the information comes from the internet.

        """

    def ask(self, question: str) ->str:

        messages =[

            {"role": "system","content": self.system_prompt},

            {"role": "user","content": question}

        ]

        try:

            response =self.client.chat.completions.create(

                model=self.model,

                messages=messages,

                temperature=0.7,

                max_tokens=800,

                extra_body={

                    "enable_search": True

                }

            )

            return response.choices.message.content

        except Exception as e:

            return f"Error: {repr(e)}"

    def interactive_chat(self):

        print("=== Qwen Internet Search Agent ===")

        print("Enter quit / exit to end conversation")

        while True:

            question =input("n You: ").strip()

            if question.lower()in{"quit","exit"}:

                break

            print("Assistant:",self.ask(question))

Test Search Agent

Create test_search_agent.py file in the root directory:

Example

# test_search_agent.py

from src.agent_with_search import AgentWithSearch

def test_search_agent():

    agent = AgentWithSearch()

    questions =[

        "What is the weather in Beijing today?",

        "What are the latest tech news?",

        "Where was the 2024 Olympics held?",

        "What is Python?"

    ]

    for q in questions:

        print(f"n User: {q}")

        print("Assistant:", agent.ask(q)[:300],"...")

if __name__ =="__main__":

    test_search_agent()

Run test:

python test_search_agent.py User: What is the weather in Beijing today?Assistant: Today is February 7, 2026, Saturday. According to the latest weather forecast information, the weather in Beijing today is as follows: - **Weather Condition**: Cloudy turning to sunny - **Temperature Range**: Minimum temperature about **-9℃**, maximum temperature about **2℃** (some areas like Haidian District recorded **-5℃ ~ 5℃**) - **Wind Direction and Force**: **Northeast wind level 1** during the day and night, some areas turn to **south wind turning to northwest wind, less than level 3** in the afternoon - **Air Quality**: **37 (Excellent)** - **Clothing Advice**: It is recommended to wear heavy warm clothing such as cotton-padded clothes, winter coats, leather jackets, thick overcoats, down jackets, etc., and wear gloves, hats and other cold-proof accessories ...
← React OnlineAi Agent Memory System Design β†’