Metadata-Version: 2.1
Name: tokreate
Version: 0.2.0
Summary: Unified APIs for making calls to different LLMs.
Author-email: Luca Soldaini <luca@soldaini.net>
License: Apache-2.0
Project-URL: Homepage, https://github.com/soldni/tokreate
Classifier: Development Status :: 3 - Alpha
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: openai
Requires-Dist: anthropic
Requires-Dist: together
Requires-Dist: msgspec
Requires-Dist: jinja2
Requires-Dist: boto3
Requires-Dist: watchtower
Requires-Dist: logging_json==0.4.0
Provides-Extra: dev
Requires-Dist: black>=22.6.0; extra == "dev"
Requires-Dist: isort>=5.10.1; extra == "dev"
Requires-Dist: mypy>=0.971; extra == "dev"
Requires-Dist: pytest>=5.2; extra == "dev"
Requires-Dist: ipython>=8.4.0; extra == "dev"
Requires-Dist: autopep8>=1.7.0; extra == "dev"
Requires-Dist: flake8>=5.0; extra == "dev"
Requires-Dist: ipdb>=0.13.0; extra == "dev"
Requires-Dist: flake8-pyi>=22.8.1; extra == "dev"
Requires-Dist: Flake8-pyproject>=1.1.0; extra == "dev"

<h1 align="center">Tokreate</h1>

<p align="center">
    <img alt="Tokreate official logo" src="https://github.com/soldni/tokreate/blob/main/docs/res/tokreation_1x.png?raw=true" width="50%">
</p>

A library to create tokens using LLMs.

**I refuse to write more than 200 lines of code for this.**
No [LangChain](https://www.langchain.com) shenenigans.
See [tokereate.py](https://github.com/soldni/tokreate/blob/main/src/tokreate/tokereate.py) to see where we stand.


Tokreate is [available on PyPI](https://pypi.org/project/tokreate/)! Simply run:

```shell
pip install tokreate
```

to get started.

## Usage

Start by setting up a call action:

```python
from tokreate import CallAction

greetings = CallAction(
    prompt="Hello, my name is {{ name }}; what's yours?",
    model="gpt-3.5-turbo"
)

# tokreate always returns a history of the conversation
history = greetings.run(name="Alice")

for turn in history:
    print(turn)

# Output:
# user>>> Hello, my name is Alice; what's yours?
# assistant>>> Hello Alice, nice to meet you! I'm ChatGPT. How can I assist you today?
```

Actions can be chained together:

```python

from tokreate import CallAction

greetings = CallAction(
    prompt="Hello, my name is {{ name }}; what's yours?",
    model="gpt-3.5-turbo"
)
tell_joke = CallAction(
    prompt="Can you finish this joke: What do you call a {{ animal }} with no legs?",
    model="gpt-3.5-turbo"
)

history = (greetings >> tell_joke).run(name="Alice", animal="snake")

for turn in history:
    print(turn)

# Output:
# user>>> Hello, my name is Alice; what's yours?
# assistant>>> Hello Alice, nice to meet you! I'm ChatGPT. How can I assist you today?
# user>>> Can you finish this joke: What do you call a snake with no legs?
# assistant>>> What do you call a snake with no legs? A "legless" reptile!
```

You can use parsers to extract information from any call:

```python

import json
from tokreate import CallAction, ParseAction

random_numbers = CallAction(
    prompt="Return a list of {{ random_count }} random numbers.",
    system="You are an helpful assistant who responds by always returning a valid JSON object.",
    model="gpt-3.5-turbo"
)
json_parser = ParseAction(
    parser=json.loads
)
*_, last_turn = (random_numbers >> json_parser).run(random_count='five')

parsed_content = last_turn.state["json.loads"]
print(f"Response {parsed_content} (type: {type(parsed_content)})")

# Output:
# Response {'numbers': [4, 9, 2, 7, 1]} (type: <class 'dict'>)
```

You can switch to different models at any point:

```python
from tokreate import CallAction

greetings = CallAction(
    prompt="Hello, my name is {{ name }}; what's yours?",
    model="claude-instant-v1.1"
)

history = greetings.run(name="Alice")

for turn in history:
    print(turn)

# Output:
# user>>> Hello, my name is Alice; what's yours?
# assistant>>> I'm Claude, an AI assistant created by Anthropic.
```

Let's try with one of the TogetherAI models:

```python
from tokreate import CallAction

greetings = CallAction(
    prompt="Hello, my name is {{ name }}; what's yours?",
    model="togethercomputer/llama-2-70b-chat",
    history=False   # TogetherAI models don't support history at API level yet :( you can make your own in prompt
)

history = greetings.run(name="Alice")

for turn in history:
    print(turn)

# Output:
# user>>> Hello, my name is Alice; what's yours?
# assistant>>>  Hello Alice! My name is ChatBot, it's nice to meet you. How can I assist you today? Is there a specific topic you'd like to discuss or ask me a question about?
```
