Skip to content

Commit 9e8c900

Browse files
authored
Node.js Open AI examples (#149)
* feat: added examples for nodejs * feat: added examples for nodejs * docs: updated README * docs: updated README
1 parent c155a7a commit 9e8c900

File tree

12 files changed

+425
-0
lines changed

12 files changed

+425
-0
lines changed

.gitignore

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,152 @@ dmypy.json
139139
# Pytorch checkpoint
140140
*.pth
141141
*.pt
142+
143+
# Node lock files since this is a example repo and they can be ignored
144+
package-lock.json
145+
yarn.lock
146+
147+
# Created by https://www.toptal.com/developers/gitignore/api/node
148+
# Edit at https://www.toptal.com/developers/gitignore?templates=node
149+
150+
### Node ###
151+
# Logs
152+
logs
153+
*.log
154+
npm-debug.log*
155+
yarn-debug.log*
156+
yarn-error.log*
157+
lerna-debug.log*
158+
.pnpm-debug.log*
159+
160+
# Diagnostic reports (https://nodejs.org/api/report.html)
161+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
162+
163+
# Runtime data
164+
pids
165+
*.pid
166+
*.seed
167+
*.pid.lock
168+
169+
# Directory for instrumented libs generated by jscoverage/JSCover
170+
lib-cov
171+
172+
# Coverage directory used by tools like istanbul
173+
coverage
174+
*.lcov
175+
176+
# nyc test coverage
177+
.nyc_output
178+
179+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
180+
.grunt
181+
182+
# Bower dependency directory (https://bower.io/)
183+
bower_components
184+
185+
# node-waf configuration
186+
.lock-wscript
187+
188+
# Compiled binary addons (https://nodejs.org/api/addons.html)
189+
build/Release
190+
191+
# Dependency directories
192+
node_modules/
193+
jspm_packages/
194+
195+
# Snowpack dependency directory (https://snowpack.dev/)
196+
web_modules/
197+
198+
# TypeScript cache
199+
*.tsbuildinfo
200+
201+
# Optional npm cache directory
202+
.npm
203+
204+
# Optional eslint cache
205+
.eslintcache
206+
207+
# Optional stylelint cache
208+
.stylelintcache
209+
210+
# Microbundle cache
211+
.rpt2_cache/
212+
.rts2_cache_cjs/
213+
.rts2_cache_es/
214+
.rts2_cache_umd/
215+
216+
# Optional REPL history
217+
.node_repl_history
218+
219+
# Output of 'npm pack'
220+
*.tgz
221+
222+
# Yarn Integrity file
223+
.yarn-integrity
224+
225+
# dotenv environment variable files
226+
.env
227+
.env.development.local
228+
.env.test.local
229+
.env.production.local
230+
.env.local
231+
232+
# parcel-bundler cache (https://parceljs.org/)
233+
.cache
234+
.parcel-cache
235+
236+
# Next.js build output
237+
.next
238+
out
239+
240+
# Nuxt.js build / generate output
241+
.nuxt
242+
dist
243+
244+
# Gatsby files
245+
.cache/
246+
# Comment in the public line in if your project uses Gatsby and not Next.js
247+
# https://nextjs.org/blog/next-9-1#public-directory-support
248+
# public
249+
250+
# vuepress build output
251+
.vuepress/dist
252+
253+
# vuepress v2.x temp and cache directory
254+
.temp
255+
256+
# Docusaurus cache and generated files
257+
.docusaurus
258+
259+
# Serverless directories
260+
.serverless/
261+
262+
# FuseBox cache
263+
.fusebox/
264+
265+
# DynamoDB Local files
266+
.dynamodb/
267+
268+
# TernJS port file
269+
.tern-port
270+
271+
# Stores VSCode versions used for testing VSCode extensions
272+
.vscode-test
273+
274+
# yarn v2
275+
.yarn/cache
276+
.yarn/unplugged
277+
.yarn/build-state.yml
278+
.yarn/install-state.gz
279+
.pnp.*
280+
281+
### Node Patch ###
282+
# Serverless Webpack directories
283+
.webpack/
284+
285+
# Optional stylelint cache
286+
287+
# SvelteKit build / generate output
288+
.svelte-kit
289+
290+
# End of https://www.toptal.com/developers/gitignore/api/node

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ dataset.upload_dataset(task="text_clf", split="train", module_dir="path_to_imdb_
4141
| Compute Orchestration | [CRUD Operations](ComputeOrchestration/crud_operations.ipynb) | Basic Functionalities (create, list, get, delete) of Compute Orchestration Classes - ComputeCluster, Nodepool, Deployment | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Clarifai/examples/blob/main/ComputeOrchestration/crud_operations.ipynb) |
4242
| Model Predict | [Predict](models/model_predict/README.md) | Call predict of any model in a [Python native way](models/model_predict/clarifai_llm.py) with our SDK. See many more examples [here](models/model_predict/) | [Docs for Python/Javascript](https://docs.clarifai.com/compute/models/inference/api) |
4343
| | [Using OpenAI Client](models/model_predict/openai_llm.py) | Use the openAI client to call openAI-compatible models in Clarifai. See many more examples [here](models/model_predict/). | [Docs for Pyhon/Typescript](https://docs.clarifai.com/compute/models/inference/open-ai) |
44+
| | [Using OpenAI Client on Node.js](nodejs/openai/getTextResponse.js) | Use the OpenAI node client to call OpenAI-compatible models in Clarifai. See many more examples [here](nodejs/openai/) | [Docs for Typescript](https://docs.clarifai.com/compute/models/inference/open-ai/) |
45+
| | [Using Vercel AI SDK on Node.js](nodejs/vercel-ai-sdk/getTextResponse.js) | Use the Vercel AI SDK with OpenAI provider to call OpenAI-compatible models in Clarifai. See many more examples [here](nodejs/vercel-ai-sdk/) | [Docs for Typescript](https://docs.clarifai.com/compute/models/inference/open-ai/) |
4446
| | [Using LiteLLM](models/model_predict/openai_llm.py) | Use Litellm to call openAI-compatible models in Clarifai. See many more examples [here](models/model_predict/) | |
4547
| | [Legacy Models](http://docs.clarifai.com/compute/models/inference/api-legacy/) | Call predict of any model not uploaded to compute orchestration with our SDK | [Docs](http://docs.clarifai.com/compute/models/inference/api-legacy/) |
4648
| Model Upload | [Model Upload](https://github.com/clarifai/runners-examples) | Upload custom models, MCP tools, or any python function you want. See our new runner examples repo for compute orchestration with many examples covered! | |

nodejs/openai/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Clarifai Open AI Example
2+
3+
Clarifai provides an OpenAI-compatible API endpoint, which allows you to leverage your existing OpenAI API code and workflows to make inferences with Clarifai models, including those that integrate or wrap OpenAI models.
4+
5+
The built-in compatibility layer converts your OpenAI calls directly into Clarifai API requests, letting you harness Clarifai's diverse models as custom tools in your OpenAI projects.
6+
7+
This simplifies the integration process, as you don't need to rewrite your code specifically for Clarifai's native API structure if you're already familiar with OpenAI's.
8+
9+
## Prerequisites
10+
11+
- A Clarifai account with API access
12+
- A Clarifai Personal Access Token (PAT)
13+
14+
## Setup
15+
16+
Clone this directory to your local machine using [degit](https://github.com/Rich-Harris/degit)
17+
18+
```bash
19+
degit clarifai/examples/nodejs/openai
20+
```
21+
22+
Install the required packages:
23+
24+
```bash
25+
npm install
26+
```
27+
28+
Add your Clarifai PAT to the environment variable `CLARIFAI_PAT`
29+
30+
```bash
31+
export CLARIFAI_PAT=your_pat_here
32+
```
33+
34+
or use a `.env` file to setup your environment variables.
35+
36+
## Notes
37+
38+
- The example uses Clarifai's GPT-4o model by default, but you can easily switch to other models
39+
- Make sure your Clarifai PAT has the necessary permissions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import OpenAI from "openai";
2+
3+
// Ensure you have the CLARIFAI_PAT environment variable set with your Clarifai Personal Access Token
4+
const client = new OpenAI({
5+
baseURL: "https://api.clarifai.com/v2/ext/openai/v1",
6+
apiKey: process.env.CLARIFAI_PAT,
7+
});
8+
9+
// Stream response from the model
10+
const streamingResponse = await client.chat.completions.create({
11+
model: "https://clarifai.com/openai/chat-completion/models/gpt-4o",
12+
messages: [
13+
{ role: "system", content: "You are a helpful assistant." },
14+
{ role: "user", content: "What is photosynthesis?" },
15+
],
16+
stream: true,
17+
});
18+
19+
for await (const part of streamingResponse) {
20+
if (part.choices?.[0]?.delta.content) {
21+
console.log(part.choices[0].delta.content);
22+
}
23+
}

nodejs/openai/getTextResponse.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import OpenAI from "openai";
2+
3+
// Ensure you have the CLARIFAI_PAT environment variable set with your Clarifai Personal Access Token
4+
const client = new OpenAI({
5+
baseURL: "https://api.clarifai.com/v2/ext/openai/v1",
6+
apiKey: process.env.CLARIFAI_PAT,
7+
});
8+
9+
// Example usage of the OpenAI client with Clarifai's GPT-4o model
10+
const response = await client.chat.completions.create({
11+
model: "https://clarifai.com/openai/chat-completion/models/gpt-4o",
12+
messages: [
13+
{ role: "system", content: "You are a helpful assistant." },
14+
{ role: "user", content: "What is photosynthesis?" },
15+
],
16+
});
17+
18+
console.log(response.choices?.[0]?.message.content);

nodejs/openai/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "openai",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"openai": "^5.0.1"
15+
}
16+
}

nodejs/openai/toolCalling.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import OpenAI from "openai";
2+
3+
// Ensure you have the CLARIFAI_PAT environment variable set with your Clarifai Personal Access Token
4+
const client = new OpenAI({
5+
baseURL: "https://api.clarifai.com/v2/ext/openai/v1",
6+
apiKey: process.env.CLARIFAI_PAT,
7+
});
8+
9+
// Example of using tools with the OpenAI client
10+
const tools = [
11+
{
12+
type: "function",
13+
function: {
14+
name: "get_weather",
15+
description: "Get current temperature for a given location.",
16+
parameters: {
17+
type: "object",
18+
properties: {
19+
location: {
20+
type: "string",
21+
description: "City and country e.g. Bogotá, Colombia",
22+
},
23+
},
24+
required: ["location"],
25+
additionalProperties: false,
26+
},
27+
strict: true,
28+
},
29+
},
30+
];
31+
32+
const toolCompletion = await client.chat.completions.create({
33+
model: "https://clarifai.com/openai/chat-completion/models/gpt-4o",
34+
messages: [
35+
{ role: "system", content: "You are a helpful assistant." },
36+
{ role: "user", content: "What is the weather in New York?" },
37+
],
38+
tools,
39+
});
40+
41+
console.log(toolCompletion.choices?.[0]?.message.tool_calls);

nodejs/vercel-ai-sdk/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Clarifai Vercel AI SDK Example
2+
3+
Clarifai provides an OpenAI-compatible API endpoint, which allows you to leverage your existing OpenAI API code and workflows to make inferences with Clarifai models, including those that integrate or wrap OpenAI models.
4+
5+
The built-in compatibility layer converts your OpenAI calls directly into Clarifai API requests, letting you harness Clarifai's diverse models as custom tools in your OpenAI projects.
6+
7+
This simplifies the integration process, as you don't need to rewrite your code specifically for Clarifai's native API structure if you're already familiar with OpenAI's.
8+
9+
The [Vercel AI SDK](https://ai-sdk.dev/) provides a convenient way to interact with Clarifai's OpenAI-compatible API. You can leverage the [OpenAI provider](https://ai-sdk.dev/providers/ai-sdk-providers/openai) to interact with Clarifai models.
10+
11+
## Prerequisites
12+
13+
- A Clarifai account with API access
14+
- A Clarifai Personal Access Token (PAT)
15+
16+
## Setup
17+
18+
Clone this directory to your local machine using [degit](https://github.com/Rich-Harris/degit)
19+
20+
```bash
21+
degit clarifai/examples/nodejs/vercel-ai-sdk
22+
```
23+
24+
Install the required packages:
25+
26+
```bash
27+
npm install
28+
```
29+
30+
Add your Clarifai PAT to the environment variable `CLARIFAI_PAT`
31+
32+
```bash
33+
export CLARIFAI_PAT=your_pat_here
34+
```
35+
36+
or use a `.env` file to setup your environment variables.
37+
38+
## Notes
39+
40+
- The example uses Clarifai's GPT-4o model by default, but you can easily switch to other models
41+
- Make sure your Clarifai PAT has the necessary permissions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createOpenAI } from "@ai-sdk/openai";
2+
import { streamText } from "ai";
3+
4+
const openai = createOpenAI({
5+
baseURL: "https://api.clarifai.com/v2/ext/openai/v1",
6+
apiKey: process.env.CLARIFAI_PAT,
7+
});
8+
9+
const model = openai(
10+
"https://clarifai.com/openai/chat-completion/models/gpt-4o",
11+
);
12+
13+
const stream = streamText({
14+
model,
15+
messages: [
16+
{ role: "system", content: "You are a helpful assistant." },
17+
{ role: "user", content: "What is photosynthesis?" },
18+
],
19+
});
20+
21+
for await (const part of stream.textStream) {
22+
console.log(part);
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createOpenAI } from "@ai-sdk/openai";
2+
import { generateText } from "ai";
3+
4+
const openai = createOpenAI({
5+
baseURL: "https://api.clarifai.com/v2/ext/openai/v1",
6+
apiKey: process.env.CLARIFAI_PAT,
7+
});
8+
9+
const model = openai(
10+
"https://clarifai.com/openai/chat-completion/models/gpt-4o",
11+
);
12+
13+
const { text } = await generateText({
14+
model,
15+
messages: [
16+
{ role: "system", content: "You are a helpful assistant." },
17+
{ role: "user", content: "What is photosynthesis?" },
18+
],
19+
});
20+
21+
console.log(text);

0 commit comments

Comments
 (0)