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.
This document covers configuration for the CanvasAgent_default and CanvasAgent_deep_research named agents. The entity ids for these agents are default_canvas and deep_research. You can use the lowercase ids to initialize those agents or the names. All other agents are instances of Dynamic Agent Canvas or Dynamic Agent Deep Research, so you can follow the same steps for any agent instance.
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:
- Navigate to your application in C3 AI Studio.
- Select Jupyter Notebook.
- Set the
py-canvasruntime 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:
```python
# Default config
few_shot_manager = c3.Genai.Memory.DynamicAgentFewShot.Config.inst()
```You can also access the embedder (turns text into embeddings) with:
```python
few_shot_manager.embedder
```You can access the vector store (stores the embeddings) with:
```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
queryis 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.
- The query string that this example corresponds to. When provided, similarity search is conducted against this field instead of
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.
- 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.
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",
}Add the memory to the few shot manager configuration.
Pythonfew_shot_manager.addMemories([memory])Confirm the memory was created.
Python# Fetch the memory from the entity type c3.Genai.Memory.DynamicAgentFewShot.fetch()Test the retrieval of the few-shot example by query.
Pythonquery_string = "Greet me" query_result = agent.run(query=query_string)
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")