# Process each query dynamically.
for item in queries:
input_messages = [{"role": "user", "content": item["query"]}]
print("\n🌟--- Processing Query ---🌟")
print(f"🔍 **User Query:** {item['query']}")
# Call the Responses API with tools enabled and allow parallel tool calls.
response = client.responses.create(
model="gpt-4o",
input=[
{
"role": "system",
"content": "When prompted with a question, select the right tool to use based on the question.",
},
{"role": "user", "content": item["query"]},
],
tools=tools,
parallel_tool_calls=True,
)
print("\n✨ **Initial Response Output:**")
print(response.output)
# Determine if a tool call is needed and process accordingly.
if response.output:
tool_call = response.output[0]
if tool_call.type in ["web_search_preview", "function_call"]:
tool_name = tool_call.name if tool_call.type == "function_call" else "web_search_preview"
print(f"\n🔧 **Model triggered a tool call:** {tool_name}")
if tool_name == "PineconeSearchDocuments":
print("🔍 **Invoking PineconeSearchDocuments tool...**")
res = query_pinecone_index(client, index, MODEL, item["query"])
if res["matches"]:
best_match = res["matches"][0]["metadata"]
result = f"**Question:** {best_match.get('Question', 'N/A')}\n**Answer:** {best_match.get('Answer', 'N/A')}"
else:
result = "**No matching documents found in the index.**"
print("✅ **PineconeSearchDocuments tool invoked successfully.**")
else:
print("🔍 **Invoking simulated web search tool...**")
result = "**Simulated web search result.**"
print("✅ **Simulated web search tool invoked successfully.**")
# Append the tool call and its output back into the conversation.
input_messages.append(tool_call)
input_messages.append({"type": "function_call_output", "call_id": tool_call.call_id, "output": str(result)})
# Get the final answer incorporating the tool's result.
final_response = client.responses.create(
model="gpt-4o", input=input_messages, tools=tools, parallel_tool_calls=True
)
print("\n💡 **Final Answer:**")
print(final_response.output_text)
else:
# If no tool call is triggered, print the response directly.
print("💡 **Final Answer:**")
print(response.output_text)