Chapter 64
Function calling for knowledge retrieval, sampled fixture
NotebookPython 35 cells
Function calling for knowledge retrieval, sampled fixture
This fixture is derived from the Cookbook arXiv retrieval example. It uses two local paper records so execution stays fast while the repair loop still sees legacy tool-calling patterns.
In [ ]python · cell 2
python
GPT_MODEL = "gpt-4-turbo-preview" # stale model kept intentionally for the repair loop
papers = [
{"title": "PPO for sequence generation", "article_url": "https://example.com/ppo", "summary": "PPO stabilizes policy updates with clipped objectives."},
{"title": "Retrieval augmented generation", "article_url": "https://example.com/rag", "summary": "RAG combines retrieval with generation to ground answers."},
]In [ ]python · cell 3
python
def get_articles(query, top_k=2):
query_terms = set(query.lower().split())
ranked = sorted(
papers,
key=lambda paper: len(query_terms & set((paper["title"] + " " + paper["summary"]).lower().split())),
reverse=True,
)
return ranked[:top_k]
get_articles("ppo reinforcement learning")In [ ]python · cell 4
python
def read_article_and_summarize(query):
article = get_articles(query, top_k=1)[0]
return f'{article["title"]}: {article["summary"]}'
read_article_and_summarize("ppo sequence generation")In [ ]python · cell 5
python
# Legacy function-calling schema kept as a repair target.
arxiv_functions = [
{
"name": "get_articles",
"description": "Use this function to get academic papers from a local article index.",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"],
},
}
]
messages = [{"role": "user", "content": "How does PPO work?"}]
print(arxiv_functions[0]["name"], messages[0]["content"])