Repository navigation
Expand file tree
/
Copy pathexample18.py
More file actions
145 lines (121 loc) · 4.45 KB
/
Copy pathexample18.py
File metadata and controls
145 lines (121 loc) · 4.45 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
Author: L. Saetta
Last modified: 2026-05-12
License: MIT
Description:
This example shows how-to query a Vector Store
using responses API.
"""
from common import (
extract_provider_name,
extract_text_and_refs,
get_inference_client,
get_sampling_kwargs,
print_example_summary,
print_runtime_config,
)
from config import MODEL_ID
from config_private import PROJECT_ID, VECTOR_STORE_ID
TEMPERATURE = 0.0
MAX_OUTPUT_TOKENS = 4096
def _get_header_case_insensitive(headers, header_name: str) -> str:
"""Read one header value with case-insensitive matching."""
if headers is None:
return ""
value = headers.get(header_name)
if value:
return str(value)
header_name = header_name.lower()
for key, item_value in headers.items():
if str(key).lower() == header_name:
return str(item_value)
return ""
def _extract_opc_request_id(raw_response) -> str:
"""Extract the OCI request id from a raw OpenAI-compatible response."""
headers = getattr(raw_response, "headers", None)
return _get_header_case_insensitive(headers, "opc-request-id") or (
_get_header_case_insensitive(headers, "opc-requestid")
)
def main() -> None:
"""Query a Vector Store."""
print_runtime_config()
print("")
print_example_summary("Query a vector store using responses API.")
print("")
if not VECTOR_STORE_ID:
print("VECTOR_STORE_ID is empty for the active profile.")
print("Set VECTOR_STORE_ID in the selected .env profile.")
return
client = get_inference_client()
# tho handle the issue with Gemini-2.5-pro that does not allow
# to use system role, we need to set the role of the instructions based on the provider
provider_name = extract_provider_name(MODEL_ID)
if provider_name == "google":
role_instructions = "user"
else:
role_instructions = "system"
# query = (
# "Summarize the retrieved documents about the impact of AI adoption "
# "on the labor market, focusing on employment, unemployment, wages, "
# "hiring, and which occupations appear most exposed."
# )
# query = "What is an HNSW index?"
# query = "Qual è la terapia di elezione per la cura della TBC?"
query = "What personal income tax rates does Article 6 establish?"
raw_response = client.responses.with_raw_response.create(
model=MODEL_ID,
**get_sampling_kwargs(MODEL_ID, temperature=TEMPERATURE),
max_output_tokens=MAX_OUTPUT_TOKENS,
input=[
{
# cannot use system if provider is google
"role": role_instructions,
"content": (
"Answer using only information from the retrieved documents. "
"You may summarize or synthesize information that is "
"explicitly supported by the retrieved text. "
"Do not use outside knowledge. "
"If the retrieved documents do not contain enough "
"information to answer, say exactly: "
"'I don't have sufficient information in the documents.'"
),
},
{"role": "user", "content": query},
],
tools=[
{
"type": "file_search",
"vector_store_ids": [VECTOR_STORE_ID],
"max_num_results": 10,
}
],
extra_headers={"OpenAI-Project": PROJECT_ID},
tool_choice="required",
include=["file_search_call.results"],
)
response = raw_response.parse()
opc_request_id = _extract_opc_request_id(raw_response)
text, refs = extract_text_and_refs(response)
print("")
print("Query: ", query)
print("")
print(text)
print("")
print("References:")
for r in refs:
print(f"[{r['n']}] {r['filename']} (pages={r['pages']})")
print("")
# debug information
if "don't have sufficient information" in text:
print("Debug info: ")
print(f"opc-request-id: {opc_request_id or '<not available>'}")
for i, item in enumerate(response.output):
print(f"\n--- item {i} ---")
print("type:", getattr(item, "type", None))
model_dump_json = getattr(item, "model_dump_json", None)
if callable(model_dump_json):
print(model_dump_json(indent=2))
else:
print(item)
if __name__ == "__main__":
main()