# cosette


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Install

``` sh
pip install cosette
```

## Getting started

OpenAI’s Python SDK will automatically be installed with Cosette, if you
don’t already have it.

``` python
from cosette import *
```

Cosette only exports the symbols that are needed to use the library, so
you can use `import *` to import them. Alternatively, just use:

``` python
import cosette
```

…and then add the prefix `cosette.` to any usages of the module.

Cosette provides `models`, which is a list of models currently available
from the SDK.

``` python
' '.join(models)
```

    'o1-preview o1-mini gpt-4o gpt-4o-mini gpt-4-turbo gpt-4 gpt-4-32k gpt-3.5-turbo gpt-3.5-turbo-instruct o1 o3-mini chatgpt-4o-latest o1-pro o3 o4-mini gpt-4.1 gpt-4.1-mini gpt-4.1-nano'

For these examples, we’ll use GPT-4.1.

``` python
model = 'gpt-4.1'
```

## Chat

The main interface to Cosette is the
[`Chat`](https://AnswerDotAI.github.io/cosette/core.html#chat) class,
which provides a stateful interface to the models:

``` python
chat = Chat(model, sp="""You are a helpful and concise assistant.""")
chat("I'm Jeremy")
```

Hi Jeremy! How can I help you today?

<details>

- id: chatcmpl-BjwyifaV82goo6WYIeEORBGDMLCSA
- choices: \[Choice(finish_reason=‘stop’, index=0, logprobs=None,
  message=ChatCompletionMessage(content=‘Hi Jeremy! How can I help you
  today?’, refusal=None, role=‘assistant’, annotations=\[\], audio=None,
  function_call=None, tool_calls=None))\]
- created: 1750291172
- model: gpt-4.1-2025-04-14
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_51e1070cf2
- usage: CompletionUsage(completion_tokens=10, prompt_tokens=21,
  total_tokens=31,
  completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0,
  audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0),
  prompt_tokens_details=PromptTokensDetails(audio_tokens=0,
  cached_tokens=0))

</details>

``` python
r = chat("What's my name?")
r
```

Your name is Jeremy. How can I assist you, Jeremy?

<details>

- id: chatcmpl-BjwyjN4t2wKzVWBRVWhD6buZF8y07
- choices: \[Choice(finish_reason=‘stop’, index=0, logprobs=None,
  message=ChatCompletionMessage(content=‘Your name is Jeremy. How can I
  assist you, Jeremy?’, refusal=None, role=‘assistant’,
  annotations=\[\], audio=None, function_call=None, tool_calls=None))\]
- created: 1750291173
- model: gpt-4.1-2025-04-14
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_b3f1157249
- usage: CompletionUsage(completion_tokens=13, prompt_tokens=43,
  total_tokens=56,
  completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0,
  audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0),
  prompt_tokens_details=PromptTokensDetails(audio_tokens=0,
  cached_tokens=0))

</details>

As you see above, displaying the results of a call in a notebook shows
just the message contents, with the other details hidden behind a
collapsible section. Alternatively you can `print` the details:

``` python
print(r)
```

    ChatCompletion(id='chatcmpl-BjwyjN4t2wKzVWBRVWhD6buZF8y07', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='Your name is Jeremy. How can I assist you, Jeremy?', refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=None))], created=1750291173, model='gpt-4.1-2025-04-14', object='chat.completion', service_tier='default', system_fingerprint='fp_b3f1157249', usage=In: 43; Out: 13; Total: 56)

You can use `stream=True` to stream the results as soon as they arrive
(although you will only see the gradual generation if you execute the
notebook yourself, of course!)

``` python
for o in chat("What's your name?", stream=True): print(o, end='')
```

    I’m an AI assistant created by OpenAI, and you can just call me Assistant! If you’d like to give me a nickname, feel free—what would you like to call me?

## Model Capabilities

Different OpenAI models have different capabilities. Some models such as
o1-mini do not have support for streaming, system prompts, or
temperature. Query these capbilities using these functions:

``` python
# o1 does not support streaming or setting the temperature
can_stream('o1'), can_set_system_prompt('o1'), can_set_temperature('o1')
```

    (True, True, False)

``` python
# gpt-4o has these capabilities
can_stream('gpt-4o'), can_set_system_prompt('gpt-4o'), can_set_temperature('gpt-4o')
```

    (True, True, True)

## Tool use

[Tool use](https://docs.openai.com/claude/docs/tool-use) lets the model
use external tools.

We use [docments](https://fastcore.fast.ai/docments.html) to make
defining Python functions as ergonomic as possible. Each parameter (and
the return value) should have a type, and a docments comment with the
description of what it is. As an example we’ll write a simple function
that adds numbers together, and will tell us when it’s being called:

``` python
def sums(
    a:int,  # First thing to sum
    b:int=1 # Second thing to sum
) -> int: # The sum of the inputs
    "Adds a + b."
    print(f"Finding the sum of {a} and {b}")
    return a + b
```

Sometimes the model will say something like “according to the `sums`
tool the answer is” – generally we’d rather it just tells the user the
answer, so we can use a system prompt to help with this:

``` python
sp = "Never mention what tools you use."
```

We’ll get the model to add up some long numbers:

``` python
a,b = 604542,6458932
pr = f"What is {a}+{b}?"
pr
```

    'What is 604542+6458932?'

To use tools, pass a list of them to
[`Chat`](https://AnswerDotAI.github.io/cosette/core.html#chat):

``` python
chat = Chat(model, sp=sp, tools=[sums])
```

Now when we call that with our prompt, the model doesn’t return the
answer, but instead returns a `tool_use` message, which means we have to
call the named tool with the provided parameters:

``` python
r = chat(pr)
r
```

    Finding the sum of 604542 and 6458932

- id: chatcmpl-Bjwyvg3bSWW0pKTxdwKCfhZKnwpho
- choices: \[Choice(finish_reason=‘tool_calls’, index=0, logprobs=None,
  message=ChatCompletionMessage(content=None, refusal=None,
  role=‘assistant’, annotations=\[\], audio=None, function_call=None,
  tool_calls=\[ChatCompletionMessageToolCall(id=‘call_cry44pvhtr0KDszQFufZjyGN’,
  function=Function(arguments=‘{“a”:604542,“b”:6458932}’, name=‘sums’),
  type=‘function’)\]))\]
- created: 1750291185
- model: gpt-4.1-2025-04-14
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_51e1070cf2
- usage: CompletionUsage(completion_tokens=21, prompt_tokens=86,
  total_tokens=107,
  completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0,
  audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0),
  prompt_tokens_details=PromptTokensDetails(audio_tokens=0,
  cached_tokens=0))

Cosette handles all that for us – we just have to pass along the
message, and it all happens automatically:

``` python
chat()
```

604,542 + 6,458,932 equals 7,063,474.

<details>

- id: chatcmpl-Bjx0vtvAnE4W7z0dupqPfqnJngBCy
- choices: \[Choice(finish_reason=‘stop’, index=0, logprobs=None,
  message=ChatCompletionMessage(content=‘604,542 + 6,458,932 equals
  7,063,474.’, refusal=None, role=‘assistant’, annotations=\[\],
  audio=None, function_call=None, tool_calls=None))\]
- created: 1750291309
- model: gpt-4.1-2025-04-14
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_51e1070cf2
- usage: CompletionUsage(completion_tokens=19, prompt_tokens=118,
  total_tokens=137,
  completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0,
  audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0),
  prompt_tokens_details=PromptTokensDetails(audio_tokens=0,
  cached_tokens=0))

</details>

You can see how many tokens have been used at any time by checking the
`use` property.

``` python
chat.use
```

    In: 204; Out: 40; Total: 244

### Tool loop

We can do everything needed to use tools in a single step, by using
[`Chat.toolloop`](https://AnswerDotAI.github.io/cosette/toolloop.html#chat.toolloop).
This can even call multiple tools as needed solve a problem. For
example, let’s define a tool to handle multiplication:

``` python
def mults(
    a:int,  # First thing to multiply
    b:int=1 # Second thing to multiply
) -> int: # The product of the inputs
    "Multiplies a * b."
    print(f"Finding the product of {a} and {b}")
    return a * b
```

Now with a single call we can calculate `(a+b)*2` – by passing
`show_trace` we can see each response from the model in the process:

``` python
chat = Chat(model, sp=sp, tools=[sums,mults])
pr = f'Calculate ({a}+{b})*2'
pr
```

    'Calculate (604542+6458932)*2'

``` python
def pchoice(r): print(r.choices[0])
```

``` python
r = chat.toolloop(pr)
```

OpenAI uses special tags for math equations, which we can replace using
[`wrap_latex`](https://AnswerDotAI.github.io/cosette/core.html#wrap_latex):

``` python
for o in r:
    display(wrap_latex(contents(o)))
```

(604542 + 6458932) × 2 = 14,126,948.

## Images

As everyone knows, when testing image APIs you have to use a cute puppy.

``` python
fn = Path('samples/puppy.jpg')
Image(filename=fn, width=200)
```

<img src="index_files/figure-commonmark/cell-23-output-1.jpeg"
width="200" />

We create a
[`Chat`](https://AnswerDotAI.github.io/cosette/core.html#chat) object as
before:

``` python
chat = Chat(model)
```

Claudia expects images as a list of bytes, so we read in the file:

``` python
img = fn.read_bytes()
```

Prompts to Claudia can be lists, containing text, images, or both, eg:

``` python
chat([img, "In brief, what color flowers are in this image?"])
```

The flowers in the image are purple.

<details>

- id: chatcmpl-Bjx2lnRK05FvJWFh2smshhAWw0TXx
- choices: \[Choice(finish_reason=‘stop’, index=0, logprobs=None,
  message=ChatCompletionMessage(content=‘The flowers in the image are
  purple.’, refusal=None, role=‘assistant’, annotations=\[\],
  audio=None, function_call=None, tool_calls=None))\]
- created: 1750291423
- model: gpt-4.1-2025-04-14
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_51e1070cf2
- usage: CompletionUsage(completion_tokens=8, prompt_tokens=273,
  total_tokens=281,
  completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0,
  audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0),
  prompt_tokens_details=PromptTokensDetails(audio_tokens=0,
  cached_tokens=0))

</details>

The image is included as input tokens.

``` python
chat.use
```

    In: 273; Out: 8; Total: 281

Alternatively, Cosette supports creating a multi-stage chat with
separate image and text prompts. For instance, you can pass just the
image as the initial prompt (in which case the model will make some
general comments about what it sees), and then follow up with questions
in additional prompts:

``` python
chat = Chat(model)
chat(img)
```

This is an image of an adorable puppy lying on the grass next to some
purple flowers. The puppy appears to be a Cavalier King Charles Spaniel,
known for their sweet expressions, long ears, and beautiful markings.
The scene looks peaceful and charming, with the flowers adding a touch
of color and nature to the setting.

<details>

- id: chatcmpl-Bjx2nluEtIzGnD5IMxE8c2RsG3CNW
- choices: \[Choice(finish_reason=‘stop’, index=0, logprobs=None,
  message=ChatCompletionMessage(content=‘This is an image of an adorable
  puppy lying on the grass next to some purple flowers. The puppy
  appears to be a Cavalier King Charles Spaniel, known for their sweet
  expressions, long ears, and beautiful markings. The scene looks
  peaceful and charming, with the flowers adding a touch of color and
  nature to the setting.’, refusal=None, role=‘assistant’,
  annotations=\[\], audio=None, function_call=None, tool_calls=None))\]
- created: 1750291425
- model: gpt-4.1-2025-04-14
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_51e1070cf2
- usage: CompletionUsage(completion_tokens=65, prompt_tokens=262,
  total_tokens=327,
  completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0,
  audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0),
  prompt_tokens_details=PromptTokensDetails(audio_tokens=0,
  cached_tokens=0))

</details>

``` python
chat('What direction is the puppy facing?')
```

The puppy is facing towards the camera, looking directly at the viewer.
Its body is positioned sideways, but its head is turned forward, making
eye contact with the camera.

<details>

- id: chatcmpl-Bjx2pgeYeYLF5UHSd9iY68IeAjIxy
- choices: \[Choice(finish_reason=‘stop’, index=0, logprobs=None,
  message=ChatCompletionMessage(content=‘The puppy is facing towards the
  camera, looking directly at the viewer. Its body is positioned
  sideways, but its head is turned forward, making eye contact with the
  camera.’, refusal=None, role=‘assistant’, annotations=\[\],
  audio=None, function_call=None, tool_calls=None))\]
- created: 1750291427
- model: gpt-4.1-2025-04-14
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_51e1070cf2
- usage: CompletionUsage(completion_tokens=34, prompt_tokens=342,
  total_tokens=376,
  completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0,
  audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0),
  prompt_tokens_details=PromptTokensDetails(audio_tokens=0,
  cached_tokens=0))

</details>

``` python
chat('What color is it?')
```

The puppy is predominantly white with brown markings, particularly on
its ears and around its eyes. Its nose is black. This color pattern is
common in certain breeds, such as the Cavalier King Charles Spaniel.

<details>

- id: chatcmpl-Bjx2rl2tCdnEcvWF78UN0UaSwnvUS
- choices: \[Choice(finish_reason=‘stop’, index=0, logprobs=None,
  message=ChatCompletionMessage(content=‘The puppy is predominantly
  white with brown markings, particularly on its ears and around its
  eyes. Its nose is black. This color pattern is common in certain
  breeds, such as the Cavalier King Charles Spaniel.’, refusal=None,
  role=‘assistant’, annotations=\[\], audio=None, function_call=None,
  tool_calls=None))\]
- created: 1750291429
- model: gpt-4.1-2025-04-14
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_51e1070cf2
- usage: CompletionUsage(completion_tokens=42, prompt_tokens=389,
  total_tokens=431,
  completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0,
  audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0),
  prompt_tokens_details=PromptTokensDetails(audio_tokens=0,
  cached_tokens=0))

</details>

Note that the image is passed in again for every input in the dialog, so
that number of input tokens increases quickly with this kind of chat.

``` python
chat.use
```

    In: 993; Out: 141; Total: 1134
