Skip to main content

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 no api_key is provided to the Client 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.28.0.tar.gz (32.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opperai-0.28.0-py3-none-any.whl (55.5 kB view details)

Uploaded Python 3

File details

Details for the file opperai-0.28.0.tar.gz.

File metadata

  • Download URL: opperai-0.28.0.tar.gz
  • Upload date:
  • Size: 32.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for opperai-0.28.0.tar.gz
Algorithm Hash digest
SHA256 5237717597554c573ea4c9bd5bb333e5d68bfd21a2f1db4d0b9982c1b980f472
MD5 79d7731a969dcee0a743ab30b4932e9b
BLAKE2b-256 3d0d9819435526d5247a499d3a1e29a4b00c79d1be8c2f02d2ca75bd7f043faa

See more details on using hashes here.

Provenance

The following attestation bundles were made for opperai-0.28.0.tar.gz:

Publisher: publish.yml on opper-ai/opper-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file opperai-0.28.0-py3-none-any.whl.

File metadata

  • Download URL: opperai-0.28.0-py3-none-any.whl
  • Upload date:
  • Size: 55.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for opperai-0.28.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6edb2362d35850084ed2ae9f2c60b1e75fcee349d5e73939ba5baac9d49064d
MD5 667143af0ee3d9ca3a6511f5bdcc7cf8
BLAKE2b-256 8284be32a10b981055f9e76ac78cb6d30f49d5fdcc9731db5505b47e6b6851df

See more details on using hashes here.

Provenance

The following attestation bundles were made for opperai-0.28.0-py3-none-any.whl:

Publisher: publish.yml on opper-ai/opper-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page