C3 AI Documentation Home

Dynamic Agent Few-shots

Few-shot prompts are examples that help the agent generate better answers by showing it how to respond to certain types of queries. These examples are part of the agent's prompt and guide its reasoning through in-context learning, allowing it to recognize patterns and produce more accurate responses.

After receiving a user query, the agent processes it through a series of well-defined steps—such as interpreting the question, retrieving relevant context, generating a response, and executing code if needed. Each step can be customized, allowing you to understand how the agent thinks and adapts. This visibility is enabled through step-level callbacks, which expose the agent’s intermediate reasoning and actions.

Prerequisites

Before you begin, ensure you can access Jupyter Notebook and the Application C3 AI Console.

Open Jupyter Notebook

Open Jupyter Notebook from the C3 Generative AI Application card:

  1. Navigate to your application in C3 AI Studio.
  2. Select Jupyter Notebook.
  3. Set the py-canvas runtime as the notebook kernel.

How few-shots are stored and retrieved

Memory is a dynamic agent feature which allows agents to remember important information across conversations. This memory is often implemented using embeddings, which are vector representations of text.

Those embeddings are stored in a vector store. When a new query comes in, the embedding vector of the query is compared semantically with the stored vectors in long-term memory.

In prompt engineering, you want to manage this memory and only retrieve the few-shot examples that are relevant. Once you retrieve the relevant few-shot examples, you should add them as context into the system prompt.

The memory manager of the dynamic agent is implemented in the Genai.Memory.DynamicAgentFewShot.Config Type:

Text
```python
# Default config
few_shot_manager = c3.Genai.Memory.DynamicAgentFewShot.Config.inst()
```

You can also access the embedder (turns text into embeddings) with:

Text
```python
few_shot_manager.embedder
```

You can access the vector store (stores the embeddings) with:

Text
```python
few_shot_manager.vectorStore
```

The fields of few-shot examples are:

  • displayText (required):
    • The string content display to the LLM agent when this example is retrieved.
    • User's queries are retrieved against this field if query is not provided for a few-shot example.
  • specialization (optional):
    • The specialization of the python agent.
    • This distinguishes between different agent specializations.
  • query (optional):
    • The query string that this example corresponds to. When provided, similarity search is conducted against this field instead of displayText, allowing optimization for retrieval without affecting the content shown to the agent. Enables alternative phrasings or keywords to improve matching.
  • description (optional):
    • Internal documentation for the example. Documents the purpose and context of the example, facilitates example management and organization, and helps track the intended use case for each example.

Add the few-shot example in the Dynamic agent's memory

To add few-shot examples, you should use the config interface Genai.Memory.DynamicAgentFewShot.Config#addMemories, which handles the vector encoding as well as the upsert.

  1. Add a few-shot example with a query, displayText, and an <execute> block. In this example, the LLM is instructed to return a greeting like "Hello, ma'am.
Python
query = "Greet me"
display_text = f"""User:
{query}
Assistant:
<execute>
print("Hello ma'am!")
</execute>"""

memory = {
    "id": "ingested_example_1",
    "displayText": display_text,
    "query": query,
    "description": "This is a demo example for adding few-shots for agent.",
    "specialization": "genericAnalysis",
}
  1. Add the memory to the few shot manager configuration.

    Python
    few_shot_manager.addMemories([memory])
  2. Confirm the memory was created.

    Python
    # Fetch the memory from the entity type
    c3.Genai.Memory.DynamicAgentFewShot.fetch()
  3. Test the retrieval of the few-shot example by query.

    Python
    query_string = "Greet me"
    query_result = agent.run(query=query_string)

    LLM output

  4. Clean up the few-shot examples.

    Python
    # Remove all DynamicAgentFewShot memories at once
    count = c3.Genai.Memory.DynamicAgentFewShot.removeAll({}, True)
    print(f"Removed {count} few-shot memories")

See also

Was this page helpful?