Skip to content

feat: add openai-agents tracing processor #166

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def main():
"url": "https://api.github.com/repos/chainlit/chainlit",
"mime": "application/json",
"metadata": {"test": "test"},
}
},
)

print(attachment.to_dict())
Expand Down
13 changes: 5 additions & 8 deletions examples/langchain_toolcall.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
from literalai import LiteralClient

from langchain_openai import ChatOpenAI # type: ignore
from dotenv import load_dotenv
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain.agents.agent import BaseSingleActionAgent
from langchain_community.tools.tavily_search import TavilySearchResults

from langchain.agents import create_tool_calling_agent
from langchain.agents import AgentExecutor
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.runnables.config import RunnableConfig
from langchain.agents.agent import BaseSingleActionAgent
from langchain_openai import ChatOpenAI # type: ignore

from dotenv import load_dotenv
from literalai import LiteralClient

# Add OPENAI_API_KEY and TAVILY_API_KEY for this example.
load_dotenv()
Expand Down
5 changes: 2 additions & 3 deletions examples/langchain_variable.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model
from literalai import LiteralClient


from dotenv import load_dotenv
from literalai import LiteralClient

load_dotenv()

Expand Down
7 changes: 4 additions & 3 deletions examples/llamaindex.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from literalai import LiteralClient
from llama_index.core import Document, VectorStoreIndex
from dotenv import load_dotenv
from llama_index.core import Document, VectorStoreIndex

from literalai import LiteralClient

load_dotenv()

Expand All @@ -14,7 +15,7 @@
questions = [
"Tell me about LLMs",
"How do you fine-tune a neural network ?",
"What is RAG ?"
"What is RAG ?",
]

# No context, create a Thread (it will be named after the first user query)
Expand Down
13 changes: 5 additions & 8 deletions examples/llamaindex_workflow.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import asyncio
from llama_index.core.workflow import (
Event,
StartEvent,
StopEvent,
Workflow,
step,
)

from llama_index.core.workflow import Event, StartEvent, StopEvent, Workflow, step
from llama_index.llms.openai import OpenAI

from literalai.client import LiteralClient

lai_client = LiteralClient()
Expand All @@ -16,7 +12,8 @@
class JokeEvent(Event):
joke: str

class RewriteJoke(Event):

class RewriteJoke(Event):
joke: str


Expand Down
2 changes: 1 addition & 1 deletion examples/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_completion(welcome_message, text):
{
"role": "system",
"content": "Tell an inspiring quote to the user, mentioning their name. Be extremely supportive while "
"keeping it short. Write one sentence per line.",
"keeping it short. Write one sentence per line.",
},
{
"role": "assistant",
Expand Down
7 changes: 3 additions & 4 deletions examples/multimodal.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import base64
import requests # type: ignore
import time

from literalai import LiteralClient
from openai import OpenAI

import requests # type: ignore
from dotenv import load_dotenv
from openai import OpenAI

from literalai import LiteralClient

load_dotenv()

Expand Down
27 changes: 27 additions & 0 deletions examples/openai_agents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import asyncio

from agents import Agent, Runner, set_trace_processors, trace
from dotenv import load_dotenv

from literalai import LiteralClient

load_dotenv()

client = LiteralClient()


async def main():
agent = Agent(name="Joke generator", instructions="Tell funny jokes.")

with trace("Joke workflow"):
first_result = await Runner.run(agent, "Tell me a joke")
second_result = await Runner.run(
agent, f"Rate this joke: {first_result.final_output}"
)
print(f"Joke: {first_result.final_output}")
print(f"Rating: {second_result.final_output}")


if __name__ == "__main__":
set_trace_processors([client.openai_agents_tracing_processor()])
asyncio.run(main())
2 changes: 1 addition & 1 deletion literalai/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from literalai.api.synchronous import LiteralAPI
from literalai.api.asynchronous import AsyncLiteralAPI
from literalai.api.synchronous import LiteralAPI

__all__ = ["LiteralAPI", "AsyncLiteralAPI"]
3 changes: 1 addition & 2 deletions literalai/api/helpers/attachment_helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import mimetypes
from typing import Dict, Optional, TypedDict, Union

from literalai.observability.step import Attachment

from literalai.api.helpers import gql
from literalai.observability.step import Attachment


def create_attachment_helper(
Expand Down
6 changes: 2 additions & 4 deletions literalai/api/helpers/prompt_helpers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import logging
from typing import TYPE_CHECKING, Optional, TypedDict, Callable
from typing import TYPE_CHECKING, Callable, Optional, TypedDict

from literalai.cache.prompt_helpers import put_prompt
from literalai.observability.generation import GenerationMessage
from literalai.prompt_engineering.prompt import Prompt, ProviderSettings

from literalai.cache.prompt_helpers import put_prompt

if TYPE_CHECKING:
from literalai.api import LiteralAPI
from literalai.cache.shared_cache import SharedCache

from literalai.api.helpers import gql


logger = logging.getLogger(__name__)


Expand Down
7 changes: 3 additions & 4 deletions literalai/api/helpers/score_helpers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import math
from typing import Any, Dict, List, Optional, TypedDict

from literalai.observability.filter import scores_filters, scores_order_by
from literalai.my_types import PaginatedResponse
from literalai.observability.step import ScoreType, ScoreDict, Score

from literalai.api.helpers import gql
from literalai.my_types import PaginatedResponse
from literalai.observability.filter import scores_filters, scores_order_by
from literalai.observability.step import Score, ScoreDict, ScoreType


def get_scores_helper(
Expand Down
5 changes: 2 additions & 3 deletions literalai/api/helpers/step_helpers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from typing import Any, Dict, List, Optional, Union

from literalai.observability.filter import steps_filters, steps_order_by
from literalai.api.helpers import gql
from literalai.my_types import PaginatedResponse
from literalai.observability.filter import steps_filters, steps_order_by
from literalai.observability.step import Step, StepDict, StepType

from literalai.api.helpers import gql


def create_step_helper(
thread_id: Optional[str] = None,
Expand Down
5 changes: 2 additions & 3 deletions literalai/api/helpers/thread_helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import Any, Dict, List, Optional

from literalai.observability.filter import threads_filters, threads_order_by
from literalai.api.helpers import gql
from literalai.my_types import PaginatedResponse
from literalai.observability.filter import threads_filters, threads_order_by
from literalai.observability.step import StepType
from literalai.observability.thread import Thread

from literalai.api.helpers import gql


def get_threads_helper(
first: Optional[int] = None,
Expand Down
5 changes: 2 additions & 3 deletions literalai/api/helpers/user_helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Any, Dict, Optional

from literalai.observability.filter import users_filters
from literalai.my_types import PaginatedResponse, User

from literalai.api.helpers import gql
from literalai.my_types import PaginatedResponse, User
from literalai.observability.filter import users_filters


def get_users_helper(
Expand Down
2 changes: 1 addition & 1 deletion literalai/cache/prompt_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from literalai.prompt_engineering.prompt import Prompt
from literalai.cache.shared_cache import SharedCache
from literalai.prompt_engineering.prompt import Prompt


def put_prompt(cache: SharedCache, prompt: Prompt):
Expand Down
2 changes: 2 additions & 0 deletions literalai/callback/langchain_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ def _build_llm_settings(
)
model_keys = ["azure_deployment", "deployment_name", "model", "model_name"]
model = next((settings[k] for k in model_keys if k in settings), None)
if isinstance(model, str):
model = model.replace("models/", "")
tools = None
if "functions" in settings:
tools = [
Expand Down
Loading