Skip to content

Commit

Permalink
improve the implementation of naive RAG
Browse files Browse the repository at this point in the history
  • Loading branch information
JianxinMa committed Apr 25, 2024
1 parent e271cdf commit 8bc6a05
Show file tree
Hide file tree
Showing 75 changed files with 2,160 additions and 1,759 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ log.jsonl

ai_agent/debug.json
ai_agent/local_prompts/*
*/debug.json
**/debug.json
**/debug.log*
debug.json
ai_agent/log.jsonl
qwen_agent.egg-info/*
Expand Down
2 changes: 1 addition & 1 deletion docs/agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The Agent receives a list of messages as input and produces a generator that yie

Different Agent classes have various workflows. In the [agents](../qwen_agent/agents) directory, we provide several different fundamental Agent subclasses.
For instance, the [ArticleAgent](../qwen_agent/agents/article_agent.py) returns a message that includes an article;
the [DocQAAgent](../qwen_agent/agents/docqa_agent.py) returns a message that contains the results of a document Q&A Results.
the [BasicDocQA](../qwen_agent/agents/doc_qa/basic_doc_qa.py) returns a message that contains the results of a document Q&A Results.

These types of Agents have relatively fixed response patterns and are suited for fairly specific use cases.

Expand Down
2 changes: 1 addition & 1 deletion docs/agent_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Agent接收一个消息列表输入,并返回一个消息列表的生成器,

不同Agent类具有不同的工作流程,我们在[agents](../qwen_agent/agents)目录提供了多个不同的基础的Agent子类,
例如[ArticleAgent](../qwen_agent/agents/article_agent.py)接收消息后,返回消息包含一篇文章;
[DocQAAgent](../qwen_agent/agents/docqa_agent.py)返回消息包含文档问答的结果。
[BasicDocQA](../qwen_agent/agents/doc_qa/basic_doc_qa.py)返回消息包含文档问答的结果。
可以看出,这类Agent回复模式相对固定,使用场景也比较固定。

### 1.1. Assistant 类
Expand Down
24 changes: 16 additions & 8 deletions examples/assistant_add_custom_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,34 @@ class MyImageGen(BaseTool):
'name': 'prompt',
'type': 'string',
'description': 'Detailed description of the desired image content, in English',
'required': True
'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)
return json.dumps(
{'image_url': f'https://image.pollinations.ai/prompt/{prompt}'},
ensure_ascii=False,
)


def init_agent_service():
llm_cfg = {'model': 'qwen-max'}
system = ('According to the user\'s request, you first draw a picture and then automatically '
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')

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.join(ROOT_RESOURCE, 'doc.pdf')])
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.join(ROOT_RESOURCE, 'doc.pdf')],
)

return bot

Expand Down
15 changes: 10 additions & 5 deletions examples/assistant_growing_girl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
def init_agent_service():
llm_cfg = {'model': 'qwen-max'}
tools = ['image_gen']
bot = Assistant(llm=llm_cfg,
function_list=tools,
system_message='你扮演一个漫画家,根据我给你的女孩的不同阶段,使用工具画出每个阶段女孩的的图片,'
'并串成一个故事讲述出来。要求图片背景丰富')
bot = Assistant(
llm=llm_cfg,
function_list=tools,
system_message='你扮演一个漫画家,根据我给你的女孩的不同阶段,使用工具画出每个阶段女孩的的图片,'
'并串成一个故事讲述出来。要求图片背景丰富',
)
return bot


Expand Down Expand Up @@ -42,7 +44,10 @@ def app():
messages.extend(response)


def test(query='请用image_gen开始创作!', file: Optional[str] = os.path.join(ROOT_RESOURCE, 'growing_girl.pdf')):
def test(
query='请用image_gen开始创作!',
file: Optional[str] = os.path.join(ROOT_RESOURCE, 'growing_girl.pdf'),
):
# Define the agent
bot = init_agent_service()

Expand Down
6 changes: 5 additions & 1 deletion examples/assistant_weather_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ def init_agent_service():
'你需要查询相应地区的天气,然后调用给你的画图工具绘制一张城市的图,并从给定的诗词文档中选一首相关的诗词来描述天气,不要说文档以外的诗词。')

tools = ['image_gen', 'amap_weather']
bot = Assistant(llm=llm_cfg, system_message=system, function_list=tools)
bot = Assistant(
llm=llm_cfg,
system_message=system,
function_list=tools,
)

return bot

Expand Down
143 changes: 0 additions & 143 deletions examples/gpt_mentions.py

This file was deleted.

44 changes: 24 additions & 20 deletions examples/group_chat_chess.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,30 @@
CFGS = {
'background':
f'一个五子棋群组,棋盘为5*5,黑棋玩家和白棋玩家交替下棋,每次玩家下棋后,棋盘进行更新并展示。{NPC_NAME}下白棋,{USER_NAME}下黑棋。',
'agents': [{
'name':
'棋盘',
'description':
'负责更新棋盘',
'instructions':
'你扮演一个五子棋棋盘,你可以根据原始棋盘和玩家下棋的位置坐标,把新的棋盘用矩阵展示出来。棋盘中用0代表无棋子、用1表示黑棋、用-1表示白棋。用坐标<i,j>表示位置,i代表行,j代表列,棋盘左上角位置为<0,0>。',
'selected_tools': ['code_interpreter']
}, {
'name':
NPC_NAME,
'description':
'白棋玩家',
'instructions':
'你扮演一个玩五子棋的高手,你下白棋。棋盘中用0代表无棋子、用1黑棋、用-1白棋。用坐标<i,j>表示位置,i代表行,j代表列,棋盘左上角位置为<0,0>,请决定你要下在哪里,你可以随意下到一个位置,不要说你是AI助手不会下!返回格式为坐标:\n<i,j>\n除了这个坐标,不要返回其他任何内容',
}, {
'name': USER_NAME,
'description': '黑棋玩家',
'is_human': True
}]
'agents': [
{
'name':
'棋盘',
'description':
'负责更新棋盘',
'instructions':
'你扮演一个五子棋棋盘,你可以根据原始棋盘和玩家下棋的位置坐标,把新的棋盘用矩阵展示出来。棋盘中用0代表无棋子、用1表示黑棋、用-1表示白棋。用坐标<i,j>表示位置,i代表行,j代表列,棋盘左上角位置为<0,0>。',
'selected_tools': ['code_interpreter'],
},
{
'name':
NPC_NAME,
'description':
'白棋玩家',
'instructions':
'你扮演一个玩五子棋的高手,你下白棋。棋盘中用0代表无棋子、用1黑棋、用-1白棋。用坐标<i,j>表示位置,i代表行,j代表列,棋盘左上角位置为<0,0>,请决定你要下在哪里,你可以随意下到一个位置,不要说你是AI助手不会下!返回格式为坐标:\n<i,j>\n除了这个坐标,不要返回其他任何内容',
},
{
'name': USER_NAME,
'description': '黑棋玩家',
'is_human': True
},
],
}


Expand Down
33 changes: 16 additions & 17 deletions examples/multi_agent_router.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""A multi-agent cooperation example implemented by router and assistant"""

import os
from typing import Optional

Expand All @@ -14,23 +15,21 @@ def init_agent_service():
tools = ['image_gen', 'code_interpreter']

# Define a vl agent
bot_vl = Assistant(llm=llm_cfg_vl)
bot_vl = Assistant(llm=llm_cfg_vl, name='多模态助手', description='可以理解图像内容。')

# Define a tool agent
bot_tool = ReActChat(llm=llm_cfg, function_list=tools)
bot_tool = ReActChat(
llm=llm_cfg,
name='工具助手',
description='可以使用画图工具和运行代码来解决问题',
function_list=tools,
)

# Define a router (simultaneously serving as a text agent)
bot = Router(llm=llm_cfg,
agents={
'vl': {
'obj': bot_vl,
'desc': '多模态助手,可以理解图像内容。'
},
'tool': {
'obj': bot_tool,
'desc': '工具助手,可以使用画图工具和运行代码来解决问题'
}
})
bot = Router(
llm=llm_cfg,
agents=[bot_vl, bot_tool],
)
return bot


Expand Down Expand Up @@ -65,10 +64,10 @@ def app():


def test(
query: str = 'hello',
image: # noqa
str = 'https://img01.sc115.com/uploads/sc/jpgs/1505/apic11540_sc115.com.jpg', # noqa
file: Optional[str] = os.path.join(ROOT_RESOURCE, 'poem.pdf')): # noqa
query: str = 'hello',
image: str = 'https://img01.sc115.com/uploads/sc/jpgs/1505/apic11540_sc115.com.jpg',
file: Optional[str] = os.path.join(ROOT_RESOURCE, 'poem.pdf'),
):
# Define the agent
bot = init_agent_service()

Expand Down
6 changes: 2 additions & 4 deletions examples/react_data_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ def app():
messages.extend(response)


def test(
query: # noqa
str = 'pd.head the file first and then help me draw a line chart to show the changes in stock prices',
file: Optional[str] = os.path.join(ROOT_RESOURCE, 'stock_prices.csv')): # noqa
def test(query: str = 'pd.head the file first and then help me draw a line chart to show the changes in stock prices',
file: Optional[str] = os.path.join(ROOT_RESOURCE, 'stock_prices.csv')):
# Define the agent
bot = init_agent_service()

Expand Down
4 changes: 2 additions & 2 deletions examples/visual_storytelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def _run(self,
**kwargs) -> Iterator[List[Message]]:
"""Define the workflow"""

assert (isinstance(messages[-1]['content'], list) and
any([item.image for item in messages[-1]['content']])), 'This agent requires input of images'
assert isinstance(messages[-1]['content'], list)
assert any([item.image for item in messages[-1]['content']]), 'This agent requires input of images'

# Image understanding
new_messages = copy.deepcopy(messages)
Expand Down
2 changes: 1 addition & 1 deletion qwen_agent/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.0.2'
__version__ = '0.0.3'
from .agent import Agent

__all__ = ['Agent']
Loading

0 comments on commit 8bc6a05

Please sign in to comment.