Opper Python client
Project description
Opper Python SDK
This is the Opper Python SDK. See below for getting started, and the docs for more information. The SDK has builtin documentation and examples in function docstrings, which should be visible in your code editor as you are using the functions.
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.
Using opper
from typing import List
from opperai import Index, Opper, trace
from opperai.types import DocumentIn
from opperai.types.indexes import DocumentIn, RetrievalResponse
from pydantic import BaseModel
class Answer(BaseModel):
steps: List[str]
class QuestionAndContext(BaseModel):
question: str
context: List[RetrievalResponse]
@trace
def answer_question(index: Index, question: str) -> Answer:
results = index.query(question, 1, None)
result, response = opper.call(
name="answer_question",
instructions="Answer the question and provide the steps to do so",
input=QuestionAndContext(question=question, context=results),
output_type=Answer,
)
response.span.save_metric("artificial_score", 5)
return result
@trace
def translate(answer: Answer, language: str) -> str:
result, _ = opper.call(
name="translate",
instructions="Translate the answer to the given language",
input=answer,
output_type=Answer,
)
return result
qna = [
{
"question": "I cannot log in to my account",
"answer": "Use the reset password feature by clicking on 'Forgot password?' and then follow the instructions from email",
"id": "1",
},
{
"question": "How can I see my invoices?",
"answer": "Go to the billing section and click on 'Invoices'",
"id": "2",
},
{
"question": "How can I add a new user to my account?",
"answer": "Upgrade account and add the user to your account",
"id": "3",
},
]
opper = Opper()
def index_qna(qnas: list[dict]):
index = opper.indexes.create("qna")
for qna in qnas:
index.add(
DocumentIn(
key=qna["id"],
content=f"question: {qna['question']}\nanswer: {qna['answer']}",
metadata={
"id": qna["id"],
},
)
)
return index
def run():
index = index_qna(qna)
question = "How can I see my invoices?"
with opper.traces.start("answer_question") as trace:
answer = answer_question(index, question)
print(answer)
run()
More examples
See examples in our documentation and examples folder.
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.18.0.tar.gz
(31.7 kB
view details)
Built Distribution
opperai-0.18.0-py3-none-any.whl
(52.8 kB
view details)
File details
Details for the file opperai-0.18.0.tar.gz
.
File metadata
- Download URL: opperai-0.18.0.tar.gz
- Upload date:
- Size: 31.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | afa55b0abb4954c7a06a38beaf184cd0f0b64205e5e4a6bfbf3a00d09a7e7b08 |
|
MD5 | 43c70f58561b222271800a41a3683363 |
|
BLAKE2b-256 | f431cee883bffa83a25aea7f660ebc7ff75725cdf687c38fe2cfcbbb9f1706d4 |
File details
Details for the file opperai-0.18.0-py3-none-any.whl
.
File metadata
- Download URL: opperai-0.18.0-py3-none-any.whl
- Upload date:
- Size: 52.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77833f3ef59c80c303c3990b6f702a80b413224fde4230d35ff6df9e0c4257ca |
|
MD5 | 4ef443cbc149bb908adfff76a305ddd9 |
|
BLAKE2b-256 | 022bc0626e71f21fa8ab328e93664764c2e79a1a21d7d261fd07074dc8e518ae |