Skip to content
Open
Show file tree
Hide file tree
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 @@ -95,8 +95,11 @@ response = co.chat(
},
],
)
print(response.message.content[-1].text)

print(response.message.content[0].text)
# for message in response.message.content:
# if hasattr(message, 'text'):
# print(message.text)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover commented-out tutorial code

Low Severity

A commented-out loop over response.message.content was left in the first chatbot example after the content[-1].text print. It has no surrounding explanation, appears in only this snippet, and looks like an exploratory alternative rather than tutorial content.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 6f553cd. Configure here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add a comment description if necessary. Just wanted to show an example for the user of iterating the messages for only those that have a text attribute.

```

```
Expand Down Expand Up @@ -158,7 +161,7 @@ response = co.chat(
# assistant turn 2
messages.append(response.message)

print(response.message.content[0].text)
print(response.message.content[-1].text)
```


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,15 @@ response = co.chat(

# Append assistant response to the chat history
messages.append(
{"role": "assistant", "content": response.message.content[0].text}
{
"role": "assistant",
"content": response.message.content[-1].text,
}
)

# Print final response
print("Response:")
print(response.message.content[0].text)
print(response.message.content[-1].text)
print("=" * 50)

# Print citations (if any)
Expand Down Expand Up @@ -385,13 +388,13 @@ def run_assistant(query, messages=None):
messages.append(
{
"role": "assistant",
"content": response.message.content[0].text,
"content": response.message.content[-1].text,
}
)

# Print final response
print("Response:")
print(response.message.content[0].text)
print(response.message.content[-1].text)
print("=" * 50)

# Print citations (if any)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ response = co.chat(
)

# Display the response
print(response.message.content[0].text)
print(response.message.content[-1].text)

# Display the citations and source documents
if response.message.citations:
Expand Down Expand Up @@ -436,7 +436,7 @@ response = co.chat(
)

# Display the response
print(response.message.content[0].text)
print(response.message.content[-1].text)

# Display the citations and source documents
if response.message.citations:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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.
The response contains several objects. For simplicity, what we want right now is the `message.content[-1].text` object (the `text` attribute of the last (-1) element of the `content` array attribute).

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

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

print(response.message.content[0].text)
print(response.message.content[-1].text)
```

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

print(response.message.content[0].text)
print(response.message.content[-1].text)
```

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

print(response.message.content[0].text)
print(response.message.content[-1].text)
```

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

print(response.message.content[0].text)
print(response.message.content[-1].text)
```

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

print(f"{idx+1}: {response.message.content[0].text}\n")
print(f"{idx+1}: {response.message.content[-1].text}\n")
```

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

print(f"{idx+1}: {response.message.content[0].text}\n")
print(f"{idx+1}: {response.message.content[-1].text}\n")
```

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

json_object = json.loads(response.message.content[0].text)
json_object = json.loads(response.message.content[-1].text)

print(json_object)
```
Expand Down Expand Up @@ -329,7 +329,10 @@ response = co.chat_stream(

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

Expand Down
34 changes: 17 additions & 17 deletions notebooks/guides/getting-started/v2/tutorial_pt2_v2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@
"source": [
"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.\n",
"\n",
"The response contains several objects. For simplicity, what we want right now is the `message.content[0].text` object.\n",
"The response contains several objects. For simplicity, what we want right now is the `message.content[-1].text` object (the `text` attribute of the last (-1) element of the `content` array attribute).\n",
"\n",
"Here's an example of the assistant responding to a new hire's query asking for help to make introductions."
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -124,7 +124,7 @@
")\n",
"# messages=[cohere.UserMessage(content=message)])\n",
"\n",
"print(response.message.content[0].text)"
"print(response.message.content[-1].text)"
]
},
{
Expand Down Expand Up @@ -162,7 +162,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -183,7 +183,7 @@
" messages=[{\"role\": \"user\", \"content\": message}]\n",
")\n",
"\n",
"print(response.message.content[0].text)"
"print(response.message.content[-1].text)"
]
},
{
Expand All @@ -199,7 +199,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -235,7 +235,7 @@
" messages=[{\"role\": \"user\", \"content\": message}]\n",
")\n",
"\n",
"print(response.message.content[0].text)"
"print(response.message.content[-1].text)"
]
},
{
Expand Down Expand Up @@ -265,7 +265,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -284,7 +284,7 @@
"response = co.chat(model=\"command-a-plus-05-2026\",\n",
" messages=[{\"role\": \"user\", \"content\": message}])\n",
"\n",
"print(response.message.content[0].text)"
"print(response.message.content[-1].text)"
]
},
{
Expand All @@ -302,7 +302,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -330,7 +330,7 @@
" temperature=0,\n",
" )\n",
"\n",
" print(f\"{idx+1}: {response.message.content[0].text}\\n\")"
" print(f\"{idx+1}: {response.message.content[-1].text}\\n\")"
]
},
{
Expand All @@ -342,7 +342,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -370,7 +370,7 @@
" temperature=1,\n",
" )\n",
"\n",
" print(f\"{idx+1}: {response.message.content[0].text}\\n\")"
" print(f\"{idx+1}: {response.message.content[-1].text}\\n\")"
]
},
{
Expand Down Expand Up @@ -403,7 +403,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -446,7 +446,7 @@
" },\n",
")\n",
"\n",
"json_object = json.loads(response.message.content[0].text)\n",
"json_object = json.loads(response.message.content[-1].text)\n",
"\n",
"print(json_object)"
]
Expand Down Expand Up @@ -481,7 +481,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -502,7 +502,7 @@
"\n",
"for event in response:\n",
" if event:\n",
" if event.type == \"content-delta\":\n",
" if event.type == \"content-delta\" and event.delta.message.content.text is not None:\n",
" print(event.delta.message.content.text, end=\"\")"
]
},
Expand Down
Loading