clientai
0.4.4
Clientai是一個Python軟件包,它為構建AI應用程序提供了一個統一的框架,從直接提供商的交互到透明的LLM驅動代理,並對OpenAI,Replicate,Groq和Ollama無縫支持。
文檔:igorbenav.github.io/clientai/
要與所有提供商一起安裝客戶端,請運行:
pip install " clientai[all] "或者,如果您只想安裝特定的提供商:
pip install " clientai[openai] " # For OpenAI support
pip install " clientai[replicate] " # For Replicate support
pip install " clientai[ollama] " # For Ollama support
pip install " clientai[groq] " # For Groq support from clientai import ClientAI
# Initialize with OpenAI
client = ClientAI ( 'openai' , api_key = "your-openai-key" )
# Generate text
response = client . generate_text (
"Tell me a joke" ,
model = "gpt-3.5-turbo" ,
)
print ( response )
# Chat functionality
messages = [
{ "role" : "user" , "content" : "What is the capital of France?" },
{ "role" : "assistant" , "content" : "Paris." },
{ "role" : "user" , "content" : "What is its population?" }
]
response = client . chat (
messages ,
model = "gpt-3.5-turbo" ,
)
print ( response ) from clientai import client
from clientai . agent import create_agent , tool
@ tool ( name = "calculator" )
def calculate_average ( numbers : list [ float ]) -> float :
"""Calculate the arithmetic mean of a list of numbers."""
return sum ( numbers ) / len ( numbers )
analyzer = create_agent (
client = client ( "groq" , api_key = "your-groq-key" ),
role = "analyzer" ,
system_prompt = "You are a helpful data analysis assistant." ,
model = "llama-3.2-3b-preview" ,
tools = [ calculate_average ]
)
result = analyzer . run ( "Calculate the average of these numbers: [1000, 1200, 950, 1100]" )
print ( result )為了保證的輸出結構和類型安全:
from clientai . agent import Agent , think
from pydantic import BaseModel , Field
from typing import List
class Analysis ( BaseModel ):
summary : str = Field ( min_length = 10 )
key_points : List [ str ] = Field ( min_items = 1 )
sentiment : str = Field ( pattern = "^(positive|negative|neutral)$" )
class DataAnalyzer ( Agent ):
@ think (
name = "analyze" ,
json_output = True , # Enable JSON formatting
)
def analyze_data ( self , data : str ) -> Analysis : # Enable validation
"""Analyze data with validated output structure."""
return """
Analyze this data and return a JSON with:
- summary: at least 10 characters
- key_points: non-empty list
- sentiment: positive, negative, or neutral
Data: {data}
"""
# Initialize and use
analyzer = DataAnalyzer ( client = client , default_model = "gpt-4" )
result = analyzer . run ( "Sales increased by 25% this quarter" )
print ( f"Sentiment: { result . sentiment } " )
print ( f"Key Points: { result . key_points } " )請參閱我們的文檔以獲取更多示例,包括:
客戶端代理模塊建立在四個核心原則上:
以及時為中心的設計:提示是明確的,可辯論的和透明的。您看到的是發送給模型的內容。
首先自定義:每個組件都被設計為擴展或覆蓋。創建自定義步驟,工具選擇器或全新的工作流程模式。
零鎖定:從高級組件開始,然後根據需要下降至較低的水平。你可以:
Agent歡迎捐款!有關更多信息,請參閱我們的貢獻指南。
該項目是根據MIT許可證獲得許可的 - 有關詳細信息,請參見許可證文件。
Igor Magalhaes - @igormagalhaesr - [email protected] github.com/igorbenav