Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ co = cohere.ClientV2(api_key="COHERE_API_KEY")

To get started with Chat, we need to pass two parameters, `model` for the LLM model ID and `messages`, which we add a single user message. We then call the Chat endpoint through the client we created earlier.

The response contains several objects. For simplicity, what we want right now is the `message.content[0].text` object.

Here's an example of the assistant responding to a new hire's query asking for help to make introductions.


Expand All @@ -60,7 +58,11 @@ response = co.chat(
)
# messages=[cohere.UserMessage(content=message)])

print(response.message.content[0].text)
for content_item in response.message.content:
if content_item.type == "thinking":
print("thinking:", content_item.thinking)
if content_item.type == "text":
print("text:", content_item.text)
```

```
Expand Down Expand Up @@ -103,7 +105,11 @@ response = co.chat(
)
# messages=[cohere.UserMessage(content=message)])

print(response.message.content[0].text)
for content_item in response.message.content:
if content_item.type == "thinking":
print("thinking:", content_item.thinking)
if content_item.type == "text":
print("text:", content_item.text)
```

```
Expand Down Expand Up @@ -144,7 +150,11 @@ response = co.chat(
messages=[{"role": "user", "content": message}],
)

print(response.message.content[0].text)
for content_item in response.message.content:
if content_item.type == "thinking":
print("thinking:", content_item.thinking)
if content_item.type == "text":
print("text:", content_item.text)
```

```
Expand Down Expand Up @@ -173,7 +183,11 @@ response = co.chat(
messages=[{"role": "user", "content": message}],
)

print(response.message.content[0].text)
for content_item in response.message.content:
if content_item.type == "thinking":
print("thinking:", content_item.thinking)
if content_item.type == "text":
print("text:", content_item.text)
```

```
Expand Down Expand Up @@ -201,7 +215,9 @@ for idx in range(3):
temperature=0,
)

print(f"{idx+1}: {response.message.content[0].text}\n")
for content_item in response.message.content:
if content_item.type == "text":
print(f"{idx+1}: {content_item.text}\n")
```

```
Expand All @@ -228,7 +244,9 @@ for idx in range(3):
temperature=1,
)

print(f"{idx+1}: {response.message.content[0].text}\n")
for content_item in response.message.content:
if content_item.type == "text":
print(f"{idx+1}: {content_item.text}\n")
```

```
Expand Down Expand Up @@ -293,7 +311,10 @@ response = co.chat(
},
)

json_object = json.loads(response.message.content[0].text)
for content_item in response.message.content:
if content_item.type == "text":
json_object = json.loads(content_item.text)
break

print(json_object)
```
Expand All @@ -314,7 +335,7 @@ The Chat endpoint also provides streaming support. In a streamed response, the e

To activate it, use `co.chat_stream()` instead of `co.chat()`.

In streaming mode, the endpoint will generate a series of objects. To get the actual text contents, we take objects whose `event_type` is `content-delta`.
In streaming mode, the endpoint will generate a series of objects. To get the actual text contents, we take objects whose `event.type` is `content-delta`.


```python PYTHON
Expand All @@ -328,9 +349,9 @@ response = co.chat_stream(
)

for event in response:
if event:
if event.type == "content-delta":
print(event.delta.message.content.text, end="")
if event and event.type == "content-delta":
if event.delta.message.content.text:
print(event.delta.message.content.text, end="", flush=True)
```

```
Expand Down