POST
/
functions
from typing import Optional
from pydantic import BaseModel, Field
from opperai import Opper
import time

opper = Opper(http_bearer="YOUR_API_KEY")


class QuestionInput(BaseModel):
    question: str = Field(description="The question to answer")
    context: Optional[str] = Field(None, description="Additional context")


class AnswerOutput(BaseModel):
    answer: str = Field(description="The answer to the question")
    confidence: Optional[float] = Field(None, description="Confidence score 0-1")


# Create a new function with unique name
unique_name = f"question_answerer_{int(time.time())}"
function = opper.functions.create(
    name=unique_name,
    description="Answers questions based on provided context",
    instructions="You are a helpful assistant that answers questions clearly and concisely. If context is provided, use it to inform your answer.",
    input_schema=QuestionInput,
    output_schema=AnswerOutput,
    model="openai/gpt-4o-mini",
)

print(f"Created function: {function.name}")
print(f"Function ID: {function.id}")
print(f"Dataset ID: {function.dataset_id}")
{
  "name": "my-function",
  "description": "This function is used to add two numbers and return the result.",
  "instructions": "You are a calculator that adds two numbers and returns the result.",
  "input_schema": {
    "properties": {
      "x": {
        "title": "X",
        "type": "integer"
      },
      "y": {
        "title": "Y",
        "type": "integer"
      }
    },
    "required": [
      "x",
      "y"
    ],
    "title": "OpperInputExample",
    "type": "object"
  },
  "output_schema": {
    "properties": {
      "sum": {
        "title": "Sum",
        "type": "integer"
      }
    },
    "required": [
      "sum"
    ],
    "title": "OpperOutputExample",
    "type": "object"
  },
  "model": {
    "extra_headers": {},
    "name": "openai/gpt-4o-mini",
    "options": {
      "temperature": 0.5
    }
  },
  "configuration": {
    "beta.evaluation.enabled": true,
    "invocation.cache.ttl": 0,
    "invocation.few_shot.count": 0,
    "invocation.structured_generation.max_attempts": 5
  },
  "id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
  "dataset_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
  "revision_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a"
}

Authorizations

Authorization
string
header
required

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

Body

application/json

Response

201
application/json

Successful Response

The response is of type object.