Skip to content

Instantly share code, notes, and snippets.

@harpiechoise
Created November 13, 2023 00:37
Show Gist options
  • Save harpiechoise/bd1bbb810f4aa3ca223f32ae34746a1b to your computer and use it in GitHub Desktop.
Save harpiechoise/bd1bbb810f4aa3ca223f32ae34746a1b to your computer and use it in GitHub Desktop.
from fastapi import FastAPI, Request
from pydantic import BaseModel, Field
class Result(BaseModel):
result: int = Field(title="Result",
description="The result of an arithmetic operation.")
app = FastAPI(
title="Arithmetic API",
description="This is a simple API that performs arithmetic operations.",
version="0.0.1",
servers=[{'url': 'https://z3c6nsr.localto.net/',
'description': 'Arithmetic API server'}],
)
@app.get("//maths/add/{num1}/{num2}",
name="Add",
description="This is an endpoint that adds two numbers using path parameters, and yields the result in JSON format.",
tags=["maths"],
response_model=Result
)
def add(num1: int, num2: int, request: Request):
ephemeral_user_id = request.headers.get('openai-ephemeral-user-id')
conversation_id = request.headers.get('openai-conversation-id')
print(f"ephemeral_user_id: {ephemeral_user_id}")
print(f"conversation_id: {conversation_id}")
return Result(result=num1 + num2)
@app.get("//maths/subtract/{num1}/{num2}",
name="Subtract",
description="This is an endpoint that subtracts two numbers using path parameters, and yields the result in JSON format.",
tags=["maths"],
response_model=Result
)
def subtract(num1: int, num2: int):
return Result(result=num1 - num2)
@app.get("//maths/multiply/{num1}/{num2}",
name="Multiply",
description="This is an endpoint that multiplies two numbers using path parameters, and yields the result in JSON format.",
tags=["maths"],
response_model=Result
)
def multiply(num1: int, num2: int):
return Result(result=num1 * num2)
@app.get("//maths/divide/{num1}/{num2}",
name="Divide",
description="This is an endpoint that divides two numbers using path parameters, and yields the result in JSON format.",
tags=["maths"],
response_model=Result
)
def divide(num1: int, num2: int):
return Result(result=num1 / num2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment