Execute one agent turn via Ollama. Parameters ---------- prompt: User message or OSINT target description. on_tool_call: Optional async callback — same signature as ``OpenOSINTAgent.run``. Returns ------- Agen
(
self,
prompt: str,
on_tool_call: Any = None,
)
| 607 | self.history = [] |
| 608 | |
| 609 | async def run( |
| 610 | self, |
| 611 | prompt: str, |
| 612 | on_tool_call: Any = None, |
| 613 | ) -> AgentResponse: |
| 614 | """ |
| 615 | Execute one agent turn via Ollama. |
| 616 | |
| 617 | Parameters |
| 618 | ---------- |
| 619 | prompt: |
| 620 | User message or OSINT target description. |
| 621 | on_tool_call: |
| 622 | Optional async callback — same signature as ``OpenOSINTAgent.run``. |
| 623 | |
| 624 | Returns |
| 625 | ------- |
| 626 | AgentResponse |
| 627 | Final text response and list of tool calls made. |
| 628 | """ |
| 629 | try: |
| 630 | import ollama # type: ignore |
| 631 | except ImportError: |
| 632 | return AgentResponse( |
| 633 | content="", |
| 634 | error=("'ollama' library is not installed. Install it with: pip install ollama"), |
| 635 | ) |
| 636 | |
| 637 | self.history.append({"role": "user", "content": prompt}) |
| 638 | messages: list[Any] = [ |
| 639 | {"role": "system", "content": SYSTEM_PROMPT}, |
| 640 | *self.history, |
| 641 | ] |
| 642 | ctx = _AgentRunContext(messages=messages, tool_calls=[], on_tool_call=on_tool_call) |
| 643 | |
| 644 | try: |
| 645 | client = ollama.AsyncClient(host=self.host) |
| 646 | while True: |
| 647 | response = await client.chat( |
| 648 | model=self.model, |
| 649 | messages=ctx.messages, |
| 650 | tools=_OLLAMA_TOOLS, |
| 651 | ) |
| 652 | msg = response.message |
| 653 | if not msg.tool_calls: |
| 654 | text = msg.content or "" |
| 655 | self.history.append({"role": "assistant", "content": text}) |
| 656 | return AgentResponse(content=text, tool_calls=ctx.tool_calls) |
| 657 | await _process_ollama_tool_turn(ctx, msg) |
| 658 | except Exception as exc: |
| 659 | logger.exception("Unexpected error in Ollama agent loop.") |
| 660 | return AgentResponse(content="", error=str(exc)) |