A reader types “hey, how do I read a CSV in Python 3.11 without pandas?” into your documentation search. A useful rewrite is “read CSV Python 3.11 without pandas”. Drop two words at the end and the search points the reader toward exactly the dependency they wanted to avoid.
Keep the constraints in the search box
Give Qwen one job and a short output budget. Keep the original question alongside its rewrite. In the example below, a small guard catches a missing version or exclusion before the candidate reaches search.
Run the example locally
Start Ollama on your computer and run ollama pull qwen2.5:1.5b in a terminal. Save the script as example.py and run python example.py with Python 3. It uses the standard library. The first request may take longer while Ollama loads the model.
import json
from urllib.request import Request, urlopen
question = "hey, how do I read a CSV in Python 3.11 without pandas?"
payload = {
'model': 'qwen2.5:1.5b',
'messages': [
{'role': 'system', 'content': "Rewrite the input as one short search query. Preserve versions, names and exclusions exactly. Return only the query. Never answer the question."},
{'role': 'user', 'content': question},
],
'stream': False,
'options': {'temperature': 0, 'num_predict': 64},
}
request = Request('http://localhost:11434/api/chat',
data=json.dumps(payload).encode(),
headers={'Content-Type': 'application/json'})
with urlopen(request, timeout=120) as response:
result = json.load(response)['message']['content'].strip()
required = ('3.11', 'without pandas')
valid = result and all(term in result.lower() for term in required)
print(result if valid else question)Check the result
Try the same search with “using pandas” and “without pandas”. Inspect the first three results for each. Count a rewrite as useful only when the right documentation moves up and the constraint survives. The two literal checks here cover this example. A general search box needs checks derived from each input.
Further reading: official model card and license · Ollama chat API · more Python tutorials.
No comments:
Post a Comment