Setup

Set PAREA_API_KEY as an environment variable, or in a .env file

export PAREA_API_KEY=<your API key>

Install the Parea Package

pip install parea-ai

Automatic logging of any OpenAI request

By importing Parea from parea and initializing it with your API key, you can log any OpenAI request with a single line of code.

import os
from datetime import datetime

import openai
from dotenv import load_dotenv

from parea import Parea

load_dotenv()

# openai calls will be automatically logged
openai.api_key = os.getenv("OPENAI_API_KEY")

p = Parea(api_key=os.getenv("PAREA_API_KEY"))


def argument_generator(query: str, additional_description: str = "", date=datetime.now()) -> str:
    return (
        openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0613",
            messages=[
                {"role": "system", "content": f"""You are a debater making an argument on a topic. {additional_description}. The current time is {date}"""},
                {"role": "user", "content": f"""The discussion topic is {query}"""},
            ],
            temperature=0.0,
        )
        .choices[0]
        .message["content"]
    )


result = argument_generator(
    "Whether caffeine is good for you.",
    additional_description="Provide a concise, few sentence argument on why caffeine is good for you.",
)
print(result)

Visualization in the dashboard

After the API calls were captured, we can view their trace in the dashboard (top image) and step through them in the detailed view (bottom image).

Dashboard overview Dashboard detailed

Capture & associate feedback with the completion(s)

Continuing from the previous example, we can capture feedback associated with the previous completion(s) by using the record_feedback method.

from parea.schemas.models import FeedbackRequest
from parea.utils.trace_utils import get_current_trace_id

trace_id = get_current_trace_id()
print(f"trace_id: {trace_id}")
# 0.0 (bad) to 1.0 (good)
p.record_feedback(FeedbackRequest(trace_id=trace_id, score=0.7))