`complete()` is the asynchronous BaseLLM method for completing a prompt with an LLM. It accepts a prompt string and returns a CompleteResult containing the generated response and the original prompt. BaseLLM provides a common interface that provider integrations can implement by subclassing it. section 3.1 section 3.2.1
Use `complete()` for simple prompt-to-text interaction. For a conversational dialogue represented as a sequence of messages, use `chat()`; tool calling is handled through chat interactions. For output conforming to a specified data format, use `structured_output()`. section 3.1 section 3.4
In OllamaLLM, `complete()` awaits `self._client.generate()`, passing the instance’s model, the prompt, and any additional keyword arguments. It converts the Ollama result into a CompleteResult, preserving the framework’s return type across the provider boundary. section 3.2.1
Listing 3.7 Implementing OllamaLLM: complete()
# llm_agents_from_scratch/llms/ollama/llm.py
from typing import Any
from ollama import AsyncClient
from llm_agents_from_scratch.base.llm import BaseLLM
from llm_agents_from_scratch.data_structures import (
CompleteResult,
)
class OllamaLLM(BaseLLM):
"""Ollama LLM class."""
… #A
async def complete(self, prompt: str, **kwargs: Any) -> CompleteResult:
"""Complete a prompt with an Ollama LLM.
Args:
prompt (str): The prompt to complete.
**kwargs (Any): Additional keyword arguments.
Returns:
CompleteResult: The text completion result.
"""
response = await self._client.generate( #B
model=self.model, #C
prompt=prompt, #D
**kwargs,
)
return CompleteResult(
response=response.response, #E
prompt=prompt,
)Inspect the asynchronous adapter from `complete()` to Ollama’s `generate()` and the construction of CompleteResult. section 3.2.1
Complete a prompt
Given a configured OllamaLLM instance `llm`, call `response = await llm.complete("Tell me a joke.")`. Printing the result displays both the generated joke and the prompt. section 3.2.1
