Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/nikos/agent-toolkit' into nikos/…
Browse files Browse the repository at this point in the history
…agent-toolkit

# Conflicts:
#	packages/agent-toolkit/package.json
  • Loading branch information
nikosdouvlis committed Feb 11, 2025
2 parents 592773f + b7e5ea1 commit fa2a164
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
46 changes: 23 additions & 23 deletions packages/agent-toolkit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
> This SDK is recommended for testing purposes only unless you are confident in the agent's behavior and have implemented necessary security measures such as guardrails and best practices.
## Table of Contents

1. [Getting Started](#getting-started)
2. [Public APIs](#public-apis)
3. [Use with Vercel's AI SDK](#use-with-vercels-ai-sdk)
Expand Down Expand Up @@ -66,11 +67,13 @@ The toolkit offers the same tools and core APIs across frameworks, but their pub
For more details on each tool, refer to the framework-specific directories or the [Clerk BAPI documentation](https://clerk.com/docs/reference/backend-api).

### Prerequisites

- `ai-sdk`: `"^3.4.7 || ^4.0.0"`, or `langchain`: `"^0.3.6"`
- A Clerk application
- An API key for an AI model compatible with Langchain

### Example Repository

- [Clerk AI SDK Example](https://github.com/clerk/agent-toolkit-example)

## Use with Vercel's AI SDK
Expand All @@ -91,11 +94,11 @@ For more details on each tool, refer to the framework-specific directories or th

```typescript
// Import the helper from the ai-sdk path
import { createClerkToolkit } from "@clerk/agent-toolkit/ai-sdk";
import { openai } from "@ai-sdk/openai";
import { streamText } from "ai";
import { auth } from "@clerk/nextjs/server";
import { systemPrompt } from "@/lib/ai/prompts";
import { createClerkToolkit } from '@clerk/agent-toolkit/ai-sdk';
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
import { auth } from '@clerk/nextjs/server';
import { systemPrompt } from '@/lib/ai/prompts';

export const maxDuration = 30;

Expand All @@ -109,7 +112,7 @@ export async function POST(req: Request) {
const toolkit = await createClerkToolkit({ context: { userId } });

const result = streamText({
model: openai("gpt-4o"),
model: openai('gpt-4o'),
messages,
// Optional - inject session claims into the system prompt
system: toolkit.injectSessionClaims(systemPrompt),
Expand All @@ -126,8 +129,6 @@ export async function POST(req: Request) {

## Use with Langchain



1. Install the Clerk Agent Toolkit package:

```shell
Expand All @@ -144,12 +145,12 @@ export async function POST(req: Request) {

```typescript
// Import the helper from the langchain path
import { createClerkToolkit } from "@clerk/agent-toolkit/langchain";
import { ChatOpenAI } from "@langchain/openai";
import { auth } from "@clerk/nextjs/server";
import { HumanMessage, SystemMessage } from "@langchain/core/messages";
import { LangChainAdapter } from "ai";
import { systemPrompt } from "@/lib/ai/prompts";
import { createClerkToolkit } from '@clerk/agent-toolkit/langchain';
import { ChatOpenAI } from '@langchain/openai';
import { auth } from '@clerk/nextjs/server';
import { HumanMessage, SystemMessage } from '@langchain/core/messages';
import { LangChainAdapter } from 'ai';
import { systemPrompt } from '@/lib/ai/prompts';

export const maxDuration = 30;

Expand All @@ -161,8 +162,8 @@ export async function POST(req: Request) {
// Optional - scope the toolkit to a specific user
const toolkit = await createClerkToolkit({ context: { userId } });

const model = new ChatOpenAI({ model: "gpt-4o", temperature: 0 });
const model = new ChatOpenAI({ model: 'gpt-4o', temperature: 0 });

// Bind the tools you want to use to the model
const modelWithTools = model.bindTools(toolkit.users());

Expand All @@ -183,8 +184,7 @@ export async function POST(req: Request) {
const stream = await modelWithTools.stream(messages);
return LangChainAdapter.toDataStreamResponse(stream);
}

````
```

## Advanced Usage

Expand All @@ -193,21 +193,21 @@ export async function POST(req: Request) {
If you need to set the Clerk secret key dynamically or use different Clerk instances, pass a custom `clerkClient`:

```typescript
import { createClerkToolkit } from "@clerk/agent-toolkit/ai-sdk";
import { createClerkClient } from "@clerk/backend";
import { createClerkToolkit } from '@clerk/agent-toolkit/ai-sdk';
import { createClerkClient } from '@clerk/backend';

export async function POST(req: Request) {
// Create a new Clerk client
const clerkClient = createClerkClient({ secretKey: "sk_" });
const clerkClient = createClerkClient({ secretKey: 'sk_' });

// Instantiate a new Clerk toolkit with the custom client
const toolkit = await createClerkToolkit({ clerkClient });

// Use the toolkit as usual
const result = streamText({
model: openai("gpt-4o"),
model: openai('gpt-4o'),
messages,
tools: toolkit.users()
tools: toolkit.users(),
});
}
```
Expand Down
6 changes: 3 additions & 3 deletions packages/agent-toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
"license": "MIT",
"author": "Clerk",
"sideEffects": false,
"files": [
"dist"
],
"exports": {
"./langchain": {
"types": "./dist/langchain/index.d.ts",
Expand All @@ -30,6 +27,9 @@
"import": "./dist/ai-sdk/index.mjs"
}
},
"files": [
"dist"
],
"scripts": {
"build": "tsup --env.NODE_ENV production",
"clean": "rimraf ./dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/agent-toolkit/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {ToolkitContext} from "./types";
import type { ToolkitContext } from './types';

export const defaultToolkitContext: ToolkitContext = {
allowPrivateMetadata: false,
Expand Down

0 comments on commit fa2a164

Please sign in to comment.