Skip to main content

Idiomatic way to build chatgpt apps using async generators in python

Project description

turbo-chat

Idiomatic way to build chatgpt apps using async generators in python

turbo

About

The ChatGPT API uses a new input format called chatml. In openai's python client, the format is used something like this:

messages = [
    {"role": "system", "content": "Greet the user!"},
    {"role": "user", "content": "Hello world!"},
]

The idea here is to incrementally build the messages using an async generator and then use that to generate completions. Async generators are incredibly versatile and simple abstraction for doing this kind of stuff. They can also be composed together very easily.

# Equivalent turbo-chat generator
async def example():
    yield System(content="Greet the user!")
    yield User(content="Hello World!")

    # To run generation, just yield Generate(),
    # the lib will take care of correctly running the app, and
    # return the value back here.
    output = yield Generate()
    print(output.content)

See more detailed example below.

Installation

pip install turbo-chat

Example

from typing import AsyncGenerator, Union

from turbo_chat import (
    turbo,
    System,
    User,
    Assistant,
    GetInput,
    Generate,
    run,
)

# Get user
async def get_user(id):
    return {"zodiac": "pisces"}

# Set user zodiac mixin
# Notice that no `@turbo()` decorator used here
async def set_user_zodiac(user_id: int):

    user_data: dict = await get_user(user_id)
    zodiac: str = user_data["zodiac"]

    yield User(content=f"My zodiac sign is {zodiac}")


# Horoscope app
@turbo(temperature=0.0)
async def horoscope(user_id: int):

    yield System(content="You are a fortune teller")

    # Yield from mixin
    async for output in set_user_zodiac(user_id):
        yield output

    # Prompt runner to ask for user input
    input = yield GetInput(message="What do you want to know?")

    # Yield the input
    yield User(content=input)

    # Generate (overriding the temperature)
    value = yield Generate(temperature=0.9)

# Let's run this
app: AsyncGenerator[Union[Assistant, GetInput], str] = horoscope({"user_id": 1})

_input = None
while not (result := await (app.run(_input)).done:
    if result.needs_input:
        # Prompt user with the input message
        _input = input(result.content)
        continue

    print(result.content)

# Output
# >>> What do you want to know? Tell me my fortune
# >>> As an AI language model, I cannot predict the future or provide supernatural fortune-telling. However, I can offer guidance and advice based on your current situation and past experiences. Is there anything specific you would like me to help you with?
#

Custom memory

You can also customize how the messages are persisted in-between the executions.

from turbo_chat import turbo, BaseMemory

class RedisMemory(BaseMemory):
    """Implement BaseMemory methods here"""

    async def setup(self, **kwargs) -> None:
        ...

    async def append(self, item) -> None:
        ...

    async def clear(self) -> None:
        ...


# Now use the memory in a turbo_chat app
@turbo(memory_class=RedisMemory)
async def app():
    ...

Get access to memory object directly (just declare an additional param)

@turbo()
async def app(some_param: Any, memory: BaseMemory):

    messages = await memory.get()
    ...

Generate a response to use internally but don't yield downstream

@turbo()
async def example():
    yield System(content="You are a good guy named John")
    yield User(content="What is your name?")
    result = yield Generate(forward=False)

    yield User(content="How are you doing?")
    result = yield Generate()

b = example()
results = [output async for output in b]

assert len(results) == 1

Add a simple in-memory cache

You can also subclass the BaseCache class to create a custom cache.

cache = SimpleCache()

@turbo(cache=cache)
async def example():
    yield System(content="You are a good guy named John")
    yield User(content="What is your name?")
    result = yield Generate()

b = example()
results = [output async for output in b]

assert len(cache.cache) == 1

Latest Changes

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

turbo_chat-0.3.8.tar.gz (803.2 kB view details)

Uploaded Source

Built Distribution

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

turbo_chat-0.3.8-py3-none-any.whl (812.9 kB view details)

Uploaded Python 3

File details

Details for the file turbo_chat-0.3.8.tar.gz.

File metadata

  • Download URL: turbo_chat-0.3.8.tar.gz
  • Upload date:
  • Size: 803.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.4 Linux/5.4.0-1105-azure

File hashes

Hashes for turbo_chat-0.3.8.tar.gz
Algorithm Hash digest
SHA256 a0456a99369613a19080a4cdbdbcf67799a73ffe9f9bc938d31298a7d308898d
MD5 afa8ee465f83005a0bbe6d9212d7b16f
BLAKE2b-256 567179c48791cd08570765f46fd601cab7d8480536901a71ac0861306b6223f9

See more details on using hashes here.

File details

Details for the file turbo_chat-0.3.8-py3-none-any.whl.

File metadata

  • Download URL: turbo_chat-0.3.8-py3-none-any.whl
  • Upload date:
  • Size: 812.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.4 Linux/5.4.0-1105-azure

File hashes

Hashes for turbo_chat-0.3.8-py3-none-any.whl
Algorithm Hash digest
SHA256 6d5f7892a6695851253486a9deb750e6a236df7623f38875da73e82f041ccb1c
MD5 333f61605f85c4fba6f322bc02951c3e
BLAKE2b-256 45f36234731fed2f16a58a73c1d1f6a9fefbfbe70ce79c6b5873c3dec008559b

See more details on using hashes here.

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