
測試,評估和監視您的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} }
}