先睹為快,九個(gè) Python 程序員還不知道的 AI 大模型開源庫
AI 大行其道,Python 作為 AI 開發(fā)最炙手可熱的編程語言,也水漲船高。
各位作為 Python 開發(fā)者怎么能不了解一下 Python 相關(guān)的 AI 開源庫呢?
以下我就為大家列出了一些目前還鮮為人知的 AI 開源庫,有幾個(gè)還真不錯(cuò)哦。
1. composio
composio
- 網(wǎng)址:https://github.com/ComposioHQ/composio
- 安裝:pip install composio-core
- 添加 GitHub 集成:composio add github
Composio 是為不同工具集構(gòu)建工作流的 AI 智能體。它支持 100 多種工具,包括谷歌應(yīng)用、Excel、Github、GitLab、Redis、Notion、Slack 等應(yīng)用,支持如點(diǎn)擊、輸入、復(fù)制、粘貼等系統(tǒng)操作,還支持智能搜索、截圖、下載、上傳等瀏覽器操作。
Composio 能夠?qū)崿F(xiàn)鑒權(quán)管理,將智能體與不同工具整合在一起,并執(zhí)行各種操作。它支持多種鑒權(quán)方式,包括 OAuth1.0/OAuth2.0、ApiKey 和基本驗(yàn)證等方式。
此外,Composio 還兼容 OpenAI、Claude、LlamaIndex、Langchain 和 Gemini 等多種智能體框架。
在它的官方文檔中提供了設(shè)置投資分析師智能體的示例,因?yàn)楸容^復(fù)雜,就不在本文中展示了,有興趣的朋友可以看一下。網(wǎng)址在這里,https://docs.composio.dev/guides/python/investment-analyst
在其 Github 說明文件里還有一個(gè)示例,創(chuàng)建智能體為 Github 資源庫加星的。這個(gè)操作比較簡單,大家可以看一下示例代碼。
1composio add github # Connect your Github - Run this in terminal
運(yùn)行以下 Python 腳本可以使用智能體給 Github 資源庫加星。
1from openai import OpenAI
2from composio_openai import ComposioToolSet, App, Action
3
4openai_client = OpenAI(
5 api_key="{{OPENAIKEY}}"
6)
7
8# Initialise the Composio Tool Set
9
10composio_tool_set = ComposioToolSet()
11
12# Get GitHub tools that are pre-configured
13actions = composio_tool_set.get_actions(
14 actinotallow=[Action.GITHUB_ACTIVITY_STAR_REPO_FOR_AUTHENTICATED_USER]
15)
16
17my_task = "Star a repo composiodev/composio on GitHub"
18
19# Setup openai assistant
20assistant_instruction = "You are a super intelligent personal assistant"
21
22assistant = openai_client.beta.assistants.create(
23 name="Personal Assistant",
24 instructinotallow=assistant_instruction,
25 model="gpt-4-turbo",
26 tools=actions,
27)
28
29# create a thread
30thread = openai_client.beta.threads.create()
31
32message = openai_client.beta.threads.messages.create(
33 thread_id=thread.id,
34 role="user",
35 cnotallow=my_task
36)
37
38# Execute Agent with integrations
39run = openai_client.beta.threads.runs.create(
40 thread_id=thread.id,
41 assistant_id=assistant.id
42)
43
44
45# Execute Function calls
46response_after_tool_calls = composio_tool_set.wait_and_handle_assistant_tool_calls(
47 client=openai_client,
48 run=run,
49 thread=thread,
50)
51
52print(response_after_tool_calls)
2. Julep
Julep
- 網(wǎng)址:https://docs.julep.ai/
- 安裝:pip install julep
Julep 的開發(fā)者在開發(fā) AI 應(yīng)用時(shí),發(fā)現(xiàn)讓大模型應(yīng)用具備記憶和知識(shí)的壁壘非常高,多智能體框架中的智能體操作難以控制,整合眾多開發(fā) AI 應(yīng)用的工具、技術(shù)與模型非常困難。為了解決這些問題,他們開發(fā)了 Julep ~ 這個(gè)支持狀態(tài)管理的大模型應(yīng)用開發(fā)平臺(tái)。
Julep 提供了內(nèi)置的狀態(tài)管理系統(tǒng),能夠自動(dòng)管理上下文,并使用 CozoDB 保存和提取聊天歷史。它支持不同用戶與智能體之間的交互,并能方便地在不同大模型框架之間進(jìn)行切換。
借助 Composio,Julep 內(nèi)置了 100 多種工具。此外,Julep 支持定義類似 GitHub Actions 的智能體工作流,以異步方式執(zhí)行任務(wù)。
它還支持使用 Docker Compose 進(jìn)行生產(chǎn)部署,很快還將支持 K8s。
以下是 Julep 的示例代碼,大家可以簡單了解一下。
1from julep import Client
2from pprint import pprint
3import textwrap
4import os
5
6base_url = os.environ.get("JULEP_API_URL")
7api_key = os.environ.get("JULEP_API_KEY")
8
9client = Client(api_key=api_key, base_url=base_url)
10
11#create agent
12agent = client.agents.create(
13 name="Jessica"
14 model="gpt-4",
15 tools=[] # Tools defined here
16)
17#create a user
18user = client.users.create(
19 name="Anon",
20 about="Average nerdy tech bro/girl spending 8 hours a day on a laptop,
21)
22#create a session
23situation_prompt = """You are Jessica. You're a stuck-up Cali teenager.
24You basically complain about everything. You live in Bel-Air, Los Angeles and
25drag yourself to Curtis High School when necessary.
26"""
27session = client.sessions.create(
28 user_id=user.id, agent_id=agent.id, situatinotallow=situation_prompt
29)
30#start a conversation
31
32user_msg = "hey. what do u think of Starbucks?"
33response = client.sessions.chat(
34 session_id=session.id,
35 messages=[
36 {
37 "role": "user",
38 "content": user_msg,
39 "name": "Anon",
40 }
41 ],
42 recall=True,
43 remember=True,
44)
45
46print("\n".join(textwrap.wrap(response.response[0][0].content, width=100)))
47
3. Aider
Aider
- 網(wǎng)址:https://aider.chat/docs/install.html
Aider 是一款 AI 結(jié)對(duì)編程輔助工具,支持啟動(dòng)新項(xiàng)目、編輯文件,并與現(xiàn)有的 GitHub 資源庫集成。
Aider 能夠調(diào)用 GPT-4o、Claude 3.5 Sonnet、DeepSeek Coder、Llama 70b 等大型語言模型。
它可以進(jìn)行代碼測(cè)試、解決 Bug、重構(gòu)代碼,甚至更新文檔,并支持 Python、JavaScript、TypeScript、PHP、HTML、CSS 等編程語言。
你可以在代碼編輯器中邊編寫代碼邊與 Aider 聊天,讓它提供建議,甚至使用語音編程功能。
- 安裝:
1$ pip install aider-chat
2
3# 進(jìn)入 git 資源庫的目錄
4$ cd /to/your/git/repo
5
6# 使用 Claude 3.5 Sonnet
7$ export ANTHROPIC_API_KEY=your-key-goes-here
8$ aider
9
10# 使用 GPT-4o
11$ export OPENAI_API_KEY=your-key-goes-here
12$ aider
4. Haystack
Haystack
- 網(wǎng)址:https://aider.chat/docs/install.html
- 安裝:pip install haystack-ai
Haystack 是構(gòu)建大模型應(yīng)用的開源框架,可用于開發(fā)檢索增強(qiáng)生成管道和高級(jí)搜索系統(tǒng),能夠智能地處理大規(guī)模文檔集合。
盡管構(gòu)建 AI 管道的框架很多,但如果需要將端到端的搜索管道集成到生產(chǎn)應(yīng)用中,Haystack 是首選。
無論是 RAG、問答系統(tǒng)還是語義搜索,Haystack 靈活的管道組合方式讓開發(fā)、維護(hù)和部署變得輕松便捷。
使用 Haystack 可以輕松地將排序器、向量存儲(chǔ)和解析器集成到管道中,從而將原型快速轉(zhuǎn)化為生產(chǎn)級(jí)的解決方案。
以下是使用 haystack 的一個(gè)小示例。
1import os
2
3from haystack import Pipeline, PredefinedPipeline
4import urllib.request
5
6os.environ["OPENAI_API_KEY"] = "Your OpenAI API Key"
7urllib.request.urlretrieve("https://www.gutenberg.org/cache/epub/7785/pg7785.txt", "davinci.txt")
8
9indexing_pipeline = Pipeline.from_template(PredefinedPipeline.INDEXING)
10indexing_pipeline.run(data={"sources": ["davinci.txt"]})
11
12rag_pipeline = Pipeline.from_template(PredefinedPipeline.RAG)
13
14query = "How old was he when he died?"
15result = rag_pipeline.run(data={"prompt_builder": {"query":query}, "text_embedder": {"text": query}})
16print(result["llm"]["replies"][0])
5. Mem0
Mem0
- 網(wǎng)址:https://github.com/mem0ai/mem0
- 安裝:pip install mem0ai
Mem0 為大模型提供了一個(gè)智能且自我優(yōu)化的長期記憶層,使個(gè)性化的 AI 體驗(yàn)?zāi)軌蜇灤┯诓煌瑧?yīng)用。
它能夠在用戶會(huì)話、交互操作和 AI 智能體之間保留信息,確保與用戶交互的連續(xù)性。同時(shí),Mem0 會(huì)根據(jù)用戶的互動(dòng)不斷優(yōu)化個(gè)性化體驗(yàn)。
Mem0 的 API 設(shè)計(jì)簡單直觀,便于無縫集成到各種應(yīng)用中。此外,它還能確保不同平臺(tái)和設(shè)備上的數(shù)據(jù)和行為保持一致。
6. FastEmbed
FastEmbed
- 網(wǎng)址:https://github.com/mem0ai/mem0
- 安裝:
1pip install fastembed
2
3# 或使用 GPU
4
5pip install fastembed-gpu
FastEmbed 是一個(gè)輕量級(jí)的高性能 Python 庫,用于嵌入生成模型。它支持多種流行的文本模型。默認(rèn)的文本嵌入模型是 Flag Embedding,支持 “query” 和 “passage” 前綴的輸入文本。
FastEmbed 不需要 GPU,無需下載 GB 級(jí)別的 PyTorch。嵌入生成的時(shí)間一般都很長,導(dǎo)致整個(gè)流程的速度很慢,F(xiàn)astEmbed 使用 ONNX 運(yùn)行時(shí),比 PyTorch 更快,同時(shí)可以利用數(shù)據(jù)并行來處理大型數(shù)據(jù)集。
以下是 FastEmbed 創(chuàng)建文檔嵌入的方法。
1from fastembed import TextEmbedding
2from typing import List
3
4# 文檔列表示例
5documents: List[str] = [
6 "This is built to be faster and lighter than other embedding libraries, e.g. Transformers, Sentence-Transformers, etc.",
7 "FastEmbed is supported by and maintained by Quadrant."
8]
9
10# 觸發(fā)模型下載與初始化
11embedding_model = TextEmbedding()
12print("The model BAAI/bge-small-en-v1.5 is ready to use.")
13
14embeddings_generator = embedding_model.embed(documents) # reminder this is a generator
15embeddings_list = list(embedding_model.embed(documents))
16 # You can also convert the generator to a list, and that to a Numpy array
17len(embeddings_list[0]) # Vector of 384 dimensions
7. LiteLLM
LiteLLM
- 網(wǎng)址:https://docs.litellm.ai/docs/
- 安裝:pip install litellm
很多大模型服務(wù)商并不遵循 OpenAI SDK 的文本、圖像或嵌入生成格式。LiteLLM 可以讓Claude、Gemini 等生成相同格式的內(nèi)容,讓它們作為 OpenAI 模型的替代品。
LiteLLM 支持 100 多個(gè)大模型,讓它們使用相同的輸入輸出格式。還可以跨多個(gè)部署(如 Azure/OpenAI)實(shí)現(xiàn)重試與回調(diào)邏輯并跟蹤支出,為每個(gè)項(xiàng)目設(shè)置預(yù)算。
以下是一個(gè)簡單示例。
1from litellm import completion
2import os
3
4# LiteLLM 使用 OpenAI 模型
5
6os.environ["OPENAI_API_KEY"] = "your-API-key"
7
8response = completion(
9 model="gpt-3.5-turbo",
10 messages=[{ "content": "Hello, how are you?","role": "user"}]
11)
12
13# LiteLLM 使用 Claude 模型
14os.environ["ANTHROPIC_API_KEY"] = "your-API-key"
15
16response = completion(
17 model="claude-2",
18 messages=[{ "content": "Hello, how are you?","role": "user"}]
19)
8. Camel-ai
Camel-ai
- 網(wǎng)址:https://docs.litellm.ai/docs/
- 安裝:pip install camel-ai
Camel-ai 是一個(gè)多智能體框架,它可以構(gòu)建自動(dòng)化任務(wù)、世界模擬與數(shù)據(jù)生成方向的多智能體系統(tǒng)。
Camel-ai 旨在通過研究智能體之間的自主合作以深入了解智能體的交流規(guī)律。
在它的 Github 庫中提供了兩個(gè) ChatGPT 智能體對(duì)話的演示,其中一個(gè)扮演 Python 程序員,另一個(gè)扮演股票交易員,兩個(gè)智能體合作開發(fā)一個(gè)股票交易機(jī)器人,詳見 https://github.com/camel-ai/camel。
另外,這里還有一個(gè)簡單的 Camel 腳本。
1from camel.messages import BaseMessage as bm
2from camel.agents import ChatAgent
3
4sys_msg = bm.make_assistant_message(
5 role_name='stone',
6 cnotallow='you are a curious stone wondering about the universe.')
7
8# 定義智能體
9agent = ChatAgent(
10 system_message=sys_msg,
11 message_window_size=10, # [Optional] the length of chat memory
12 )
13
14# 定義用戶信息
15usr_msg = bm.make_user_message(
16 role_name='prof. Claude Shannon',
17 cnotallow='what is information in your mind?')
18
19# 給智能體發(fā)信息
20response = agent.step(usr_msg)
21
22# 檢查響應(yīng),僅作為演示
23print(response.msgs[0].content)
9. E2B
- 網(wǎng)址:https://e2b.dev/
- 安裝:pip install e2b_code_interpreter
E2B 是一個(gè)為 AI 應(yīng)用提供代碼解釋的工具,利用它可以構(gòu)建 AI 代碼執(zhí)行應(yīng)用、AI 數(shù)據(jù)分析應(yīng)用、AI 代碼導(dǎo)師應(yīng)用及大模型推理應(yīng)用。
E2B 代碼解釋器的 SDK 支持在沙盒中安全運(yùn)行 AI 生成的代碼,這個(gè)沙盒包含一個(gè) Jupyter 服務(wù)器,你可以通過 SDK 進(jìn)行控制。
如果你對(duì)構(gòu)建這類應(yīng)用感興趣,可以訪問以下網(wǎng)址了解更多信息:https://e2b.dev/docs/hello-world/py。
以下是其官網(wǎng)提供的一個(gè)簡單示例。
1from e2b_code_interpreter import CodeInterpreter
2
3with CodeInterpreter() as sandbox:
4 sandbox.notebook.exec_cell("x = 1")
5
6 execution = sandbox.notebook.exec_cell("x+=1; x")
7 print(execution.text) # outputs 2