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 )總體而言,此代碼文件定義了一個聊天機器人系統,該系統可以通過從數據庫中檢索信息並根據語言模型生成響應來回答用戶問題或提示。聊天機器人還可以執行代碼並根據用戶問題的上下文提供答案。