-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcohere_basic.py
More file actions
52 lines (39 loc) · 1.3 KB
/
Copy pathcohere_basic.py
File metadata and controls
52 lines (39 loc) · 1.3 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
"""Smallest possible ``@protect`` usage with raw Cohere.
The first ``@protect`` call lazily creates the runtime and patches
httpx so the Cohere Python SDK fires ``track_llm`` events
automatically. NullRun's URL-keyed extractor reads the Cohere
response body and pulls out ``prompt_tokens`` /
``completion_tokens``.
Run:
pip install nullrun cohere
export NULLRUN_API_KEY=nr_live_...
export COHERE_API_KEY=...
python examples/cohere_basic.py
"""
from __future__ import annotations
from _env import load_env
load_env() # populate os.environ from examples/.env (no-op if absent)
import os
import cohere
import nullrun
from nullrun import protect, shutdown
client = cohere.ClientV2(api_key=os.environ["COHERE_API_KEY"])
@protect
def answer(prompt: str) -> str:
response = client.chat(
model="command-r-plus",
messages=[{"role": "user", "content": prompt}],
)
# Cohere returns a list of message content blocks; pick the text ones.
parts = []
for msg in response.message.content or []:
text = getattr(msg, "text", None)
if text:
parts.append(text)
return "".join(parts)
if __name__ == "__main__":
try:
with nullrun.handle():
print(answer("In one sentence, what does NullRun do?"))
finally:
shutdown()