Opper Python client
Project description
Opper Python SDK
Install
pip install opperai
Configuration
Environment variable
OPPER_API_KEY
environment variable is read by the SDK if noapi_key
is provided to theClient
object.OPPER_PROJECT
is attached to traces and can be used for filtering in the Opper UI.OPPER_DEFAULT_MODEL
will define the model used by functions created with thefn
decorator
When using the fn
decorator the SDK client is automatically initialized with the OPPER_API_KEY
environment variable.
Using the fn
decorator
from opperai import fn
@fn()
def translate(text: str, target_language: str) -> str:
"""Translate text to a target language."""
print(translate("Hello","fr"))
>>> "Bonjour"
The fn
decorator automatically creates an Opper function ready to be called like any other function in your code. They're no different than any other function!
Calling functions
To call a function you created at https://platform.opper.ai you can use the following code:
from opperai import Opper
from opperai.types import Message
opper = Opper()
function = opper.functions.create(
"jokes/tell",
instructions="given a topic tell a joke",
)
response = function.chat(
messages=[Message(role="user", content="topic: python")]
)
print(response)
Async function calling
import asyncio
from opperai import AsyncOpper
from opperai.types import Message
async def main():
opper = AsyncOpper()
function = await opper.functions.create(
"jokes/tell",
instructions="given a topic tell a joke",
)
response = await function.chat(
messages=[Message(role="user", content="topic: python")],
)
print(response)
if __name__ == "__main__":
asyncio.run(main())
Streaming responses
from opperai import Opper
from opperai.types import Message
opper = Opper()
function = opper.functions.create(
"jokes/tell",
instructions="given a topic tell a joke",
description="tell a joke",
)
response = function.chat(
messages=[Message(role="user", content="topic: python")],
stream=True
)
for delta in response.deltas:
print(delta, end="", flush=True)
Async streaming responses
import asyncio
from opperai import AsyncOpper
from opperai.types import Message
async def main():
opper = AsyncOpper()
function = await opper.functions.create(
"jokes/tell",
instructions="given a topic tell a joke",
description="tell a joke",
)
response = await function.chat(
messages=[Message(role="user", content="topic: python")],
stream=True,
)
async for delta in response.deltas:
print(delta, end="", flush=True)
if __name__ == "__main__":
asyncio.run(main())
Indexes
from opperai import Opper
from opperai.types import Document, Filter
opper = Opper()
index = opper.indexes.create("my-index")
index.upload_file("file.txt")
index.add(Document(key="key1", content="Hello world 1", metadata={"score": 0}))
index.add(Document(key="key1", content="Hello world 1", metadata={"score": 1}))
index.add(Document(key="key2", content="Hello world 3", metadata={"score": 0}))
response = index.query("Hello")
print(response)
response = index.query("Hello", filters=[Filter(key="score", operation="=", value="1")])
print(response)
Examples
See examples in our documentation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
opperai-0.5.3.tar.gz
(39.3 kB
view hashes)
Built Distribution
Close
Hashes for opperai-0.5.3-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf4bcabe85cf9b96c1b734053aecabe72782be568d8850180fd67b3b3024ed00 |
|
MD5 | d1b61ae8367d24957f44241f6dd788b1 |
|
BLAKE2b-256 | 563e2152bde0df74e5593c3db5f37b5e5550716a1b9d3c6f0681586b17350387 |