import os
import asyncio
from literalai import LiteralClient
from openai import AsyncOpenAI
openai_client = AsyncOpenAI(api_key=os.getenv("OPENAI_API_KEY"))
literal_client = LiteralClient(api_key=os.getenv("LITERAL_API_KEY"))
# Optionally instrument the openai client to log generations
literal_client.instrument_openai()
async def main():
prompt = await literal_client.api.get_prompt(name="Default")
# Optionally pass variables to the prompt
variables = {"foo": "bar"}
messages = prompt.format(variables)
stream = await openai_client.chat.completions.create(
messages=messages,
# Optionally pass the tools defined in the prompt
tools=prompt.tools,
# Pass the settings defined in the prompt
**prompt.settings,
stream=True
)
async for chunk in stream:
print(chunk)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())