from cosette import *
cosette
Install
pip install cosette
Getting started
OpenAI’s Python SDK will automatically be installed with Cosette, if you don’t already have it.
Cosette only exports the symbols that are needed to use the library, so you can use import *
to import them. Alternatively, just use:
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.
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')
For these examples, we’ll use GPT-4o.
= models[2] model
Chat
The main interface to Cosette is the Chat
class, which provides a stateful interface to the models:
= Chat(model, sp="""You are a helpful and concise assistant.""")
chat "I'm Jeremy") chat(
Hi Jeremy! How can I assist you today?
- id: chatcmpl-AxxPi7CxP8htRrhdq9LCsxpify0PZ
- choices: [Choice(finish_reason=‘stop’, index=0, logprobs=None, message=ChatCompletionMessage(content=‘Hi Jeremy! How can I assist you today?’, refusal=None, role=‘assistant’, audio=None, function_call=None, tool_calls=None))]
- created: 1738853102
- model: gpt-4o-2024-08-06
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_4691090a87
- usage: CompletionUsage(completion_tokens=11, prompt_tokens=21, total_tokens=32, 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))
= chat("What's my name?")
r r
You mentioned that your name is Jeremy.
- id: chatcmpl-AxxPjt6ssVIPEdoLxwAiZm8gPBbmA
- choices: [Choice(finish_reason=‘stop’, index=0, logprobs=None, message=ChatCompletionMessage(content=‘You mentioned that your name is Jeremy.’, refusal=None, role=‘assistant’, audio=None, function_call=None, tool_calls=None))]
- created: 1738853103
- model: gpt-4o-2024-08-06
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_4691090a87
- usage: CompletionUsage(completion_tokens=9, prompt_tokens=43, total_tokens=52, 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))
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:
print(r)
ChatCompletion(id='chatcmpl-AxxPjt6ssVIPEdoLxwAiZm8gPBbmA', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='You mentioned that your name is Jeremy.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None))], created=1738853103, model='gpt-4o-2024-08-06', object='chat.completion', service_tier='default', system_fingerprint='fp_4691090a87', usage=In: 43; Out: 9; Total: 52)
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!)
for o in chat("What's your name?", stream=True): print(o, end='')
I'm an AI assistant and don't have a personal name, but you can call me Assistant. How can I help you today, Jeremy?
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:
# o1 does not support streaming or setting the temperature
'o1'), can_set_system_prompt('o1'), can_set_temperature('o1') can_stream(
(False, True, False)
# gpt-4o has these capabilities
'gpt-4o'), can_set_system_prompt('gpt-4o'), can_set_temperature('gpt-4o') can_stream(
(True, True, True)
Tool use
Tool use lets the model use external tools.
We use docments 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:
def sums(
int, # First thing to sum
a:int=1 # Second thing to sum
b:-> 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:
= "Never mention what tools you use." sp
We’ll get the model to add up some long numbers:
= 604542,6458932
a,b = f"What is {a}+{b}?"
pr pr
'What is 604542+6458932?'
To use tools, pass a list of them to Chat
:
= Chat(model, sp=sp, tools=[sums]) chat
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:
= chat(pr)
r r
Finding the sum of 604542 and 6458932
- id: chatcmpl-AxxPmOn0RKSJDkJJnJFe0fsKqrOnq
- choices: [Choice(finish_reason=‘tool_calls’, index=0, logprobs=None, message=ChatCompletionMessage(content=None, refusal=None, role=‘assistant’, audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id=‘call_pxKfC4xMONCzur2k9X9p6NiT’, function=Function(arguments=‘{“a”:604542,“b”:6458932}’, name=‘sums’), type=‘function’)]))]
- created: 1738853106
- model: gpt-4o-2024-08-06
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_50cad350e4
- usage: CompletionUsage(completion_tokens=22, prompt_tokens=86, total_tokens=108, 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:
chat()
The sum of 604542 and 6458932 is 7063474.
- id: chatcmpl-AxxPnVPUfJiXWCQUpVXIaep48J8Qb
- choices: [Choice(finish_reason=‘stop’, index=0, logprobs=None, message=ChatCompletionMessage(content=‘The sum of 604542 and 6458932 is 7063474.’, refusal=None, role=‘assistant’, audio=None, function_call=None, tool_calls=None))]
- created: 1738853107
- model: gpt-4o-2024-08-06
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_4691090a87
- 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))
You can see how many tokens have been used at any time by checking the use
property.
chat.use
In: 204; Out: 41; Total: 245
Tool loop
We can do everything needed to use tools in a single step, by using Chat.toolloop
. This can even call multiple tools as needed solve a problem. For example, let’s define a tool to handle multiplication:
def mults(
int, # First thing to multiply
a:int=1 # Second thing to multiply
b:-> 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:
= Chat(model, sp=sp, tools=[sums,mults])
chat = f'Calculate ({a}+{b})*2'
pr pr
'Calculate (604542+6458932)*2'
def pchoice(r): print(r.choices[0])
= chat.toolloop(pr, trace_func=pchoice) r
Finding the sum of 604542 and 6458932
Finding the product of 2 and 7103474
Choice(finish_reason='tool_calls', index=0, logprobs=None, message=ChatCompletionMessage(content=None, refusal=None, role='assistant', audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_Sfet73hgfRtSI2N25K97D7tO', function=Function(arguments='{"a": 604542, "b": 6458932}', name='sums'), type='function'), ChatCompletionMessageToolCall(id='call_mFQNJgjATAI2pYFFQyvfg0W2', function=Function(arguments='{"a": 2, "b": 7103474}', name='mults'), type='function')]))
Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The result of \\((604542 + 6458932) \\times 2\\) is 14,206,948.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None))
OpenAI uses special tags for math equations, which we can replace using wrap_latex
:
wrap_latex(contents(r))
The result of \((604542 + 6458932) \times 2\) is 14,206,948.
Images
As everyone knows, when testing image APIs you have to use a cute puppy.
= Path('samples/puppy.jpg')
fn =fn, width=200) display.Image(filename
We create a Chat
object as before:
= Chat(model) chat
Claudia expects images as a list of bytes, so we read in the file:
= fn.read_bytes() img
Prompts to Claudia can be lists, containing text, images, or both, eg:
"In brief, what color flowers are in this image?"]) chat([img,
The flowers in the image are purple.
- id: chatcmpl-AxxPrQeO00Ks5X93eKJQ1t6Dtqxu6
- choices: [Choice(finish_reason=‘stop’, index=0, logprobs=None, message=ChatCompletionMessage(content=‘The flowers in the image are purple.’, refusal=None, role=‘assistant’, audio=None, function_call=None, tool_calls=None))]
- created: 1738853111
- model: gpt-4o-2024-08-06
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_4691090a87
- usage: CompletionUsage(completion_tokens=9, prompt_tokens=458, total_tokens=467, 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))
The image is included as input tokens.
chat.use
In: 458; Out: 9; Total: 467
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:
= Chat(model)
chat chat(img)
This is an image of a Cavalier King Charles Spaniel puppy lying on the grass next to some purple flowers. The puppy has white and brown fur and an adorable expression.
- id: chatcmpl-AxxPts6mRTeap94CEPIdf7INa5Xkx
- choices: [Choice(finish_reason=‘stop’, index=0, logprobs=None, message=ChatCompletionMessage(content=‘This is an image of a Cavalier King Charles Spaniel puppy lying on the grass next to some purple flowers. The puppy has white and brown fur and an adorable expression.’, refusal=None, role=‘assistant’, audio=None, function_call=None, tool_calls=None))]
- created: 1738853113
- model: gpt-4o-2024-08-06
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_4691090a87
- usage: CompletionUsage(completion_tokens=36, prompt_tokens=447, total_tokens=483, 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))
'What direction is the puppy facing?') chat(
The puppy is facing towards the camera.
- id: chatcmpl-AxxPuqxXMuefTG3ehawZW2rMll6co
- choices: [Choice(finish_reason=‘stop’, index=0, logprobs=None, message=ChatCompletionMessage(content=‘The puppy is facing towards the camera.’, refusal=None, role=‘assistant’, audio=None, function_call=None, tool_calls=None))]
- created: 1738853114
- model: gpt-4o-2024-08-06
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_4691090a87
- usage: CompletionUsage(completion_tokens=9, prompt_tokens=497, total_tokens=506, 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))
'What color is it?') chat(
The puppy is white with brown markings.
- id: chatcmpl-AxxPw64bsATMHf3a5Rzal8WjqgHRM
- choices: [Choice(finish_reason=‘stop’, index=0, logprobs=None, message=ChatCompletionMessage(content=‘The puppy is white with brown markings.’, refusal=None, role=‘assistant’, audio=None, function_call=None, tool_calls=None))]
- created: 1738853116
- model: gpt-4o-2024-08-06
- object: chat.completion
- service_tier: default
- system_fingerprint: fp_4691090a87
- usage: CompletionUsage(completion_tokens=9, prompt_tokens=518, total_tokens=527, 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))
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.
chat.use
In: 1462; Out: 54; Total: 1516