Calls
What is a Call?
A Call is a call to a LLM to perform a generation. With the Opper SDKs, calls can be configured to use a specific model, prompt, schema. Note that all calls are automatically logged as traces in Opper.
Behind the scenes, the Opper API works to construct prompts for the model to generate outputs that optimially completes the call.
Make a Call
Make a call
from opperai import Opper
# Your API key will be loaded from the environment variable OPPER_API_KEY if not provided
opper = Opper()
result, _ = opper.call(
name="respond",
input="What is the capital of Sweden?")
print(result)
# The capital of Sweden is Stockholm.
Call with Prompt
Add instructions
from opperai import AsyncOpper
# Your API key will be loaded from the environment variable OPPER_API_KEY if not provided
opper = Opper()
result, _ = opper.call(
name="respond",
input="What is the capital of Sweden?",
instructions="Given a question, produce an answer in Swedish"
)
print(result)
# Huvudstaden i Sverige är Stockholm.
Use Schemas
Functions can be configured to return a specific JSON schema and Opper takes care of "forcing" the models to respond in the correct way:
Structured Output
from opperai import Opper
from pydantic import BaseModel
# Your API key will be loaded from the environment variable OPPER_API_KEY if not provided
opper = Opper()
class Room(BaseModel):
beds: int
seaview: bool
description: str
result, _ = await opper.call(
name="extractRoom",
input="Room at Grand Hotel with 2 beds and a view to the sea.",
output_type=Room
)
print(result)
# Room(beds=2, seaview=True, description="Room at Grand Hotel with 2 beds and a view to the sea")
Specifying Model
Opper default uses azure/gpt4-turbo
for all calls. To specify a different model:
Call a specific model
from opperai import Opper
# Your API key will be loaded from the environment variable OPPER_API_KEY if not provided
opper = Opper()
result, _ = opper.call(
name="respond",
input="What is the capital of Sweden?",
model="anthropic/claude-3.5-sonnet"
)
print(result)
# The capital of Sweden is Stockholm.
See Models for more information.
Specifying Examples
Providing examples to a call is a great way to show how you want outputs to look like given some inputs. This will help the model reason without having to improve the prompt. A good way to instruct a cheap model to perform well.
Examples
from opperai import Opper
from opperai.types import Example
# Your API key will be loaded from the environment variable OPPER_API_KEY if not provided
opper = Opper()
result, _ = opper.call(
name="GetFirstWeekday",
instructions= "Extract the first weekday mentioned in the text",
examples= [
Example(
input= "Today is Tuesday, yesterday was Monday",
output= "Monday",
),
Example(
input= "Sunday, Saturday and Friday are the best days of the week",
output= "Friday",
),
Example(
input= "It is Saturday that is the best day of the week, next to Tuesday",
output= "Tuesday",
),
],
input="Do you want to come by on this Thursday or this Tuesday?",
)
print(result)
# Tuesday
Turn call into a Function
By naming a call, you can turn it into a Generative Function. You can get analytics and evaluations of this Function in the Opper platform.
Turn call into a Function
from opperai import Opper
# Your API key will be loaded from the environment variable OPPER_API_KEY if not provided
opper = Opper()
result, _ = opper.call(
name="respond",
input="What is the capital of Sweden?",
)
print(result)
# The capital of Sweden is Stockholm.
Read more
- Read our Cookbook for additional examples: https://github.com/opper-ai/opper-cookbook