Skip to content

Commit ac08232

Browse files
committed
client-openai-vector_stores-completions.py
1 parent 53f4ee8 commit ac08232

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/python3
2+
3+
"""
4+
Demo script showing RAG with Chat Completions API - Manual retrieval with explicit control
5+
6+
Run this script after starting a Llama Stack server:
7+
llama stack run starter
8+
"""
9+
10+
import io
11+
import os
12+
13+
import requests
14+
from openai import OpenAI
15+
16+
c = OpenAI(base_url="http://localhost:8321/v1/", api_key="none")
17+
18+
vs = c.vector_stores.create()
19+
buf = io.BytesIO(str("abc is green").encode('utf-8'))
20+
21+
fid = c.files.create(file=("aa.txt", buf), purpose="assistants").id
22+
c.vector_stores.files.create(vs.id, file_id=fid)
23+
24+
query = "color of abc"
25+
26+
res = c.vector_stores.search(vs.id, query=query, max_num_results=3, rewrite_query=False)
27+
28+
context = "\n\n".join([ci.text for r in res.data for ci in r.content])
29+
30+
completion = c.chat.completions.create(
31+
model=os.getenv("INFERENCE_MODEL", "ollama/llama3.2:3b"),
32+
messages=[
33+
{
34+
"role": "system",
35+
"content": "You are a helpful assistant",
36+
},
37+
{
38+
"role": "user",
39+
"content": f"Context:\n{context}\n\nQuestion: {query}",
40+
},
41+
],
42+
temperature=0.7,
43+
)
44+
45+
print(completion.choices[0].message.content)

0 commit comments

Comments
 (0)