import json
import os
import requests
from openai import OpenAI
LIGHTON_API_KEY = os.environ["LIGHTON_API_KEY"]
INCEPTRON_API_KEY = os.environ["INCEPTRON_API_KEY"]
inceptron = OpenAI(
base_url="https://api.inceptron.io/v1",
api_key=INCEPTRON_API_KEY,
)
SEARCH_TOOL = {
"type": "function",
"function": {
"name": "lighton_search",
"description": (
"Search the company knowledge base for passages relevant to a query. "
"Returns ranked excerpts with their source filename and page numbers."
),
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Natural-language search query.",
},
"max_results": {
"type": "integer",
"description": "Number of passages to return (1–50, default 5).",
"default": 5,
},
},
"required": ["query"],
},
},
}
def run_search(query: str, max_results: int = 5) -> str:
response = requests.post(
"https://api.lighton.ai/api/v3/search",
headers={"Authorization": f"Bearer {LIGHTON_API_KEY}"},
json={"query": query, "max_results": max_results},
)
response.raise_for_status()
results = response.json()["results"]
passages = [
f"[{r['source']['filename']}, p.{r['source']['page_start']}]\n{r['content']}"
for r in results
if r["content"]
]
return "\n\n".join(passages) if passages else "No results found."
def answer(question: str, model: str = "nvidia/llama-3.3-70b-instruct-fp8") -> str:
messages = [{"role": "user", "content": question}]
while True:
completion = inceptron.chat.completions.create(
model=model,
tools=[SEARCH_TOOL],
messages=messages,
)
choice = completion.choices[0]
if choice.finish_reason == "tool_calls":
messages.append(choice.message)
for call in choice.message.tool_calls:
args = json.loads(call.function.arguments)
result = run_search(**args)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": result,
})
else:
return choice.message.content
print(answer("What is our data retention policy?"))