Install traceAI

pip install traceAI-openai

Set Environment Variables

Set up your environment variables to authenticate with both FutureAGI and OpenAI services. These credentials enable:

  • Secure access to FutureAGI’s observability platform
  • Authentication with OpenAI’s API
  • Encrypted telemetry data transmission
import os
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
os.environ["FI_API_KEY"] = "your-futureagi-api-key"
os.environ["FI_SECRET_KEY"] = "your-futureagi-secret-key"

Define Evaluation Tags

Define evaluation criteria for monitoring LLM responses. Evaluation tags allow you to:

  • Define custom evaluation criteria
  • Set up automated response quality checks
  • Track model performance metrics

Click here here to learn how to configure eval tags for observability.

from fi_instrumentation.fi_types import EvalName, EvalSpanKind, EvalTag, EvalTagType

eval_tags = [
    EvalTag(
        eval_name=EvalName.DETERMINISTIC_EVALS,
        value=EvalSpanKind.TOOL,
        type=EvalTagType.OBSERVATION_SPAN,
        config={
            "multi_choice": False,
            "choices": ["Yes", "No"],
            "rule_prompt": "Evaluate if the function call is correct",
        },
        custom_eval_name="det_eval_openai_1"
    )
]

Register Future AGI Tracer Provider

Set up the trace provider to establish the observability pipeline. The trace provider:

  • Creates a new project in FutureAGI
  • Establishes telemetry data pipelines
  • Configures version tracking
  • Sets up evaluation frameworks
from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType

trace_provider = register(
    endpoint = "https://api.futureagi.com/tracer/observation-span/create_otel_span/",
    project_type=ProjectType.EXPERIMENT,
    project_name="function_calling_demo12",
    project_version_name="v1",
    eval_tags=eval_tags
)

Instrument OpenAI

Instrument the OpenAI client to enable telemetry collection. This step ensures that all interactions with the OpenAI API are tracked and monitored.

from traceai_openai import OpenAIInstrumentor

OpenAIInstrumentor().instrument(tracer_provider=trace_provider)

Define Function to Call OpenAI

Define the function that will be used to call the OpenAI API. This function will be used to call the OpenAI API and return the response.

import openai
from openai.types.chat import ChatCompletionToolMessageParam, ChatCompletionUserMessageParam

def get_weather_info():
    messages = [
        ChatCompletionUserMessageParam(
            role="user",
            content="What's the weather like in San Francisco?"
        )
    ]
    
    client = openai.OpenAI()
    response = client.chat.completions.create(
        model="gpt-4",
        tools=[{
            "type": "function",
            "function": {
                "name": "get_weather",
                "description": "Get weather information for a city",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "city": {
                            "type": "string",
                            "description": "City name, e.g., 'San Francisco'"
                        }
                    },
                    "required": ["city"]
                }
            }
        }],
        messages=messages
    )
    
    message = response.choices[0].message
    if tool_calls := message.tool_calls:
        tool_call_id = tool_calls[0].id
        messages.append(message)
        
        messages.append(
            ChatCompletionToolMessageParam(
                content="72°F and sunny",
                role="tool",
                tool_call_id=tool_call_id
            )
        )
        
        final_response = client.chat.completions.create(
            model="gpt-4",
            messages=messages
        )
        return final_response.choices[0].message.content
    
    return "No function call was made"


if __name__ == "__main__":
try:
    result = get_weather_info()
    print(f"Response: {result}")
except Exception as e:
    print(f"Error: {e}")