After you configure provider access in Metorial, you can connect those tools to an LLM like ChatGPT.
In this guide, you'll learn how to build a chat-enabled app that automatically handles tool calls from your Metorial providers.
What you'll learn
- How to use a Metorial provider
- How to use the Metorial SDKs
Before you start
- Create a Metorial project
- Configure at least one provider or integration
- Create an API key
1. Install the SDKs
Run the installer for your language of choice:
2. Configure clients
Instantiate both clients with your API keys and your provider deployment ID.
import { Metorial } from 'metorial';
import { metorialOpenAI } from '@metorial/openai';
import OpenAI from 'openai';
let metorial = new Metorial({
apiKey: 'metorial_sk_io2h4...'
});
let openai = new OpenAI({
apiKey: '...your-openai-api-key...'
});
from metorial import Metorial, metorial_openai
from openai import AsyncOpenAI
metorial = Metorial(api_key="metorial_sk_io2h4...")
openai = AsyncOpenAI(api_key="...your-openai-api-key...")
3. Fetch your provider tools
Create a session that exposes your deployed provider tools.
let session = await metorial.connect({
adapter: metorialOpenAI.chatCompletions(),
providers: [
{ providerDeploymentId: '...your-provider-deployment-id...' }
]
});
let tools = session.tools();
session = await metorial.connect(
adapter=metorial_openai(),
providers=[{"provider_deployment_id": "...your-provider-deployment-id..."}],
)
tools = session.tools()
4. Send your first prompt
Kick off the loop by sending an initial message.
let messages = [
{ role: "user", content: "Summarize the README.md file of the metorial/websocket-explorer repository on GitHub." }
];
messages = [
{"role": "user", "content": "Summarize the README.md file of the metorial/websocket-explorer repository on GitHub."}
]
5. Loop & handle tool calls
- Send
messages to OpenAI, passing the tools.
- If the assistant response contains
tool_calls, invoke it:
let response = await openai.chat.completions.create({
model: 'gpt-4o',
messages,
tools
});
let choice = response.choices[0]!;
let toolCalls = choice.message.tool_calls;
let toolResults = await session.callTools(toolCalls);
response = await openai.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=session.tools(),
)
choice = response.choices[0]
tool_calls = choice.message.tool_calls
tool_results = await session.call_tools(tool_calls)
- Append both the tool call requests and their results to
messages.
- Repeat until the assistant's response has no more
tool_calls.
6. Display the final output
Once there are no more tool calls, your assistant's final reply is in:
console.log(choice.message.content);
print(choice.message.content)