autogen_with_chromadb
1.0.0
Autogen是一个开源框架,可以使用多种代理来开发会话AI应用程序。
Chroma DB是用于存储和检索矢量嵌入的开源矢量数据库。
virtualenv -p python3.11 env_namepython -m venv env_nameenv_name/scripts/activate pip install -U "pyautogen[retrievechat]" chromadb
-U告诉PIP在安装之前将所有已经安装的软件包升级到其最新版本。"pyautogen[retrievechat]"安装Pyautogen包,还安装了该包装的可选“检索”额外功能 export AUTOGEN_USE_DOCKER=False
$Env:AUTOGEN_USE_DOCKER="False"
导出AUTOGEN_USE_DOCKER=False告诉Pyautogen直接在主机上运行其任务,而不是使用Docker容器。它绕过了Docker依赖性,但也失去了Docker提供的一些隔离好处。
export OPENAI_API_KEY=Fxxxxxxxxxxxxxxxxxxxxxxxxx
$Env:OPENAI_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxx"
app.py python app.py
该代码文件使用Autogen和Chromadb库定义了聊天机器人系统。这是代码的分步分类:
第一步是导入必要的库。在这种情况下,我们使用Autogen和Chromadb来创建一个可以从数据库中检索信息并基于语言模型生成响应的聊天机器人。
import autogen
import chromadb接下来,我们使用Autogen库中的AssistantAgent课程定义聊天机器人助手。此类将名称,语言模型配置和系统消息作为输入。
assistant = AssistantAgent (
name = "my_assistant" ,
llm_config = llm_config_proxy ,
system_message = "You are a helpful assistant. Provide accurate answers based on the context. Respond 'Unsure about answer' if uncertain."
)我们还使用autogen.agentchat.contrib模块的retrieveuserproxyagent类定义用户。此类采用名称,人类输入模式,系统消息,最大连续自动复制数量以及从数据库中检索信息作为输入的配置。
user = RetrieveUserProxyAgent (
name = "me_user" ,
human_input_mode = "NEVER" ,
system_message = "Assistant who has extra content retrieval power for solving difficult problems." ,
max_consecutive_auto_reply = 10 ,
retrieve_config = {
"task" : "code" ,
"docs_path" : [ './docs/autogen.pdf' ],
"chunk_token_size" : 1000 ,
"model" : config_list [ 0 ][ "model" ],
"client" : chromadb . PersistentClient ( path = '/tmp/chromadb' ),
"collection_name" : "pdfreader" ,
"get_or_create" : True ,
},
code_execution_config = { "work_dir" : "coding" },
)我们将用户的问题或提示定义为字符串变量。
user_question = """
Compose a short blog post showcasing how AutoGen is revolutionizing the future of Generative AI
through the collaboration of various agents. Craft an introduction, main body, and a compelling
conclusion. Encourage readers to share the post. Keep the post under 500 words.
"""最后,我们使用retrieveuserproxyagent类的initiate_chat方法启动用户和聊天机器人之间的聊天会话。
user . initiate_chat ( assistant , problem = user_question )总体而言,此代码文件定义了一个聊天机器人系统,该系统可以通过从数据库中检索信息并根据语言模型生成响应来回答用户问题或提示。聊天机器人还可以执行代码并根据用户问题的上下文提供答案。