forked from Arize-ai/openinference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvertexai.model.generate_content_async.py
More file actions
31 lines (22 loc) · 1008 Bytes
/
vertexai.model.generate_content_async.py
File metadata and controls
31 lines (22 loc) · 1008 Bytes
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
import asyncio
import vertexai
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from vertexai.generative_models import GenerativeModel
from openinference.instrumentation.vertexai import VertexAIInstrumentor
endpoint = "http://127.0.0.1:4317"
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
VertexAIInstrumentor().instrument(tracer_provider=tracer_provider)
vertexai.init(location="us-central1")
model = GenerativeModel("gemini-1.5-flash")
async def main() -> None:
response = await model.generate_content_async(
"Write a haiku.",
generation_config={"max_output_tokens": 20},
)
print(response)
if __name__ == "__main__":
asyncio.run(main())