Skip to content

Clement/eng 2181 otel exporter #157

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 9, 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
9 changes: 5 additions & 4 deletions examples/langchain_toolcall.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
tools = [search]

lai_client = LiteralClient()
lai_client.initialize()
lai_prompt = lai_client.api.get_or_create_prompt(
name="LC Agent",
settings={
Expand All @@ -37,13 +38,13 @@
{"role": "assistant", "content": "{{agent_scratchpad}}"},
],
)
prompt = lai_prompt.to_langchain_chat_prompt_template()
prompt = lai_prompt.to_langchain_chat_prompt_template(
additional_messages=[("placeholder", "{agent_scratchpad}")],
)

agent: BaseSingleActionAgent = create_tool_calling_agent(model, tools, prompt) # type: ignore
agent_executor = AgentExecutor(agent=agent, tools=tools)

cb = lai_client.langchain_callback()

# Replace with ainvoke for asynchronous execution.
agent_executor.invoke(
{
Expand All @@ -56,5 +57,5 @@
],
"input": "whats the weather in sf?",
},
config=RunnableConfig(callbacks=[cb], run_name="Weather SF"),
config=RunnableConfig(run_name="Weather SF"),
)
8 changes: 5 additions & 3 deletions examples/langchain_variable.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from langchain.chat_models import init_chat_model
from literalai import LiteralClient
from langchain.schema.runnable.config import RunnableConfig


from dotenv import load_dotenv

load_dotenv()

lai = LiteralClient()
lai.initialize()

prompt = lai.api.get_or_create_prompt(
name="user intent",
Expand All @@ -29,13 +30,14 @@
input_messages = messages.format_messages(
user_message="The screen is cracked, there are scratches on the surface, and a component is missing."
)
cb = lai.langchain_callback()

# Returns a langchain_openai.ChatOpenAI instance.
gpt_4o = init_chat_model( # type: ignore
model_provider=prompt.provider,
**prompt.settings,
)
print(gpt_4o.invoke(input_messages, config=RunnableConfig(callbacks=[cb])))

lai.set_properties(prompt=prompt)
print(gpt_4o.invoke(input_messages))

lai.flush_and_stop()
55 changes: 55 additions & 0 deletions examples/llamaindex_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import asyncio
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()
lai_client.initialize()


class JokeEvent(Event):
joke: str

class RewriteJoke(Event):
joke: str


class JokeFlow(Workflow):
llm = OpenAI()

@step()
async def generate_joke(self, ev: StartEvent) -> JokeEvent:
topic = ev.topic

prompt = f"Write your best joke about {topic}."
response = await self.llm.acomplete(prompt)
return JokeEvent(joke=str(response))

@step()
async def return_joke(self, ev: JokeEvent) -> RewriteJoke:
return RewriteJoke(joke=ev.joke + "What is funny?")

@step()
async def critique_joke(self, ev: RewriteJoke) -> StopEvent:
joke = ev.joke

prompt = f"Give a thorough analysis and critique of the following joke: {joke}"
response = await self.llm.acomplete(prompt)
return StopEvent(result=str(response))


@lai_client.thread(name="JokeFlow")
async def main():
w = JokeFlow(timeout=60, verbose=False)
result = await w.run(topic="pirates")
print(str(result))


if __name__ == "__main__":
asyncio.run(main())
83 changes: 83 additions & 0 deletions examples/multimodal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import base64
import requests # type: ignore
import time

from literalai import LiteralClient
from openai import OpenAI

from dotenv import load_dotenv


load_dotenv()

openai_client = OpenAI()

literalai_client = LiteralClient()
literalai_client.initialize()


def encode_image(url):
return base64.b64encode(requests.get(url).content)


@literalai_client.step(type="run")
def generate_answer(user_query, image_url):
literalai_client.set_properties(
name="foobar",
metadata={"foo": "bar"},
tags=["foo", "bar"],
)
completion = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": user_query},
{
"type": "image_url",
"image_url": {"url": image_url},
},
],
},
],
max_tokens=300,
)
return completion.choices[0].message.content


def main():
with literalai_client.thread(name="Meal Analyzer") as thread:
welcome_message = (
"Welcome to the meal analyzer, please upload an image of your plate!"
)
literalai_client.message(
content=welcome_message, type="assistant_message", name="My Assistant"
)

user_query = "Is this a healthy meal?"
user_image = "https://www.eatthis.com/wp-content/uploads/sites/4/2021/05/healthy-plate.jpg"
user_step = literalai_client.message(
content=user_query, type="user_message", name="User"
)

time.sleep(1) # to make sure the user step has arrived at Literal AI

literalai_client.api.create_attachment(
thread_id=thread.id,
step_id=user_step.id,
name="meal_image",
content=encode_image(user_image),
)

answer = generate_answer(user_query=user_query, image_url=user_image)
literalai_client.message(
content=answer, type="assistant_message", name="My Assistant"
)


main()
# Network requests by the SDK are performed asynchronously.
# Invoke flush_and_stop() to guarantee the completion of all requests prior to the process termination.
# WARNING: If you run a continuous server, you should not use this method.
literalai_client.flush_and_stop()
2 changes: 1 addition & 1 deletion examples/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


sdk = LiteralClient(batch_size=2)
sdk.instrument_openai()
sdk.initialize()


@sdk.thread
Expand Down
40 changes: 40 additions & 0 deletions literalai/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import json
import os
from traceloop.sdk import Traceloop
from typing import Any, Dict, List, Optional, Union
from typing_extensions import deprecated
import io
from contextlib import redirect_stdout

from literalai.api import AsyncLiteralAPI, LiteralAPI
from literalai.callback.langchain_callback import get_langchain_callback
Expand All @@ -10,6 +15,7 @@
experiment_item_run_decorator,
)
from literalai.event_processor import EventProcessor
from literalai.exporter import LoggingSpanExporter
from literalai.instrumentation.mistralai import instrument_mistralai
from literalai.instrumentation.openai import instrument_openai
from literalai.my_types import Environment
Expand All @@ -23,6 +29,7 @@
step_decorator,
)
from literalai.observability.thread import ThreadContextManager, thread_decorator
from literalai.prompt_engineering.prompt import Prompt
from literalai.requirements import check_all_requirements


Expand Down Expand Up @@ -92,18 +99,21 @@ def to_sync(self) -> "LiteralClient":
else:
return self # type: ignore

@deprecated("Use Literal.initialize instead")
def instrument_openai(self):
"""
Instruments the OpenAI SDK so that all LLM calls are logged to Literal AI.
"""
instrument_openai(self.to_sync())

@deprecated("Use Literal.initialize instead")
def instrument_mistralai(self):
"""
Instruments the Mistral AI SDK so that all LLM calls are logged to Literal AI.
"""
instrument_mistralai(self.to_sync())

@deprecated("Use Literal.initialize instead")
def instrument_llamaindex(self):
"""
Instruments the Llama Index framework so that all RAG & LLM calls are logged to Literal AI.
Expand All @@ -119,6 +129,13 @@ def instrument_llamaindex(self):

instrument_llamaindex(self.to_sync())

def initialize(self):
with redirect_stdout(io.StringIO()):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Traceloop is verbose so this is aimed to prevent explicit print statement.

Traceloop.init(
disable_batch=True,
exporter=LoggingSpanExporter(event_processor=self.event_processor),
)

def langchain_callback(
self,
to_ignore: Optional[List[str]] = None,
Expand Down Expand Up @@ -352,6 +369,29 @@ def get_current_root_run(self):
"""
return active_root_run_var.get()

def set_properties(
self,
name: Optional[str] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
prompt: Optional[Prompt] = None,
):
thread = active_thread_var.get()
root_run = active_root_run_var.get()
parent = active_steps_var.get()[-1] if active_steps_var.get() else None

Traceloop.set_association_properties(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an upsert so we need to maintain explicit calls.

{
"literal.thread_id": str(thread.id) if thread else "None",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can only store strings

"literal.parent_id": str(parent.id) if parent else "None",
"literal.root_run_id": str(root_run.id) if root_run else "None",
"literal.name": str(name) if name else "None",
"literal.tags": json.dumps(tags) if tags else "None",
"literal.metadata": json.dumps(metadata) if metadata else "None",
"literal.prompt": json.dumps(prompt.to_dict()) if prompt else "None",
}
)

def reset_context(self):
"""
Resets the context, forgetting active steps & setting current thread to None.
Expand Down
Loading
Loading