-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathtest_client_v2.py
More file actions
98 lines (81 loc) · 3.98 KB
/
test_client_v2.py
File metadata and controls
98 lines (81 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
import typing
import unittest
import cohere
from cohere import ToolMessage, UserMessage, AssistantMessage
co = cohere.ClientV2(timeout=10000)
package_dir = os.path.dirname(os.path.abspath(__file__))
embed_job = os.path.join(package_dir, "embed_job.jsonl")
class TestClientV2(unittest.TestCase):
def test_chat(self) -> None:
response = co.chat(
model="command-a-03-2025", messages=[cohere.UserChatMessageV2(content="hello world!")])
print(response.message)
def test_chat_stream(self) -> None:
stream = co.chat_stream(
model="command-a-03-2025", messages=[cohere.UserChatMessageV2(content="hello world!")])
events = set()
for chat_event in stream:
if chat_event is not None:
events.add(chat_event.type)
if chat_event.type == "content-delta":
print(chat_event.delta)
self.assertTrue("message-start" in events)
self.assertTrue("content-start" in events)
self.assertTrue("content-delta" in events)
self.assertTrue("content-end" in events)
self.assertTrue("message-end" in events)
def test_legacy_methods_available(self) -> None:
self.assertTrue(hasattr(co, "generate"))
self.assertTrue(callable(getattr(co, "generate")))
self.assertTrue(hasattr(co, "generate_stream"))
self.assertTrue(callable(getattr(co, "generate_stream")))
@unittest.skip("Skip v2 test for now")
def test_chat_documents(self) -> None:
from cohere import Document
documents = [
Document(data={"title": "widget sales 2019", "text": "1 million"}),
Document(data={"title": "widget sales 2020", "text": "2 million"}),
Document(data={"title": "widget sales 2021", "text": "4 million"}),
]
response = co.chat(
messages=[cohere.UserChatMessageV2(
content=[cohere.TextContent(text="how many widges were sold in 2020?")],
)],
model="command-a-03-2025",
documents=documents,
)
print(response.message)
@unittest.skip("Skip v2 test for now")
def test_chat_tools(self) -> None:
from typing import Sequence
get_weather_tool = cohere.ToolV2Function(
name="get_weather",
description="gets the weather of a given location",
parameters={
"type": "object",
"properties": {
"location": {
"type": "str",
"description": "the location to get weather, example: San Fransisco, CA",
}
},
"required": ["location"],
},
)
tools = [cohere.ToolV2(type="function", function=get_weather_tool)]
messages: cohere.ChatMessages = [
cohere.UserChatMessageV2(content="what is the weather in Toronto?")
]
res = co.chat(model="command-a-03-2025", tools=tools, messages=messages)
# call the get_weather tool
tool_result = {"temperature": "30C"}
tool_content: Sequence[cohere.TextToolContent] = [cohere.TextToolContent(text="The weather in Toronto is 30C")]
# Use the first text content from the response if available, else fallback to str
assistant_content = res.message.content[0].text if (hasattr(res.message, 'content') and isinstance(res.message.content, list) and len(res.message.content) > 0 and hasattr(res.message.content[0], 'text')) else str(res.message)
messages.append(cohere.AssistantChatMessageV2(content=[cohere.TextAssistantMessageV2ContentItem(text=assistant_content)]))
if res.message.tool_calls is not None and res.message.tool_calls[0].id is not None:
messages.append(cohere.ToolChatMessageV2(
tool_call_id=res.message.tool_calls[0].id, content=list(tool_content)))
res = co.chat(tools=tools, messages=messages, model="command-a-03-2025")
print(res.message)