-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mistral instrumentation (#1100)
* feat: add mistral instrumentation * fix: remove requirements on mistralai * fix: add changelog
- Loading branch information
Showing
4 changed files
with
68 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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,53 @@ | ||
import asyncio | ||
from typing import Union | ||
|
||
from literalai import ChatGeneration, CompletionGeneration | ||
from literalai.helper import timestamp_utc | ||
|
||
from chainlit.context import get_context | ||
from chainlit.step import Step | ||
from chainlit.utils import check_module_version | ||
|
||
|
||
def instrument_mistralai(): | ||
from literalai.instrumentation.mistralai import instrument_mistralai | ||
|
||
def on_new_generation( | ||
generation: Union["ChatGeneration", "CompletionGeneration"], timing | ||
): | ||
context = get_context() | ||
|
||
parent_id = None | ||
if context.current_step: | ||
parent_id = context.current_step.id | ||
elif context.session.root_message: | ||
parent_id = context.session.root_message.id | ||
|
||
step = Step( | ||
name=generation.model if generation.model else generation.provider, | ||
type="llm", | ||
parent_id=parent_id, | ||
) | ||
step.generation = generation | ||
# Convert start/end time from seconds to milliseconds | ||
step.start = ( | ||
timestamp_utc(timing.get("start")) | ||
if timing.get("start", None) is not None | ||
else None | ||
) | ||
step.end = ( | ||
timestamp_utc(timing.get("end")) | ||
if timing.get("end", None) is not None | ||
else None | ||
) | ||
|
||
if isinstance(generation, ChatGeneration): | ||
step.input = generation.messages | ||
step.output = generation.message_completion # type: ignore | ||
else: | ||
step.input = generation.prompt | ||
step.output = generation.completion | ||
|
||
asyncio.create_task(step.send()) | ||
|
||
instrument_mistralai(None, on_new_generation) |
This file contains 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