GithubHelp home page GithubHelp logo

upstash / qstash-py Goto Github PK

View Code? Open in Web Editor NEW
10.0 8.0 1.0 193 KB

Python SDK for Upstash QStash

Home Page: https://docs.upstash.com/qstash

License: MIT License

Python 100.00%
qstash sdk-python upstash upstash-sdk

qstash-py's Introduction

Upstash Python QStash SDK

Note

This project is in GA Stage.

The Upstash Professional Support fully covers this project. It receives regular updates, and bug fixes. The Upstash team is committed to maintaining and improving its functionality.

QStash is an HTTP based messaging and scheduling solution for serverless and edge runtimes.

QStash Documentation

Install

pip install qstash

Usage

You can get your QStash token from the Upstash Console.

Publish a JSON message

from qstash import QStash

client = QStash("<QSTASH_TOKEN>")

res = client.message.publish_json(
    url="https://example.com",
    body={"hello": "world"},
    headers={
        "test-header": "test-value",
    },
)

print(res.message_id)
from qstash import QStash

client = QStash("<QSTASH_TOKEN>")

schedule_id = client.schedule.create(
    destination="https://example.com",
    cron="*/5 * * * *",
)

print(schedule_id)
from qstash import Receiver

# Keys available from the QStash console
receiver = Receiver(
    current_signing_key="CURRENT_SIGNING_KEY",
    next_signing_key="NEXT_SIGNING_KEY",
)

# ... in your request handler

signature, body = req.headers["Upstash-Signature"], req.body

receiver.verify(
    body=body,
    signature=signature,
    url="https://example.com",  # Optional
)

Create Chat Completions

from qstash import QStash
from qstash.chat import upstash

client = QStash("<QSTASH_TOKEN>")

res = client.chat.create(
    model="meta-llama/Meta-Llama-3-8B-Instruct",
    provider=upstash(),
    messages=[
        {
            "role": "user",
            "content": "What is the capital of Turkey?",
        }
    ],
)

print(res.choices[0].message.content)

Create Chat Completions Using Custom Providers

from qstash import QStash
from qstash.chat import openai

client = QStash("<QSTASH_TOKEN>")

res = client.chat.create(
    model="gpt-3.5-turbo",
    provider=openai("<OPENAI_API_KEY>"),
    messages=[
        {
            "role": "user",
            "content": "What is the capital of Turkey?",
        }
    ],
)

print(res.choices[0].message.content)

Publish a JSON message to LLM

from qstash import QStash
from qstash.chat import upstash

client = QStash("<QSTASH_TOKEN>")

res = client.message.publish_json(
    api={"name": "llm", "provider": upstash()},
    body={
        "model": "meta-llama/Meta-Llama-3-8B-Instruct",
        "messages": [
            {
                "role": "user",
                "content": "What is the capital of Turkey?",
            }
        ],
    },
    callback="https://example-cb.com",
)

print(res.message_id)

Publish a JSON message to LLM Using Custom Providers

from qstash import QStash
from qstash.chat import openai

client = QStash("<QSTASH_TOKEN>")

res = client.message.publish_json(
    api={"name": "llm", "provider": openai("<OPENAI_API_KEY>")},
    body={
        "model": "gpt-3.5-turbo",
        "messages": [
            {
                "role": "user",
                "content": "What is the capital of Turkey?",
            }
        ],
    },
    callback="https://example-cb.com",
)

print(res.message_id)

Additional configuration

from qstash import QStash

# Create a client with a custom retry configuration. This is
# for sending messages to QStash, not for sending messages to
# your endpoints.
# The default configuration is:
# {
#   "retries": 5,
#   "backoff": lambda retry_count: math.exp(retry_count) * 50,
# }
client = QStash(
    token="<QSTASH_TOKEN>",
    retry={
        "retries": 1,
        "backoff": lambda retry_count: (2 ** retry_count) * 20,
    },
)

# Publish to URL
client.message.publish_json(
    url="https://example.com",
    body={"key": "value"},
    # Retry sending message to API 3 times
    # https://upstash.com/docs/qstash/features/retry
    retries=3,
    # Schedule message to be sent 4 seconds from now
    delay="4s",
    # When message is sent, send a request to this URL
    # https://upstash.com/docs/qstash/features/callbacks
    callback="https://example.com/callback",
    # When message fails to send, send a request to this URL
    failure_callback="https://example.com/failure_callback",
    # Headers to forward to the endpoint
    headers={
        "test-header": "test-value",
    },
    # Enable content-based deduplication
    # https://upstash.com/docs/qstash/features/deduplication#content-based-deduplication
    content_based_deduplication=True,
)

Additional methods are available for managing url groups, schedules, and messages. See the examples folder for more.

Development

  1. Clone the repository
  2. Install Poetry
  3. Install dependencies with poetry install
  4. Create a .env file with cp .env.example .env and fill in the QSTASH_TOKEN
  5. Run tests with poetry run pytest
  6. Format with poetry run ruff format .

qstash-py's People

Contributors

meshankhosla avatar mdumandag avatar cahidarda avatar pedroimpulcetto avatar mdogan avatar burak-upstash avatar ytkimirti avatar

Stargazers

 avatar  avatar Nelson LIN avatar  avatar  avatar Abhishek avatar  avatar  avatar  avatar Fahreddin Özcan avatar

Watchers

Enes Akar avatar Sertuğ Kaya avatar Muhammet Şen avatar Emir Özbir avatar  avatar  avatar Shin avatar Fahreddin Özcan avatar

Forkers

pedroimpulcetto

qstash-py's Issues

Typing Examples

Proposal

Hey there, do we want to use type hints in rows for types?

Example:

from upstash_qstash import Client
from qstash_tokens import QSTASH_TOKEN
from upstash_qstash.schedules import CreateScheduleRequest

client = Client(QSTASH_TOKEN)
schedules = client.schedules()

params: CreateScheduleRequest = {
            "cron": "* * * * *",
            "destination": "https://py-qstash-testing.requestcatcher.com",
            "body": {"hello": "world"},
            "headers": {
                "content-type": "application/json",  # This is the default, but you can override it
            },
        }
res = schedules.create(params)

So, by doing that params: CreateScheduleRequest we can give a really good experience to the user and he can make sure is passing the right params to the method.
Also, he can have a quick check with the VSCode autocomplete feature.

Screenshot 2024-05-03 at 4 49 20 PM

And see if some key is optional or required as well.

Screenshot 2024-05-03 at 4 53 48 PM


If we want to do it, I can work on that 😄

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.