liveBook · Manning

Complete()

As explained in Build a Multi-Agent System (from Scratch)

complete() is a BaseLLM method that performs text-completion interaction with a language model.

`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

How Build a Multi-Agent System (from Scratch) covers it

How deep it goes
Defines the asynchronous prompt and result interface, then implements the Ollama adapter through generate().
Examples
Demonstrates an awaited joke prompt and prints the response with its original prompt.
What you can do after
Build an OllamaLLM completion adapter and apply it to a prompt using Ollama’s Python client.
How it connects
Distinguishes text completion from chat and structured output, and packages responses in CompleteResult.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Multi-Agent System (from Scratch). The section links point to the parts of the book it draws on.