Using a Dataiku Agent in an external application#
This tutorial will show you how to call an Agent you created within Dataiku from your own application outside of Dataiku. You will learn how to connect to your Dataiku instance through the usage of the dataiku-api-client package.
Prerequisites#
First, you will need to build an Agent. You may follow the Creating and using a Code Agent tutorial. In that case, you will need the following environment:
Dataiku >= 13.4
An OpenAI connection
Python >= 3.10
A code environment with the following packages:
langchain_core # tested with 1.2.29 langchain # tested with 1.2.15 ddgs # tested with 9.13.1
To build the external application of this tutorial, you also need to install the dataiku-api-client package. You will find useful information in Installing the dataikuapi package
Gathering the needed information#
For your application to be able to connect to your Dataiku instance and work with your Agent, you will need to gather the following information and create corresponding environment variables:
DKU_DSS_URL: the URL of the Dataiku instance hosting the Agent.DKU_API_KEY: the API key you created for your Dataiku instance. Please see Public API Keys for more information.PROJECT_KEY: the identifier of your Project. This code snippet will list your projects keys.AGENT_ID: the identifier of your Agent. This code snippet can help you find it.
Note
If you use the code snippet to retrieve your AGENT_ID, be aware that the full ID will be in the form agent:AGENT_ID.
Only use the AGENT_ID part after the colon.
Calling the Agent from your application#
First, you will get all the values needed to connect to your Agent. Using environment variables keeps your secrets out of version control and lets you use the same code across environments, such as tests or a CI/CD system. For the purpose of this tutorial, let’s provide a prompt we will use in our query.
import os
REQUIRED_ENV_VARS = ("DKU_DSS_URL", "DKU_API_KEY", "PROJECT_KEY", "AGENT_ID")
CUSTOMER_ID = "fdouetteau"
PROMPT = (
f"Give all the professional information you can about the customer "
f"with ID: {CUSTOMER_ID}. Also include information about the company if you can."
)
env = {name: os.environ.get(name, "") for name in REQUIRED_ENV_VARS}
In the next step, you will connect to your Dataiku instance and get access to your Agent.
import dataikuapi
client = dataikuapi.DSSClient(env["DKU_DSS_URL"], env["DKU_API_KEY"])
agent = client.get_project(env["PROJECT_KEY"]).get_agent(env["AGENT_ID"])
At this point, you have a connection with your Agent in your Dataiku instance. We can now query the Agent using the hardcoded prompt provided as an example. Your application can compose a specific prompt adapted to suit your needs.
completion = agent.as_llm().new_completion()
completion.with_message(PROMPT, role="user")
response = completion.execute()
print(response.text)
This code can now be used to use a Dataiku Agent in your external application.
Complete code#
Here is the complete code of the application using your Agent. As a bonus to the main topic, you will find code to ensure environment variables have values and to report an issue if one or more variables are missing.
Code 4: Complete code of the application
import os
import sys
import dataikuapi
REQUIRED_ENV_VARS = ("DKU_DSS_URL", "DKU_API_KEY", "PROJECT_KEY", "AGENT_ID")
CUSTOMER_ID = "fdouetteau"
PROMPT = (
f"Give all the professional information you can about the customer "
f"with ID: {CUSTOMER_ID}. Also include information about the company if you can."
)
env = {name: os.environ.get(name, "") for name in REQUIRED_ENV_VARS}
missing_vars = [name for name, value in env.items() if not value]
if missing_vars:
print(
f"Missing required environment variable(s): {', '.join(missing_vars)}. "
"Please set them before running this script.",
file=sys.stderr,
)
sys.exit(1)
client = dataikuapi.DSSClient(env["DKU_DSS_URL"], env["DKU_API_KEY"])
agent = client.get_project(env["PROJECT_KEY"]).get_agent(env["AGENT_ID"])
completion = agent.as_llm().new_completion()
completion.with_message(PROMPT, role="user")
response = completion.execute()
print(response.text)
Wrapping up#
Congratulations! You now know how to connect to a Dataiku Agent from your own application. You will be able to compose the prompt in one part of your application and use the response in another part.
