Dataiku Agents#
Tutorials
You can find tutorials on this subject in the Developer Guide: Agents and Tools for Generative AI.
Listing your agents#
The following code lists all the Agents and their IDs, which you can reuse in the code samples listed later on this page.
import dataiku
client = dataiku.api_client()
project = client.get_default_project()
llm_list = project.list_llms()
for llm in llm_list:
if 'agent:' in llm.id:
print(f"- {llm.description} (id: {llm.id})")
Using your agent#
Using the native DSSLLM
completion, for more information,
refer to Perform completion queries on LLMs:
import dataiku
AGENT_ID = "" # Fill with your agent id
client = dataiku.api_client()
project = client.get_default_project()
llm = project.get_llm(AGENT_ID)
completion = llm.new_completion()
resp = completion.with_message("How to run an agent?").execute()
if resp.success:
print(resp.text)
With the DKUChatModel
, for more information,
refer to LangChain integration:
AGENT_ID = "" # Fill with your agent id
langchain_llm = project.get_llm(AGENT_ID).as_langchain_chat_model()
resp = langchain_llm.invoke("How to run an agent?")
print(resp.content)
Streaming (in a notebook)#
from dataikuapi.dss.llm import DSSLLMStreamedCompletionChunk, DSSLLMStreamedCompletionFooter
from IPython.display import display, clear_output
AGENT_ID = "" # Fill with your agent id
client = dataiku.api_client()
project = client.get_default_project()
llm = project.get_llm(AGENT_ID)
completion = llm.new_completion()
completion.with_message("Who is the customer fdouetteau? Please provide additional information.")
gen = ""
for chunk in completion.execute_streamed():
if isinstance(chunk, DSSLLMStreamedCompletionChunk):
gen += chunk.data["text"]
clear_output()
display("Received text: %s" % gen)
elif isinstance(chunk, DSSLLMStreamedCompletionFooter):
print("Completion is complete: %s" % chunk.data)
from IPython.display import display, clear_output
AGENT_ID = "" # Fill with your agent id
langchain_llm = project.get_llm(AGENT_ID).as_langchain_chat_model()
resp = langchain_llm.stream("Who is the customer fdouetteau? Please provide additional information.")
gen = ""
for r in resp:
clear_output()
gen += r.content
display(gen)
Asynchronous Streaming (in a notebook)#
import asyncio
from IPython.display import display, clear_output
async def func(response):
gen = ""
async for r in response:
clear_output()
gen += r.content
display(gen)
AGENT_ID = "" # Fill with your agent id
langchain_llm = project.get_llm(AGENT_ID).as_langchain_chat_model()
resp = langchain_llm.astream("Who is the customer fdouetteau? Please provide additional information.")
await(func(resp))
Code Agents#
In Dataiku, you can implement a custom agent in code that leverages models from the LLM Mesh, LangChain, and its wider ecosystem.
The resulting agent becomes part of the LLM Mesh, seamlessly integrating into your AI workflows.
Dataiku includes basic code examples to help you get started. Below are more advanced samples that showcase full-fledged examples of agents built with LangChain and LangGraph. They both work with the internal code environment for retrieval-augmented generation to avoid any code env issue.
Note
To be able to use Code Agents you will need the Advanced LLM Mesh add-on.
This support agent is designed to handle customer inquiries efficiently. With its tools, it can:
retrieve relevant information from an FAQ database
log issues for follow-up when immediate answers aren’t available
escalate complex requests to a human agent when necessary.
We have tested it on this [Paris Olympics FAQ dataset](https://www.kaggle.com/datasets/sahityasetu/paris-2024-olympics-faq), which we used to create a knowledge bank with the Embed recipe. We have embedded a column containing both the question and the corresponding answer. Use the agent on inquiries like: How will transportation work in Paris during the Olympic Games? or I booked a hotel for the Olympic games in Paris, but never received any confirmation. What’s happening? and see how it reacts!
import dataiku
from dataiku.llm.python import BaseLLM
from dataiku.langchain import LangchainToDKUTracer
from langchain.tools import Tool
from langchain.agents import create_openai_tools_agent, AgentExecutor
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
## 1. Set Up Vector Search for FAQs
# Here, we are using a knowledge bank from the flow, build with our native Embed recipe.
# We make it a Langchain retriever and pass it to our first tool.
faq_retriever = dataiku.KnowledgeBank(id="<KD_ID>").as_langchain_retriever()
faq_retriever_tool = Tool(
name="FAQRetriever",
func=faq_retriever.get_relevant_documents,
description="Retrieves answers from the FAQ database based on user questions."
)
## 2. Define (fake) Ticketing & Escalation Tools
# Simulated ticket creation function
def create_ticket(issue: str):
# Here, you would typically use the API to your internal ticketing tool.
return f"Ticket created: {issue}"
ticketing_tool = Tool(
name="CreateTicket",
func=create_ticket,
description="Creates a support ticket when the issue cannot be resolved automatically."
)
# Simulated escalation function
def escalate_to_human(issue: str):
# This function could send a notification to the support engineers, for instance.
# It can be useful to attach info about the customer's request, sentiment, and history.
return f"Escalation triggered: {issue} has been sent to a human agent."
escalation_tool = Tool(
name="EscalateToHuman",
func=escalate_to_human,
description="Escalates the issue to a human when it's too complex, or the user is upset."
)
## 3. Build the LangChain Agent
# Define LLM for agent reasoning
llm = dataiku.api_client().get_default_project().get_llm("<valid:model:id_from_the_llm_mesh>").as_langchain_chat_model()
# Agent tools (FAQ retrieval + ticketing + escalation)
tools = [faq_retriever_tool, ticketing_tool, escalation_tool]
tool_names = [tool.name for tool in tools]
# Define the prompt
prompt = ChatPromptTemplate.from_messages(
[
("system",
"""You are an AI customer support agent. Your job is to assist users by:
- Answering questions using the FAQ retriever tool.
- Creating support tickets for unresolved issues.
- Escalating issues to a human when necessary."""),
MessagesPlaceholder("chat_history", optional=True),
("human", "{input}"),
MessagesPlaceholder("agent_scratchpad"),
]
)
# Initialize an agent with tools.
# Here, we define it as an agent that uses OpenAI tools.
# More options are available at https://python.langchain.com/api_reference/langchain/agents.html
agent = create_openai_tools_agent(llm=llm, tools=tools, prompt=prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)
agent_executor.invoke({"input": "How will transportation work in Paris during the Olympic Games?"})
class MyLLM(BaseLLM):
def __init__(self):
pass
def process(self, query, settings, trace):
prompt = query["messages"][0]["content"]
tracer = LangchainToDKUTracer(dku_trace=trace)
# Wrap the agent in an executor
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True)
response = agent_executor.invoke({"input": prompt}, config={"callbacks": [tracer]})
return {"text": response["output"]}
This data analysis agent is designed to automate insights from data.
Given a table (from an SQL database) and its schema (list of columns with information about what they contain), it can:
take a user question
translate it into an SQL query
run the query and fetch the result
interpret the result and convert it back into natural language.
The code below was written for [this dataset about car sales](https://www.kaggle.com/datasets/missionjee/car-sales-report). We used a Prepare recipe to remove some columns and parse the date to a proper format. Once implemented, test your agent with questions like: What were the top 5 best-selling car models in 2023? or What was the year-over-year evolution in the Scottsdale region regarding the number of sales?.
import dataiku
from dataiku.llm.python import BaseLLM
from dataiku.langchain import LangchainToDKUTracer
from langchain.prompts import ChatPromptTemplate
from dataiku import SQLExecutor2
from langgraph.graph import StateGraph, START, END
from typing_extensions import TypedDict
# Basic configuration
# Initialize LLM
llm = dataiku.api_client().get_default_project().get_llm("<valid:model:id_from_the_llm_mesh>").as_langchain_chat_model()
# Connect to the sales database
dataset = dataiku.Dataset("car_data_prepared_sql")
table_name = dataset.get_location_info().get('info', {}).get('table')
table_schema = """
- `Car_id` (TEXT): Unique car ID
- `Date` (DATE): Date of the sale
- `Dealer_Name` (TEXT): Name of the car dealer
- `Company` (TEXT): Company or brand of the car
- `Model` (TEXT): Model of the car
- `Transmission` (TEXT): Type of transmission in the car
- `Color` (TEXT): Color of the car's exterior
- `Price` (INTEGER): Listed price of the car sold
- `Body_Style` (TEXT): Style or design of the car's body
- `Dealer_Region` (TEXT): Geographic region of the car dealer
"""
# Here, we are adding a dispatcher as the first step of our graph. If the user query is not related to car sales,
# the agent will simply answer that it can't talk about anything else that car sales.
def dispatcher(state):
"""
Decides if the query is related to car sales data or just a general question.
Args:
state (dict): The current graph state
Returns:
str: Binary decision for the next node to call
"""
user_query = state["user_query"]
# Classification prompt
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a classifier that determines whether a user's query is related to car sales data.\n\n"
"The table contains information about car sales, including:\n"
"- Sale date & price\n"
"- Info about the cars (brand, model, transmission, body style & color) \n"
"- Dealer name and region\n\n"
"If the query is related to analyzing car sales data, return 'SQL'.\n"
"Otherwise, return 'GENERIC'."
),
(
"human", "{query}"
)
]
)
# Get the classification result
classification = llm.invoke(
prompt.format_messages(query=user_query)
).content.strip()
return classification
# First node, take the user input and translate it into a coherent SQL query.
def sql_translation(state):
"""
Translates a natural language query into SQL using the database schema.
Args:
state (dict): The current graph state that contains the user_query
Returns:
state (dict): New key added to state -- sql_query -- that contains the query to execute.
"""
print("---Translate to SQL---")
user_query = state["user_query"]
# We need to pass the model our table name and schema. Adapt instructions according to your needs, of course.
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are an AI assistant that converts natural language questions into SQL queries.\n\n"
"Here are the table name: {name} and schema:\n{schema}\n\n"
"Here are some important rules:\n"
"- Use correct table and column names\n"
"- Do NOT use placeholders (e.g., '?', ':param').\n"
"- The SQL should be executable in PostgreSQL, which means that table and column names should ALWAYS be double-quoted.\n"
"- Never return your answer with SQL Markdown decorators. Just the SQL query, nothing else."
),
(
"human",
"Convert the following natural language query into an SQL query:\n\n{query}"
)
]
)
# Invoke LLM with formatted prompt
sql_query = llm.invoke(
prompt.format_messages(name=table_name, schema=table_schema, query=user_query)
).content
return {"sql_query": sql_query}
# Second node, run the SQL query on the table. For this, we are using Dataiku's API.
def database_query(state):
"""
Executes the SQL query and retrieves results.
Args:
state (dict): The current graph state that contains the query to execute.
Returns:
state (dict): New key added to state -- query_result -- that contains the result of the query.
Returns an error key if not working.
"""
print("---Run SQL query---")
sql_query = state["sql_query"]
try:
executor = SQLExecutor2(dataset=dataset)
df = executor.query_to_df(sql_query)
return {"query_result": df.to_dict(orient="records")}
except Exception as e:
return {"error": str(e)}
# Third node, interpret the results and convert it back into natural language.
def result_interpreter(state):
"""
Takes the raw database output and converts it into a natural language response.
Args:
state (dict): The current graph state, that contains the result of the query (or an error if it didn't work)
Returns:
state (dict): New key added to state -- response -- that contains the final agent response.
"""
print("---Interpret results---")
query_result = state.get("query_result", [])
if not query_result:
return {"response": "No results were found, or the query failed."}
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are an AI assistant that summarizes findings from database results in a clear, human-readable format.\n"
),
(
"human", "{query_result}"
)
]
)
formatted_prompt = prompt.format_messages(query_result=query_result)
if len(formatted_prompt) > 1000:
return {"response": "The returned results were too long to be analyzed. Rephrase your query."}
summary = llm.invoke(formatted_prompt).content
return {"response": summary}
# On the other branch of our graph, if the question is too generic, the agent will just answer with a generic response.
def generic_response(state):
return {
"response": "I'm an agent specialized in car sales data analysis. I only have access to info like "
"sales date, price, car characteristics, and dealer name or region. "
"Ask me anything about car sales!"
}
class AgentState(TypedDict):
"""State object for the agent workflow."""
user_query: str
sql_query: str
query_result: list
response: str
# Create graph
graph = StateGraph(AgentState)
# Add nodes
graph.add_node("sql_translation", sql_translation)
graph.add_node("database_query", database_query)
graph.add_node("result_interpreter", result_interpreter)
graph.add_node("generic_response", generic_response)
# Define decision edges
graph.add_conditional_edges(
START,
dispatcher,
{
"SQL": "sql_translation", # If query is about sales, go to SQL path
"GENERIC": "generic_response" # Otherwise, respond with a generic answer
}
)
# Define SQL query flow
graph.add_edge("sql_translation", "database_query")
graph.add_edge("database_query", "result_interpreter")
graph.add_edge("result_interpreter", END)
class MyLLM(BaseLLM):
def __init__(self):
pass
def process(self, query, settings, trace):
prompt = query["messages"][0]["content"]
tracer = LangchainToDKUTracer(dku_trace=trace)
# Compile the graph
query_analyzer = graph.compile()
result = query_analyzer.invoke({"user_query": prompt}, config={"callbacks": [tracer]})
resp_text = result["response"]
sql_query = result.get("sql_query", [])
if not sql_query:
return {"text": resp_text}
# If the agent did succeed, then we return the final response, as well as the sql_query, for audit purposes.
full_resp_text = f"{resp_text}\n\nHere is the SQL query I ran:\n\n{sql_query}"
return {"text": full_resp_text}
Creating your code agent#
All code agents must implement the BaseLLM
class.
The BaseLLM
class is somewhat similar to this implementation:
class BaseLLM(BaseModel):
"""The base interface for a Custom LLM"""
# Implement this for synchronous answer
def process(self, query: SingleCompletionQuery, settings: CompletionSettings,
trace: SpanBuilder) -> CompletionResponse:
raise _NotImplementedError
# Implement this for asynchronous answer
async def aprocess(self, query: SingleCompletionQuery, settings: CompletionSettings,
trace: SpanBuilder) -> CompletionResponse:
raise _NotImplementedError
# Implement this for a streamed answer
def process_stream(self, query: SingleCompletionQuery, settings: CompletionSettings,
trace: SpanBuilder) -> Iterator[StreamCompletionResponse]:
raise _NotImplementedError
yield
# Implement this for a asynchronous streamed answer
async def aprocess_stream(self, query: SingleCompletionQuery,
settings: CompletionSettings, trace: SpanBuilder) -> \
AsyncIterator[StreamCompletionResponse]:
raise _NotImplementedError
yield
Generating an answer from a code agent follows the same rules, whether the agent is synchronous, streamed, or not.
Get an LLM (refer to Using your agent, for more details).
Process the input (and the settings).
Potentially manipulate the trace.
Invoke the LLM (refer to Using your agent, for more details).
Return the answer (refer to Using your agent, for more details).
2. Process the input without history#
If you want to deal with the last message, you should only take the last content
from the query,
as highlighted in the following code:
prompt = query["messages"][-1]["content"]
completion = llm.new_completion().with_message(prompt)
## .../...
llm_resp = completion.execute()
prompt = query["messages"][-1]["content"]
message = [HumanMessage(prompt)]
## .../...
llm_resp = llm.invoke(message)
2. Process the input with history#
If your intent is to use your agent conversationally, you may need to process the query to provide the whole context to the LLM.
completion = llm.new_completion()
for m in query.get('messages'):
completion.with_message(m.get('content'), m.get('role'))
## .../...
llm_resp = completion.execute()
messages = []
for m in query.get('messages'):
match m.get('role'):
case 'user':
messages.append(HumanMessage(m.get('content')))
case 'assistant':
messages.append(AIMessage(m.get('content')))
case 'system':
messages.append(SystemMessage(m.get('content')))
case 'tool':
messages.append(ToolMessage(m.get('content')))
case _:
logger.info('Unknown role', m.get('content'))
##.../...
llm.invoke(messages)
3. Adding trace information#
with trace.subspan("Invoke the LLM") as subspan:
ai_msg = llm.invoke(messages)
subspan.attributes['messages']= str(messages)
Extracting sources#
Suppose you have an agent that is querying a knowledge bank. You can retrieve the sources by using the following code snippet:
#AGENT_ID = "" # Fill with your agent id
langchain_llm = project.get_llm(AGENT_ID).as_langchain_chat_model()
messages = "What is the climate in 2024"
current_agent_response = langchain_llm.invoke(messages)
last_trace = current_agent_response.response_metadata.get('lastTrace', None)
attributes = last_trace.get('attributes')
completionResponse = attributes.get('completionResponse')
additionalInformation = completionResponse.get('additionalInformation')
sources = additionalInformation.get('sources')
for source in sources:
items = source.get('items')
for document in items:
print(document)
Running multiple asynchronous agents#
Sometimes, achieving a task requires running multiple agents. Some of the tasks handled by the agent can be run in parallel.
To run agents in parallel, you need to invoke agents asynchronously, and be sure that your agent/tool is async
compatible.
The ainvoke()
method allows the user to make an asynchronous call.
import asyncio
AGENT_ID = "" ## Fill you your agent ID (agent:xxxxxxxx)
langchain_llm = project.get_llm(AGENT_ID).as_langchain_chat_model()
async def queryA():
try:
print("Calling query A")
resp = langchain_llm.ainvoke("Give all the professional information you can about the customer with ID: fdouetteau. Also include information about the company if you can.")
response = await resp
return response
except:
return None
async def queryB():
try:
print("Calling query B")
resp = langchain_llm.ainvoke("Give all the professional information you can about the customer with ID: tcook. Also include information about the company if you can.")
response = await resp
return response
except:
return None
## Uncomment this if you are running into a notebook
# import nest_asyncio
# nest_asyncio.apply()
loop = asyncio.get_event_loop()
results = [asyncio.create_task(query) for query in [queryA(), queryB()]]
loop.run_until_complete(asyncio.wait(results))
for r in results:
if r.result() and r.result().content:
print(r.result().content)
from langchain_core.runnables import RunnableParallel
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
AGENT_ID = "" ## Fill you your agent ID (agent:xxxxxxxx)
ids = ["id1", "id2"] ## Use your data
langchain_llm = project.get_llm(AGENT_ID).as_langchain_chat_model()
local_prompt = PromptTemplate(
input_variables=["user_id"],
template="Give all the professional information you can about the customer with ID: {user_id}. Also include information about the company if you can."
)
runnable_map = {
f"chain_{i}": local_prompt.partial(user_id=ida) | langchain_llm
for i,ida in enumerate(ids)}
parallel_chain = RunnableParallel(runnable_map)
results = parallel_chain.invoke({})
for key, output in results.items():
print(f"{key}: {getattr(output, 'content', output)}")
Reference documentation#
Classes#
|
Entry point for the DSS API client |
|
Langchain-compatible wrapper around Dataiku-mediated chat LLMs |
|
A handle to interact with a DSS-managed LLM. |
A handle to interact with a completion query. |
|
|
A handle to interact with a project on the DSS instance. |
Functions#
Get a handle to the current default project, if available (i.e. |
|
Get a handle to interact with a specific LLM |
|
List the LLM usable in this project |
|
Create a new completion query. |
|
Run the completions query and retrieve the LLM response. |
|
Run the completion query and retrieve the LLM response as streamed chunks. |
|
|
Add a message to the completion query. |
|
|
|
|
|
|
|