forked from Arize-ai/openinference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_stream_chat.py
More file actions
23 lines (18 loc) · 885 Bytes
/
openai_stream_chat.py
File metadata and controls
23 lines (18 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from llama_index.core.base.llms.types import ChatMessage
from llama_index.llms.openai import OpenAI
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from openinference.instrumentation.llama_index import LlamaIndexInstrumentor
endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
LlamaIndexInstrumentor().instrument(tracer_provider=tracer_provider)
llm = OpenAI(model="gpt-3.5-turbo")
if __name__ == "__main__":
response_gen = llm.stream_chat(
[ChatMessage(content="hello")],
stream_options={"include_usage": True},
)
for response in response_gen:
print(response.delta, end="")