先睹為快,9 個(gè) Python 程序員還不知道的 AI 大模型開源庫

AI 大行其道,Python 作為 AI 開發(fā)最炙手可熱的編程語言,也水漲船高,各位作為 Python 開發(fā)者怎么能不了解一下 Python 相關(guān)的開源庫呢?以下我就為大家列出了一些目前還鮮為人知的 AI 開源庫,有幾個(gè)還真不錯(cuò)哦。

1. composio

composio

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)驗(yà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è)操作比較簡單,大家可以看一下示例代碼。

composio add github # Connect your Github - Run this in terminal

運(yùn)行以下 Python 腳本可以使用智能體給 Github 資源庫加星。


from openai import OpenAI
from composio_openai import ComposioToolSet, App, Action

openai_client = OpenAI(
    api_key="{{OPENAIKEY}}"
)

# Initialise the Composio Tool Set

composio_tool_set = ComposioToolSet()

# Get GitHub tools that are pre-configured
actions = composio_tool_set.get_actions(
    actions=[Action.GITHUB_ACTIVITY_STAR_REPO_FOR_AUTHENTICATED_USER]
)

my_task = "Star a repo composiodev/composio on GitHub"

# Setup openai assistant
assistant_instruction = "You are a super intelligent personal assistant"

assistant = openai_client.beta.assistants.create(
    name="Personal Assistant",
    instructions=assistant_instruction,
    model="gpt-4-turbo",
    tools=actions,
)

# create a thread
thread = openai_client.beta.threads.create()

message = openai_client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content=my_task
)

# Execute Agent with integrations
run = openai_client.beta.threads.runs.create(
    thread_id=thread.id,
    assistant_id=assistant.id
)


# Execute Function calls
response_after_tool_calls = composio_tool_set.wait_and_handle_assistant_tool_calls(
    client=openai_client,
    run=run,
    thread=thread,
)

print(response_after_tool_calls)

2. Julep

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ù)。

它還可以用于生產(chǎn)環(huán)境,支持使用 Docker Compose 進(jìn)行生產(chǎn)部署,很快還將支持 K8s。

以下是 Julep 的示例代碼,大家可以簡單了解一下。

from julep import Client
from pprint import pprint
import textwrap
import os

base_url = os.environ.get("JULEP_API_URL")
api_key = os.environ.get("JULEP_API_KEY")

client = Client(api_key=api_key, base_url=base_url)

#create agent
agent = client.agents.create(
    name="Jessica"
    model="gpt-4",
    tools=[]    # Tools defined here
)
#create a user
user = client.users.create(
    name="Anon",
    about="Average nerdy tech bro/girl spending 8 hours a day on a laptop,
)
#create a session
situation_prompt = """You are Jessica. You're a stuck-up Cali teenager. 
You basically complain about everything. You live in Bel-Air, Los Angeles and 
drag yourself to Curtis High School when necessary.
"""
session = client.sessions.create(
    user_id=user.id, agent_id=agent.id, situation=situation_prompt
)
#start a conversation

user_msg = "hey. what do u think of Starbucks?"
response = client.sessions.chat(
    session_id=session.id,
    messages=[
        {
            "role": "user",
            "content": user_msg,
            "name": "Anon",
        }
    ],
    recall=True,
    remember=True,
)

print("\n".join(textwrap.wrap(response.response[0][0].content, width=100)))

3. Aider

Aider

Aider 是一款 AI 結(jié)對編程輔助工具,支持啟動(dòng)新項(xiàng)目、編輯文件,并與現(xiàn)有的 GitHub 資源庫集成。

Aider 能夠調(diào)用 GPT-4o、Claude 3.5 Sonnet、DeepSeek Coder、Llama 70b 等大型語言模型。

它可以進(jìn)行代碼測試、解決 Bug、重構(gòu)代碼,甚至更新文檔,并支持 Python、JavaScript、TypeScript、PHP、HTML、CSS 等主流編程語言。

你可以在代碼編輯器中邊編輯文件邊與 Aider 聊天,讓它提供建議,甚至使用語音編程功能。

  • 安裝:
$ pip install aider-chat

# 進(jìn)入 git 資源庫的目錄
$ cd /to/your/git/repo

# 使用 Claude 3.5 Sonnet
$ export ANTHROPIC_API_KEY=your-key-goes-here
$ aider

# 使用 GPT-4o
$ export OPENAI_API_KEY=your-key-goes-here
$ aider 

4. Haystack

Haystack

Haystack 是構(gòu)建生產(chǎn)級別的大模型應(yīng)用的開源框架,可用于開發(fā)檢索增強(qiáng)生成管道和高級搜索系統(tǒng),能夠智能地處理大規(guī)模文檔集合。

盡管構(gòu)建 AI 管道的框架很多,但如果需要將端到端的搜索管道集成到生產(chǎn)應(yīng)用中,Haystack 是首選。

無論是 RAG、問答系統(tǒng)還是語義搜索,Haystack 的靈活的管道組合方式讓開發(fā)、維護(hù)和部署變得輕松便捷。

使用 Haystack 可以輕松地將排序器、向量存儲(chǔ)和解析器集成到新的或現(xiàn)有的管道中,從而將原型快速轉(zhuǎn)化為生產(chǎn)級的解決方案。

以下是使用 haystack 的一個(gè)小示例。

import os

from haystack import Pipeline, PredefinedPipeline
import urllib.request

os.environ["OPENAI_API_KEY"] = "Your OpenAI API Key"
urllib.request.urlretrieve("https://www.gutenberg.org/cache/epub/7785/pg7785.txt", "davinci.txt")  

indexing_pipeline =  Pipeline.from_template(PredefinedPipeline.INDEXING)
indexing_pipeline.run(data={"sources": ["davinci.txt"]})

rag_pipeline =  Pipeline.from_template(PredefinedPipeline.RAG)

query = "How old was he when he died?"
result = rag_pipeline.run(data={"prompt_builder": {"query":query}, "text_embedder": {"text": query}})
print(result["llm"]["replies"][0])

5. Mem0

Mem0

Mem0 為大型語言模型提供了一個(gè)智能且自我改進(jìn)的長期記憶層,使個(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
pip install fastembed

# 或使用 GPU

pip install fastembed-gpu

FastEmbed 是一個(gè)輕量級的高性能 Python 庫,用于嵌入生成模型。它支持多種流行的文本模型。默認(rèn)的文本嵌入模型是 Flag Embedding,支持 “query” 和 “passage” 前綴的輸入文本。

FastEmbed 不需要 GPU,無需下載 GB 級別的 PyTorch。嵌入生成的時(shí)間一般都很長,導(dǎo)致整個(gè)流程的速度很慢,F(xiàn)astEmbed 使用 ONNX 運(yùn)行時(shí),比 PyTorch 更快,同時(shí)可以利用數(shù)據(jù)并行來處理大型數(shù)據(jù)集。

以下是 FastEmbed 創(chuàng)建文檔嵌入的方法。

from fastembed import TextEmbedding
from typing import List

# 文檔列表示例
documents: List[str] = [
    "This is built to be faster and lighter than other embedding libraries, e.g. Transformers, Sentence-Transformers, etc.",
    "FastEmbed is supported by and maintained by Quadrant."
]

# 觸發(fā)模型下載與初始化
embedding_model = TextEmbedding()
print("The model BAAI/bge-small-en-v1.5 is ready to use.")

embeddings_generator = embedding_model.embed(documents)  # reminder this is a generator
embeddings_list = list(embedding_model.embed(documents))
 # You can also convert the generator to a list, and that to a Numpy array
len(embeddings_list[0]) # Vector of 384 dimensions

7. LiteLLM

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è)簡單示例。

from litellm import completion
import os

# LiteLLM 使用 OpenAI 模型

os.environ["OPENAI_API_KEY"] = "your-API-key"

response = completion(
  model="gpt-3.5-turbo",
  messages=[{ "content": "Hello, how are you?","role": "user"}]
)

# LiteLLM 使用 Claude 模型
os.environ["ANTHROPIC_API_KEY"] = "your-API-key"

response = completion(
  model="claude-2",
  messages=[{ "content": "Hello, how are you?","role": "user"}]
)

8. Camel-ai

Camel-ai

Camel-ai 是一個(gè)多智能體框架,它可以構(gòu)建自動(dòng)化任務(wù)、世界模擬與數(shù)據(jù)生成方向的多智能體系統(tǒng)。

Camel-ai 旨在通過研究智能體之間的自主合作以深入了解智能體的交流規(guī)律。在它的 Github 庫中提供了兩個(gè) ChatGPT 智能體對話的演示,其中一個(gè)扮演 Python 程序員,另一個(gè)扮演股票交易員,兩個(gè)智能體合作開發(fā)一個(gè)股票交易機(jī)器人,詳見 https://github.com/camel-ai/camel。

另外,這里還有一個(gè)簡單的 Camel 腳本。

from camel.messages import BaseMessage as bm
from camel.agents import ChatAgent

sys_msg = bm.make_assistant_message(
    role_name='stone',
    content='you are a curious stone wondering about the universe.')

# 定義智能體 
agent = ChatAgent(
    system_message=sys_msg,
    message_window_size=10,    # [Optional] the length of chat memory
    )

# 定義用戶信息
usr_msg = bm.make_user_message(
    role_name='prof. Claude Shannon',
    content='what is information in your mind?')

# 給智能體發(fā)信息
response = agent.step(usr_msg)

# 檢查響應(yīng),僅作為演示
print(response.msgs[0].content)

9. E2B

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)行控制。

如果你對構(gòu)建這類應(yīng)用感興趣,可以訪問以下網(wǎng)址了解更多信息:https://e2b.dev/docs/hello-world/py

以下是其官網(wǎng)提供的一個(gè)簡單示例。

from e2b_code_interpreter import CodeInterpreter

with CodeInterpreter() as sandbox:
    sandbox.notebook.exec_cell("x = 1")

    execution = sandbox.notebook.exec_cell("x+=1; x")
    print(execution.text)  # outputs 2
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容