-
Notifications
You must be signed in to change notification settings - Fork 10
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
975359d
refactor: deprecate instrumentation methods
clementsirieix ae005e3
refactor: add basic support
clementsirieix 70c6e8a
refactor: allow send llm query
clementsirieix 95cdb03
refactor: rework method
clementsirieix b63e912
refactor: address missing issues
clementsirieix e09d261
fix: fix requirements
clementsirieix 1cdace9
fix: fix types
clementsirieix ff7f64a
feat: added workflow test
clementsirieix 8178a27
fix: fix types
clementsirieix f381baf
refactor: update todos
clementsirieix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
|
||
|
||
sdk = LiteralClient(batch_size=2) | ||
sdk.instrument_openai() | ||
sdk.initialize() | ||
|
||
|
||
@sdk.thread | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
||
|
@@ -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. | ||
|
@@ -119,6 +129,13 @@ def instrument_llamaindex(self): | |
|
||
instrument_llamaindex(self.to_sync()) | ||
|
||
def initialize(self): | ||
with redirect_stdout(io.StringIO()): | ||
Traceloop.init( | ||
disable_batch=True, | ||
exporter=LoggingSpanExporter(event_processor=self.event_processor), | ||
) | ||
|
||
def langchain_callback( | ||
self, | ||
to_ignore: Optional[List[str]] = None, | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.