-
Notifications
You must be signed in to change notification settings - Fork 725
/
__init__.py
62 lines (51 loc) · 1.96 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from typing import Union
from chainlit.context import get_context
from chainlit.step import Step
from chainlit.sync import run_sync
from chainlit.utils import check_module_version
from literalai import ChatGeneration, CompletionGeneration
from literalai.helper import timestamp_utc
def instrument_openai():
if not check_module_version("openai", "1.0.0"):
raise ValueError(
"Expected OpenAI version >= 1.0.0. Run `pip install openai --upgrade`"
)
from literalai.instrumentation.openai import instrument_openai
async 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
await step.send()
def on_new_generation_sync(
generation: Union["ChatGeneration", "CompletionGeneration"], timing
):
run_sync(on_new_generation(generation, timing))
instrument_openai(None, on_new_generation_sync)