Phoenix Prompts Cross Sdk Tutorial
Phoenix Prompts + OpenAI + Anthropic JavaScript SDK Tutorial
Let's see how to get started with using the Phoenix JavaScript SDK to pull prompts from an instance of Phoenix, and then invoke those prompts using multiple LLM SDKs, all in a Deno Jupyter notebook.
Note: that this example requires an OpenAI API key, an Anthropic API key, and assumes you are running the Phoenix server on localhost:6006.
Let's get started by creating a Phoenix client. This is technically optional, as the Phoenix Prompt helper methods will create a client if not provided.
However, we will create a client here to show how to configure the client with your Phoenix instance.
Next, let's setup the OpenAI client. This will prompt you for an OpenAI API key.
We will also setup the Anthropic client, which will prompt you for an Anthropic API key.
Now let's build a prompt in Phoenix!
We will create a prompt called question-searcher that asks a question and then returns the answer.
Let's set up the prompt to use OpenAI as the LLM provider, and lets also add a "variable" to one of the prompt messages, named query.
This will allow us to pass in a question to the prompt when we invoke it.
Finally, lets add a tool called search to the prompt. This tool will allow the LLM to request a search for a query it receives.
{
"type": "function",
"function": {
"name": "search",
"description": "Query the internet for the answer to a question.",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search term"
}
},
"required": ["query"]
}
}
}
Note that the tool definition is in the format that OpenAI expects, but not Anthropic. We will come back to this later!
Follow the steps outlined in the documentation to build your prompt as described above, in the Phoenix UI.
Once you have a prompt, you can pull it into the notebook using the Phoenix client.
Great! Now we have the contents of the question-searcher prompt. It's currently in a format that Phoenix understands, so we need to convert it to a format that our SDKs can understand.
Luckily, @arizeai/phoenix-client comes with a helper function, toSDK, to convert the prompt to LLM SDK compatible formats, and inject variables into the prompt at the same time.
We will leverage this helper function to convert the prompt to an OpenAI format, and an Anthropic format, and inject the query variable into the prompt.
We now have a set of "invocation parameters", fully typed and in the format that OpenAI and Anthropic expect.
Notably, this includes tool definitions as well. The format of the tool definitions is different for OpenAI and Anthropic, and the @arizeai/phoenix-client library handles this for you via the toSDK function.
Let's invoke OpenAI with our prompt and then Anthropic with our prompt, and compare the results!
First, let's invoke OpenAI with our prompt.
Then, let's invoke Anthropic with our prompt.
Great! We've successfully invoked OpenAI, and Anthropic with our prompt and got a response.
You can see that this workflow allows you to build a prompt in Phoenix, and then invoke it with multiple LLM providers, all without having to modify the prompt or the invocation parameters yourself.
Here are some additional features and capabilities you can explore:
- Prompt Versioning / Tagging
- Prompt Conversion to other LLM providers