偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

幾個開發(fā)大模型應(yīng)用常用的 Python 庫

開發(fā) 人工智能
本文介紹的以下這些 Python 庫可以用于構(gòu)建可靠、可擴展和高效的AI應(yīng)用程序,我們一起來看。

一、應(yīng)用層開發(fā)

1. FastAPI

FastAPI是構(gòu)建API的優(yōu)選。顧名思義,它快速、簡單,并能與Pydantic完美集成,實現(xiàn)無縫數(shù)據(jù)驗證。

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float

@app.post("/items/")
async def create_item(item: Item):
    return {"name": item.name, "price": item.price}

2. Jinja

Jinja是一個模板引擎,用于創(chuàng)建動態(tài)提示,它簡單而強大,在管理復(fù)雜的提示邏輯方面起著關(guān)鍵作用。

from jinja2 import Template


template = Template("Hello {{ name }}!")
print(template.render(name="Raj"))

二、任務(wù)調(diào)度

有時候系統(tǒng)需要處理繁重的工作,Celery庫可以幫助跨多個線程或機器分配任務(wù)。即使在要求苛刻的操作中,也能保持應(yīng)用程序的響應(yīng)速度。

from celery import Celery

app = Celery('tasks', broker='redis://localhost:6379/0')

@app.task
def add(x, y):
    return x + y

三、數(shù)據(jù)管理

數(shù)據(jù)是AI的基礎(chǔ),目前比較常用的兩種數(shù)據(jù)庫:PostgreSQL和MongoDB,分別對應(yīng)著結(jié)構(gòu)化和非結(jié)構(gòu)化數(shù)據(jù)。

1. 連接

一般使用psycopg2管理Postgre SQL,使用PyMongo管理MongoDB。 

import psycopg2

conn = psycopg2.connect(
    dbname="example", user="user", password="password", host="localhost")
cursor = conn.cursor()
cursor.execute("SELECT * FROM table")
rows = cursor.fetchall()
print(rows)

2. 簡化數(shù)據(jù)操作

SQLAlchemy可以實現(xiàn)Python API管理數(shù)據(jù)庫操作,相比SQL,這更干凈,更高效。

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)

engine = create_engine('sqlite:///example.db')
Base.metadata.create_all(engine)

四、API集成

將AI大模型集成到應(yīng)用程序是最重要的步驟,實際上我們的應(yīng)用就像是LLM的客戶端,OpenAI、Anthropic和Google API 這些庫都是AI應(yīng)用集成常用的。

import openai

openai.api_key = "your-api-key"
response = openai.Completion.create(
    engine="text-davinci-003",
    prompt="Write a Python function to add two numbers.",
    max_tokens=100
)
print(response.choices[0].text.strip())

五、數(shù)據(jù)處理

1. 結(jié)構(gòu)化

如果應(yīng)用想從LLM中得到可靠的結(jié)構(gòu)化輸出,那么Instructor庫是一個很好的選擇。它可與各種模型配合使用,并且提供了高級數(shù)據(jù)驗證功能。

from instructor import Instructor

instructor = Instructor(api_key="your-api-key")
response = instructor.get_response(prompt="What is the capital of France?", model="text-davinci-003")
print(response)

2. LangChain和LlamaIndex:

這些框架簡化了使用大型語言模型的工作。在一些場景下它們可以容簡化提示管理和嵌入之類的復(fù)雜任務(wù),使其易于入門。

from langchain.chains import LLMChain
from langchain.llms import OpenAI

llm = OpenAI(temperature=0.7)
chain = LLMChain(llm=llm)
response = chain.run("What is 2 + 2?")
print(response)

3. 向量數(shù)據(jù)庫

許多AI應(yīng)用程序,例如RAG,依賴于存儲上下文,以便于后續(xù)進行檢索。

往往會使用到向量數(shù)據(jù)庫存儲向量以及執(zhí)行相似性搜索,例如:Pinecone、Weaviate和PGVector。

import pinecone

pinecone.init(api_key="your-api-key", environment="us-west1-gcp")
index = pinecone.Index("example-index")
index.upsert([("id1", [0.1, 0.2, 0.3])])
result = index.query([0.1, 0.2, 0.3], top_k=1)
print(result)

4. DSPy

DSPy有助于自動優(yōu)化提示,在微調(diào)AI響應(yīng)時節(jié)省大量時間以及猜測。

from dsp import PromptOptimizer

optimizer = PromptOptimizer()
optimized_prompt = optimizer.optimize("Write a poem about space.")
print(optimized_prompt)

5. PyMuPDF和PyPDF2

如果AI應(yīng)用需要從PDF或文檔中提取數(shù)據(jù),這些庫是靠譜的選擇。

import fitz

doc = fitz.open("example.pdf")
for page in doc:
    print(page.get_text())

6. Pydantic

人工智能項目經(jīng)常需要處理混亂、不可預(yù)測的數(shù)據(jù),Pydantic優(yōu)雅地可以清理、組織數(shù)據(jù)。

from pydantic import BaseModel

class User(BaseModel):
    name: str
    age: int
    email: str

user = User(name="rose", age=30, email="rose@example.com")
print(user.dict())

六、跟蹤觀察

大模型應(yīng)用的開發(fā)不是一觸而就,開發(fā)只是第一步,在開發(fā)完之后還需要持續(xù)監(jiān)控它的執(zhí)行狀況并對其優(yōu)化。

Langsmith和Langsmith,這兩個平臺適合用于跟蹤LLM調(diào)用情況,包括延遲、成本和輸出等關(guān)鍵數(shù)據(jù)。

from langfuse import LangFuse

langfuse = LangFuse(api_key="your-api-key")
langfuse.log_interaction(prompt="What is 5 + 5?", response="10", latency=0.3)

以上這些Python庫可以用于構(gòu)建可靠、可擴展和高效的AI應(yīng)用程序。

責任編輯:趙寧寧 來源: andflow
相關(guān)推薦

2021-10-18 06:54:47

Go開源庫業(yè)務(wù)

2011-05-13 16:30:25

PHP

2023-12-07 11:01:27

Python常用模塊內(nèi)置模塊

2025-04-02 01:25:00

2024-05-10 06:59:06

2021-01-13 15:13:07

Python開發(fā) 工具

2009-03-23 10:25:22

JavaOracle應(yīng)用開發(fā)

2020-10-29 10:59:44

Python開發(fā)數(shù)據(jù)

2023-11-28 11:22:51

Pythonitertools庫工具

2023-04-10 15:47:42

PythonGUI 庫開發(fā)

2010-07-14 15:52:28

Telnet命令

2023-09-13 18:39:13

大模型開發(fā)棧框架

2010-02-24 13:45:40

Python開發(fā)人員

2023-11-10 10:39:58

2024-06-06 14:19:36

模型應(yīng)用開發(fā)

2023-11-01 07:34:04

大語言模型應(yīng)用協(xié)議識別

2025-03-06 07:28:31

DeepSeek大模型人工智能

2010-03-11 16:42:31

Python語言開發(fā)

2024-03-07 10:09:42

向量數(shù)據(jù)庫
點贊
收藏

51CTO技術(shù)棧公眾號