Skip to content

Agent framework and applications built upon Qwen, featuring Code Interpreter and Chrome browser extension.

License

Notifications You must be signed in to change notification settings

Chevolier/Qwen-Agent

 
 

Repository files navigation

中文 | English


Qwen-Agent is a framework for developing LLM applications based on the instruction following, tool usage, planning, and memory capabilities of Qwen. It also comes with example applications such as Browser Assistant, Code Interpreter, and Custom Assistant.

Getting Started

Installation

# Install dependencies.
git clone https://github.com/QwenLM/Qwen-Agent.git
cd Qwen-Agent
pip install -e ./

Preparation: Model Service

You can either use the model service provided by DashScope from Alibaba Cloud, or deploy your own model service using the open-source Qwen models.

If you want to use the model service provided by DashScope, please configure the environment variable:

# You need to replace YOUR_DASHSCOPE_API_KEY with your real DASHSCOPE_API_KEY.
export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY

If you want to deploy and use your own model service, please follow the instruction below, which is provided by the Qwen project, to deploy a service compatible with the OpenAI API:

# Install dependencies.
git clone [email protected]:QwenLM/Qwen.git
cd Qwen
pip install -r requirements.txt
pip install fastapi uvicorn "openai<1.0.0" "pydantic>=2.3.0" sse_starlette

# Start the model service
#   -c to specify any open-source model listed at https://huggingface.co/Qwen
#   --server-name 0.0.0.0 allows other machines to access your service.
#   --server-name 127.0.0.1 only allows the machine deploying the model to access the service.
python openai_api.py --server-name 0.0.0.0 --server-port 7905 -c Qwen/Qwen-72B-Chat

Developing Your Own Agent

Qwen-Agent provides atomic components such as LLMs and prompts, as well as high-level components such as Agents. The example below uses the Assistant component as an illustration, demonstrating how to add custom tools and quickly develop an agent that uses tools.

import json
import os

import json5
import urllib.parse
from qwen_agent.agents import Assistant
from qwen_agent.tools.base import BaseTool, register_tool

llm_cfg = {
    # Use the model service provided by DashScope:
    'model': 'qwen-max',
    'model_server': 'dashscope',
    # Use your own model service compatible with OpenAI API:
    # 'model': 'Qwen',
    # 'model_server': 'http://127.0.0.1:7905/v1',

    # (Optional) LLM hyper-paramters:
    'generate_cfg': {
        'top_p': 0.8
    }
}
system = 'According to the user\'s request, you first draw a picture and then automatically run code to download the picture ' + \
          'and select an image operation from the given document to process the image'

# Add a custom tool named my_image_gen:
@register_tool('my_image_gen')
class MyImageGen(BaseTool):
    description = 'AI painting (image generation) service, input text description, and return the image URL drawn based on text information.'
    parameters = [{
        'name': 'prompt',
        'type': 'string',
        'description': 'Detailed description of the desired image content, in English',
        'required': True
    }]

    def call(self, params: str, **kwargs) -> str:
        prompt = json5.loads(params)['prompt']
        prompt = urllib.parse.quote(prompt)
        return json.dumps(
            {'image_url': f'https://image.pollinations.ai/prompt/{prompt}'},
            ensure_ascii=False)


tools = ['my_image_gen', 'code_interpreter']  # code_interpreter is a built-in tool in Qwen-Agent
bot = Assistant(llm=llm_cfg,
                system_message=system,
                function_list=tools,
                files=[os.path.abspath('doc.pdf')])

messages = []
while True:
    query = input('user question: ')
    messages.append({'role': 'user', 'content': query})
    response = []
    for response in bot.run(messages=messages):
        print('bot response:', response)
    messages.extend(response)

The framework also provides more atomic components for developers to combine. For additional showcases, please refer to the examples directory.

Example Application: BrowserQwen

We have also developed an example application based on Qwen-Agent: a Chrome browser extension called BrowserQwen, which has key features such as:

  • You can discuss with Qwen regarding the current webpage or PDF document.
  • It records the web pages and PDF/Word/PowerPoint materials that you have browsed. It helps you understand multiple pages, summarize your browsing content, and automate writing tasks.
  • It comes with plugin integration, including Code Interpreter for math problem solving and data visualization.

BrowserQwen Demonstration

You can watch the following showcase videos to learn about the basic operations of BrowserQwen:

  • Long-form writing based on visited webpages and PDFs. video
  • Drawing a plot using code interpreter based on the given information. video
  • Uploading files, multi-turn conversation, and data analysis using code interpreter. video

Workstation - Editor Mode

This mode is designed for creating long articles based on browsed web pages and PDFs.

It allows you to call plugins to assist in rich text creation.

Workstation - Chat Mode

In this mode, you can engage in multi-webpage QA.

Create data charts using the code interpreter.

Browser Assistant

Web page QA

PDF document QA

BrowserQwen User Guide

Step 1. Deploy Local Database Service

On your local machine (the machine where you can open the Chrome browser), you will need to deploy a database service to manage your browsing history and conversation history.

If you are using DashScope's model service, then please execute the following command:

# Start the database service, specifying the model on DashScope by using the --llm flag.
# The value of --llm can be one of the following, in increasing order of resource consumption:
#   - qwen-7b/14b/72b-chat (the same as the open-sourced 7B/14B/72B-Chat model)
#   - qwen-turbo, qwen-plus, qwen-max
# "YOUR_DASHSCOPE_API_KEY" is a placeholder. The user should replace it with their actual key.
python run_server.py --api_key YOUR_DASHSCOPE_API_KEY --model_server dashscope --llm qwen-max --workstation_port 7864

If you are using your own model service instead of DashScope, then please execute the following command:

# Start the database service, specifying the model service deployed with --model_server.
# If the IP address of the model service is 123.45.67.89,
#     you can specify --model_server http://123.45.67.89:7905/v1
# If the model service and the database service are on the same machine,
#     you can specify --model_server http://127.0.0.1:7905/v1
python run_server.py --model_server http://{MODEL_SERVER_IP}:7905/v1 --workstation_port 7864

Now you can access http://127.0.0.1:7864/ to use the Workstation's Editor mode and Chat mode.

Step 2. Install Browser Assistant

Install the BrowserQwen Chrome extension:

  • Open the Chrome browser and enter chrome://extensions/ in the address bar, then press Enter.
  • Make sure that the Developer mode in the top right corner is turned on, then click on Load unpacked to upload the browser_qwen directory from this project and enable it.
  • Click the extension icon in the top right corner of the Chrome browser to pin BrowserQwen to the toolbar.

Note that after installing the Chrome extension, you need to refresh the page for the extension to take effect.

When you want Qwen to read the content of the current webpage:

  • Click the Add to Qwen's Reading List button on the screen to authorize Qwen to analyze the page in the background.
  • Click the Qwen icon in the browser's top right corner to start interacting with Qwen about the current page's content.

Evaluation Benchmark

We have also open-sourced a benchmark for evaluating the performance of a model in writing Python code and using Code Interpreter for mathematical problem solving, data analysis, and other general tasks. The benchmark can be found in the benchmark directory. The current evaluation results are as follows:

In-house Code Interpreter Benchmark (Version 20231206)
Model Accuracy of Code Execution Results (%) Executable Rate of Code (%)
Math↑Visualization-Hard↑Visualization-Easy↑General↑
GPT-4 82.8 66.7 60.8 82.8
GPT-3.5 47.3 33.3 55.7 74.1
LLaMA2-13B-Chat 8.3 1.2 15.2 48.3
CodeLLaMA-13B-Instruct 28.2 15.5 21.5 74.1
InternLM-20B-Chat 34.6 10.7 24.1 65.5
ChatGLM3-6B 54.2 4.8 15.2 62.1
Qwen-1.8B-Chat 25.6 21.4 22.8 65.5
Qwen-7B-Chat 41.9 23.8 38.0 67.2
Qwen-14B-Chat 58.4 31.0 45.6 65.5
Qwen-72B-Chat 72.7 41.7 43.0 82.8

Disclaimer

This project is not intended to be an official product, rather it serves as a proof-of-concept project that highlights the capabilities of the Qwen series models.

Important: The code interpreter is not sandboxed, and it executes code in your own environment. Please do not ask Qwen to perform dangerous tasks, and do not directly use the code interpreter for production purposes.

About

Agent framework and applications built upon Qwen, featuring Code Interpreter and Chrome browser extension.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 94.4%
  • JavaScript 3.5%
  • HTML 1.2%
  • CSS 0.9%