Build and return the Q&A agent.
(wiki_root: str, model: str, language: str = "en")
| 49 | |
| 50 | |
| 51 | def build_query_agent(wiki_root: str, model: str, language: str = "en") -> Agent: |
| 52 | """Build and return the Q&A agent.""" |
| 53 | schema_md = get_agents_md(Path(wiki_root)) |
| 54 | instructions = _QUERY_INSTRUCTIONS_TEMPLATE.format(schema_md=schema_md) |
| 55 | instructions += f"\n\nIMPORTANT: Answer in {language} language." |
| 56 | |
| 57 | @function_tool |
| 58 | def read_file(path: str) -> str: |
| 59 | """Read a Markdown file from the wiki. |
| 60 | Args: |
| 61 | path: File path relative to wiki root (e.g. 'summaries/paper.md'). |
| 62 | """ |
| 63 | return read_wiki_file(path, wiki_root) |
| 64 | |
| 65 | @function_tool |
| 66 | def get_page_content(doc_name: str, pages: str) -> str: |
| 67 | """Get text content of specific pages from a PageIndex (long) document. |
| 68 | Only use for documents with doc_type: pageindex. For short documents, |
| 69 | use read_file instead. |
| 70 | Args: |
| 71 | doc_name: Document name (e.g. 'attention-is-all-you-need'). |
| 72 | pages: Page specification (e.g. '3-5,7,10-12'). |
| 73 | """ |
| 74 | return get_wiki_page_content(doc_name, pages, wiki_root) |
| 75 | |
| 76 | @function_tool |
| 77 | def get_image(image_path: str) -> ToolOutputImage | ToolOutputText: |
| 78 | """View an image from the wiki. |
| 79 | |
| 80 | Use when a question asks about a specific figure, chart, or diagram |
| 81 | you'd need to see to answer accurately. |
| 82 | |
| 83 | Args: |
| 84 | image_path: Image path relative to wiki root (e.g. 'sources/images/doc/p1_img1.png'). |
| 85 | """ |
| 86 | result = read_wiki_image(image_path, wiki_root) |
| 87 | if result["type"] == "image": |
| 88 | return ToolOutputImage(image_url=result["image_url"]) |
| 89 | return ToolOutputText(text=result["text"]) |
| 90 | |
| 91 | from agents.model_settings import ModelSettings |
| 92 | |
| 93 | return Agent( |
| 94 | name="wiki-query", |
| 95 | instructions=instructions, |
| 96 | tools=[read_file, get_page_content, get_image], |
| 97 | model=f"litellm/{model}", |
| 98 | model_settings=ModelSettings( |
| 99 | parallel_tool_calls=False, |
| 100 | extra_headers=get_extra_headers() or None, |
| 101 | extra_args=get_timeout_extra_args(), |
| 102 | ), |
| 103 | ) |
| 104 | |
| 105 | |
| 106 | def build_chat_agent( |