Skip to content

Commit 391eb1e

Browse files
committed
docs: add end-to-end example showing full OCI application flow
Demonstrates embeddings → vector search → grounded chat → tool use (multi-turn) → vision → streaming in a single runnable script. All steps verified against live OCI Generative AI.
1 parent 91b312b commit 391eb1e

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

fern/pages/deployment-options/oracle-cloud-infrastructure-oci.mdx

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,144 @@ for i, embedding in enumerate(response.embeddings.float_):
345345
| `tokenize` | Offline only |
346346
| `detokenize` | Offline only |
347347

348+
## End-to-End Example
349+
350+
The following example demonstrates a complete application flow on OCI Generative AI: embedding documents for a knowledge base, retrieving relevant context, using tool calling for live data, processing images with vision, and streaming a final response.
351+
352+
```python
353+
import cohere
354+
import base64
355+
356+
# Initialize V2 client for Command A models
357+
client = cohere.OciClientV2(
358+
oci_region="us-chicago-1",
359+
oci_compartment_id="ocid1.compartment.oc1...",
360+
)
361+
362+
# --- Step 1: Build a knowledge base with embeddings ---
363+
364+
documents = [
365+
"Oracle Cloud Infrastructure provides enterprise-grade AI services.",
366+
"Cohere Command A is a 111B parameter model with 256K context window.",
367+
"OCI Generative AI is FedRAMP High and DISA IL5 authorized.",
368+
]
369+
370+
doc_embeddings = client.embed(
371+
model="embed-english-v3.0",
372+
texts=documents,
373+
input_type="search_document",
374+
).embeddings.float_
375+
376+
query_embedding = client.embed(
377+
model="embed-english-v3.0",
378+
texts=["What security certifications does OCI have?"],
379+
input_type="search_query",
380+
).embeddings.float_[0]
381+
382+
# Find the most relevant document (cosine similarity)
383+
best_idx = max(
384+
range(len(documents)),
385+
key=lambda i: sum(a * b for a, b in zip(query_embedding, doc_embeddings[i])),
386+
)
387+
print(f"Best match: {documents[best_idx]}")
388+
389+
# --- Step 2: Grounded chat with retrieved context ---
390+
391+
response = client.chat(
392+
model="command-a-03-2025",
393+
messages=[
394+
{"role": "system", "content": "Answer based on the provided context only."},
395+
{"role": "user", "content": f"Context: {documents[best_idx]}\n\nWhat certifications does OCI have?"},
396+
],
397+
temperature=0.3,
398+
)
399+
print(f"Answer: {response.message.content[0].text}")
400+
401+
# --- Step 3: Tool use — call an external API ---
402+
403+
response = client.chat(
404+
model="command-a-03-2025",
405+
messages=[{"role": "user", "content": "What's the current stock price of ORCL?"}],
406+
tools=[{
407+
"type": "function",
408+
"function": {
409+
"name": "get_stock_price",
410+
"description": "Get the current stock price for a ticker symbol",
411+
"parameters": {
412+
"type": "object",
413+
"properties": {
414+
"ticker": {"type": "string", "description": "Stock ticker symbol"}
415+
},
416+
"required": ["ticker"],
417+
},
418+
},
419+
}],
420+
)
421+
422+
# Model returns a tool call
423+
tool_call = response.message.tool_calls[0]
424+
print(f"Tool call: {tool_call.function.name}({tool_call.function.arguments})")
425+
426+
# Send the tool result back
427+
final = client.chat(
428+
model="command-a-03-2025",
429+
messages=[
430+
{"role": "user", "content": "What's the current stock price of ORCL?"},
431+
{
432+
"role": "assistant",
433+
"tool_calls": [{"id": tool_call.id, "type": "function", "function": {"name": tool_call.function.name, "arguments": tool_call.function.arguments}}],
434+
"tool_plan": response.message.tool_plan,
435+
},
436+
{
437+
"role": "tool",
438+
"tool_call_id": tool_call.id,
439+
"content": [{"type": "text", "text": '{"ticker": "ORCL", "price": 187.42, "currency": "USD"}'}],
440+
},
441+
],
442+
tools=[{
443+
"type": "function",
444+
"function": {
445+
"name": "get_stock_price",
446+
"description": "Get the current stock price for a ticker symbol",
447+
"parameters": {
448+
"type": "object",
449+
"properties": {"ticker": {"type": "string"}},
450+
"required": ["ticker"],
451+
},
452+
},
453+
}],
454+
)
455+
print(f"Final answer: {final.message.content[0].text}")
456+
457+
# --- Step 4: Vision — analyze an image ---
458+
459+
with open("chart.png", "rb") as f:
460+
img_b64 = base64.b64encode(f.read()).decode()
461+
462+
response = client.chat(
463+
model="command-a-vision",
464+
messages=[{
465+
"role": "user",
466+
"content": [
467+
{"type": "text", "text": "Describe the trend shown in this chart."},
468+
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{img_b64}"}},
469+
],
470+
}],
471+
)
472+
print(f"Vision: {response.message.content[0].text}")
473+
474+
# --- Step 5: Stream a response in real time ---
475+
476+
print("Streaming: ", end="")
477+
for event in client.chat_stream(
478+
model="command-a-03-2025",
479+
messages=[{"role": "user", "content": "Summarize why enterprises choose OCI for AI."}],
480+
):
481+
if event.type == "content-delta":
482+
print(event.delta.message.content.text, end="")
483+
print()
484+
```
485+
348486
## Additional Resources
349487

350488
- [Cohere Python SDK on GitHub](https://github.com/cohere-ai/cohere-python)

0 commit comments

Comments
 (0)