
测试,评估和监视您的AI应用程序
? Twitter/X • ?不和谐• parea ai • ?文档
Parea AI提供了一个SDK来评估和监视您的AI应用程序。您可以在下面看到快速入门:
我们的完整文档在这里。
pip install -U parea-ai或与Poetry安装
poetry add parea-ai测试您的AI应用意味着通过数据集执行并使用评估功能进行评分。这是通过定义和运行实验在Parea中完成的。在下面,您可以看到如何使用Levenshtein距离度量标准测试如何测试问候机器人的示例。
from parea import Parea , trace
from parea . evals . general import levenshtein
p = Parea ( api_key = "<<PAREA_API_KEY>>" ) # replace with Parea AI API key
# use the trace decorator to score the output with the Levenshtein distance
@ trace ( eval_funcs = [ levenshtein ])
def greeting ( name : str ) -> str :
return f"Hello { name } "
data = [
{ "name" : "Foo" , "target" : "Hi Foo" },
{ "name" : "Bar" , "target" : "Hello Bar" },
]
p . experiment (
name = "Greeting" ,
data = data ,
func = greeting ,
). run ()在上面的片段中,我们使用trace装饰器捕获功能的所有输入和输出。该装饰器还可以通过在后台执行levenshtein评估来评分输出。然后,我们通过p.experiment定义了一个实验,以评估数据集(此处词典列表)上的功能( greeting )。最后,调用run将执行实验,并为数据集的任何示例创建输出,分数和痕迹的报告。您可以在此处找到指向执行实验的链接。 (Todo:填充实验)
阅读有关如何在我们的文档中编写,运行和分析实验的更多信息。
通过包装各自的客户,您可以自动将所有LLM调用访问到OpenAI&Anthropic。此外,使用trace Decorator,您可以创建LLM应用程序的层次结构轨迹,以与RAG管道的检索步骤相关联的LLM调用。您可以在此处看到完整的可观察性文档,以及我们在这里的Langchain,DSPY,Litellm等人的集成。
要自动记录任何OpenAI调用,您可以使用wrap_openai_client方法与parea客户端包装OpenAI客户端。
from openai import OpenAI
from parea import Parea
client = OpenAI ( api_key = "OPENAI_API_KEY" )
# All you need to do is add these two lines
p = Parea ( api_key = "PAREA_API_KEY" ) # replace with your API key
p . wrap_openai_client ( client )
response = client . chat . completions . create (
model = "gpt-4o" ,
messages = [
{
"role" : "user" ,
"content" : "Write a Hello World program in Python using FastAPI." ,
}
],
)
print ( response . choices [ 0 ]. message . content )要自动记录任何人类呼叫,您可以使用wrap_anthropic_client方法与Parea客户端一起包裹人类客户端。
import anthropic
from parea import Parea
p = Parea ( api_key = "PAREA_API_KEY" ) # replace with your API key
client = anthropic . Anthropic ()
p . wrap_anthropic_client ( client )
message = client . messages . create (
model = "claude-3-opus-20240229" ,
max_tokens = 1024 ,
messages = [
{
"role" : "user" ,
"content" : "Write a Hello World program in Python using FastAPI." ,
}
],
)
print ( message . content [ 0 ]. text )通过使用trace Decorator,您可以创建LLM应用程序的层次结构痕迹。
from openai import OpenAI
from parea import Parea , trace
client = OpenAI ( api_key = "OPENAI_API_KEY" ) # replace with your API key
p = Parea ( api_key = "PAREA_API_KEY" ) # replace with your API key
p . wrap_openai_client ( client )
# We generally recommend creating a helper function to make LLM API calls.
def llm ( messages : list [ dict [ str , str ]]) -> str :
response = client . chat . completions . create ( model = "gpt-4o" , messages = messages )
return response . choices [ 0 ]. message . content
# This will give the Span the name of the function.
# Without the decorator the default name for all LLM call logs is `llm-openai`
@ trace
def hello_world ( lang : str , framework : str ):
return llm ([{ "role" : "user" , "content" : f"Write a Hello World program in { lang } using { framework } ." }])
@ trace
def critique_code ( code : str ):
return llm ([{ "role" : "user" , "content" : f"How can we improve this code: n { code } " }])
# Our top level function is called chain. By adding the trace decorator here,
# all sub-functions will automatically be logged and associated with this trace.
# Notice, you can also add metadata to the trace, we'll revisit this functionality later.
@ trace ( metadata = { "purpose" : "example" }, end_user_identifier = "John Doe" )
def chain ( lang : str , framework : str ) -> str :
return critique_code ( hello_world ( lang , framework ))
print ( chain ( "Python" , "FastAPI" ))部署的提示可以与产品经理和主题专家等非工程师合作。用户可以在Parea的操场上迭代,完善和测试提示。修补后,您可以部署该提示,这意味着它是通过API端点暴露的,以将其集成到您的应用程序中。在这里查看我们的完整文档。
from parea import Parea
from parea . schemas . models import Completion , UseDeployedPrompt , CompletionResponse , UseDeployedPromptResponse
p = Parea ( api_key = "<PAREA_API_KEY>" )
# You will find this deployment_id in the Parea dashboard
deployment_id = '<DEPLOYMENT_ID>'
# Assuming your deployed prompt's message is:
# {"role": "user", "content": "Write a hello world program using {{x}} and the {{y}} framework."}
inputs = { "x" : "Golang" , "y" : "Fiber" }
# You can easily unpack a dictionary into an attrs class
test_completion = Completion (
** {
"deployment_id" : deployment_id ,
"llm_inputs" : inputs ,
"metadata" : { "purpose" : "testing" }
}
)
# By passing in my inputs, in addition to the raw message with unfilled variables {{x}} and {{y}},
# you we will also get the filled-in prompt:
# {"role": "user", "content": "Write a hello world program using Golang and the Fiber framework."}
test_get_prompt = UseDeployedPrompt ( deployment_id = deployment_id , llm_inputs = inputs )
def main ():
completion_response : CompletionResponse = p . completion ( data = test_completion )
print ( completion_response )
deployed_prompt : UseDeployedPromptResponse = p . get_prompt ( data = test_get_prompt )
print ( " n n " )
print ( deployed_prompt )该项目是根据Apache Software License 2.0许可证的条款许可的。有关更多详细信息,请参见许可证。
@misc { parea-sdk ,
author = { joel-parea-ai,joschkabraun } ,
title = { Parea python sdk } ,
year = { 2023 } ,
publisher = { GitHub } ,
journal = { GitHub repository } ,
howpublished = { url{https://github.com/parea-ai/parea-sdk} }
}