Update Vercel AI SDK to support tools call (#2383)
This commit is contained in:
@@ -1,110 +0,0 @@
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
import { generateObject } from "ai";
|
||||
import { testConfig } from "../config/test-config";
|
||||
import { z } from "zod";
|
||||
|
||||
interface Provider {
|
||||
name: string;
|
||||
activeModel: string;
|
||||
apiKey: string | undefined;
|
||||
}
|
||||
|
||||
const provider: Provider = {
|
||||
name: "anthropic",
|
||||
activeModel: "claude-3-5-sonnet-20240620",
|
||||
apiKey: process.env.ANTHROPIC_API_KEY,
|
||||
}
|
||||
describe("ANTHROPIC Structured Outputs", () => {
|
||||
const { userId } = testConfig;
|
||||
let mem0: ReturnType<typeof testConfig.createTestClient>;
|
||||
jest.setTimeout(30000);
|
||||
|
||||
beforeEach(() => {
|
||||
mem0 = testConfig.createTestClient(provider);
|
||||
});
|
||||
|
||||
describe("ANTHROPIC Object Generation Tests", () => {
|
||||
// Test 1: Generate a car preference object
|
||||
it("should generate a car preference object with name and steps", async () => {
|
||||
const { object } = await generateObject({
|
||||
model: mem0(provider.activeModel, {
|
||||
user_id: userId,
|
||||
}),
|
||||
schema: z.object({
|
||||
car: z.object({
|
||||
name: z.string(),
|
||||
steps: z.array(z.string()),
|
||||
}),
|
||||
}),
|
||||
prompt: "Which car would I like?",
|
||||
});
|
||||
|
||||
expect(object.car).toBeDefined();
|
||||
expect(typeof object.car.name).toBe("string");
|
||||
expect(Array.isArray(object.car.steps)).toBe(true);
|
||||
expect(object.car.steps.every((step) => typeof step === "string")).toBe(true);
|
||||
});
|
||||
|
||||
// Test 2: Generate an array of car objects
|
||||
it("should generate an array of three car objects with name, class, and description", async () => {
|
||||
const { object } = await generateObject({
|
||||
model: mem0(provider.activeModel, {
|
||||
user_id: userId,
|
||||
}),
|
||||
output: "array",
|
||||
schema: z.object({
|
||||
name: z.string(),
|
||||
class: z.string(),
|
||||
description: z.string(),
|
||||
}),
|
||||
prompt: "Write name of three cars that I would like.",
|
||||
});
|
||||
|
||||
expect(Array.isArray(object)).toBe(true);
|
||||
expect(object.length).toBe(3);
|
||||
object.forEach((car) => {
|
||||
expect(car).toHaveProperty("name");
|
||||
expect(typeof car.name).toBe("string");
|
||||
expect(car).toHaveProperty("class");
|
||||
expect(typeof car.class).toBe("string");
|
||||
expect(car).toHaveProperty("description");
|
||||
expect(typeof car.description).toBe("string");
|
||||
});
|
||||
});
|
||||
|
||||
// Test 3: Generate an enum for movie genre classification
|
||||
it("should classify the genre of a movie plot", async () => {
|
||||
const { object } = await generateObject({
|
||||
model: mem0(provider.activeModel, {
|
||||
user_id: userId,
|
||||
}),
|
||||
output: "enum",
|
||||
enum: ["action", "comedy", "drama", "horror", "sci-fi"],
|
||||
prompt: 'Classify the genre of this movie plot: "A group of astronauts travel through a wormhole in search of a new habitable planet for humanity."',
|
||||
});
|
||||
|
||||
expect(object).toBeDefined();
|
||||
expect(object).toBe("sci-fi");
|
||||
});
|
||||
|
||||
// Test 4: Generate an object of car names without schema
|
||||
it("should generate an object with car names", async () => {
|
||||
const { object } = await generateObject({
|
||||
model: mem0(provider.activeModel, {
|
||||
user_id: userId,
|
||||
}),
|
||||
output: "no-schema",
|
||||
prompt: "Write name of 3 cars that I would like.",
|
||||
});
|
||||
|
||||
const carObject = object as { cars: string[] };
|
||||
|
||||
expect(carObject).toBeDefined();
|
||||
expect(Array.isArray(carObject.cars)).toBe(true);
|
||||
expect(carObject.cars.length).toBe(3);
|
||||
expect(carObject.cars.every((car) => typeof car === "string")).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
59
vercel-ai-sdk/tests/mem0-provider-tests/mem0-cohere.test.ts
Normal file
59
vercel-ai-sdk/tests/mem0-provider-tests/mem0-cohere.test.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
import { createMem0, retrieveMemories } from "../../src";
|
||||
import { generateText, LanguageModelV1Prompt } from "ai";
|
||||
import { testConfig } from "../../config/test-config";
|
||||
import { createCohere } from "@ai-sdk/cohere";
|
||||
|
||||
describe("COHERE MEM0 Tests", () => {
|
||||
const { userId } = testConfig;
|
||||
jest.setTimeout(30000);
|
||||
let mem0: any;
|
||||
|
||||
beforeEach(() => {
|
||||
mem0 = createMem0({
|
||||
provider: "cohere",
|
||||
apiKey: process.env.COHERE_API_KEY,
|
||||
mem0Config: {
|
||||
user_id: userId
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("should retrieve memories and generate text using COHERE provider", async () => {
|
||||
const messages: LanguageModelV1Prompt = [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ type: "text", text: "Suggest me a good car to buy." },
|
||||
{ type: "text", text: " Write only the car name and it's color." },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
const { text } = await generateText({
|
||||
// @ts-ignore
|
||||
model: mem0("command-r-plus"),
|
||||
messages: messages
|
||||
});
|
||||
|
||||
// Expect text to be a string
|
||||
expect(typeof text).toBe('string');
|
||||
expect(text.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("should generate text using COHERE provider with memories", async () => {
|
||||
const prompt = "Suggest me a good car to buy.";
|
||||
|
||||
const { text } = await generateText({
|
||||
// @ts-ignore
|
||||
model: mem0("command-r-plus"),
|
||||
prompt: prompt
|
||||
});
|
||||
|
||||
expect(typeof text).toBe('string');
|
||||
expect(text.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
60
vercel-ai-sdk/tests/mem0-provider-tests/mem0-groq.test.ts
Normal file
60
vercel-ai-sdk/tests/mem0-provider-tests/mem0-groq.test.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
import { createMem0, retrieveMemories } from "../../src";
|
||||
import { generateText, LanguageModelV1Prompt } from "ai";
|
||||
import { testConfig } from "../../config/test-config";
|
||||
import { createGroq } from "@ai-sdk/groq";
|
||||
|
||||
describe("GROQ MEM0 Tests", () => {
|
||||
const { userId } = testConfig;
|
||||
jest.setTimeout(30000);
|
||||
|
||||
let mem0: any;
|
||||
|
||||
beforeEach(() => {
|
||||
mem0 = createMem0({
|
||||
provider: "groq",
|
||||
apiKey: process.env.GROQ_API_KEY,
|
||||
mem0Config: {
|
||||
user_id: userId
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("should retrieve memories and generate text using GROQ provider", async () => {
|
||||
const messages: LanguageModelV1Prompt = [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ type: "text", text: "Suggest me a good car to buy." },
|
||||
{ type: "text", text: " Write only the car name and it's color." },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
const { text } = await generateText({
|
||||
// @ts-ignore
|
||||
model: mem0("llama3-8b-8192"),
|
||||
messages: messages
|
||||
});
|
||||
|
||||
// Expect text to be a string
|
||||
expect(typeof text).toBe('string');
|
||||
expect(text.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("should generate text using GROQ provider with memories", async () => {
|
||||
const prompt = "Suggest me a good car to buy.";
|
||||
|
||||
const { text } = await generateText({
|
||||
// @ts-ignore
|
||||
model: mem0("llama3-8b-8192"),
|
||||
prompt: prompt
|
||||
});
|
||||
|
||||
expect(typeof text).toBe('string');
|
||||
expect(text.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,7 @@ import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
import { generateObject } from "ai";
|
||||
import { testConfig } from "../config/test-config";
|
||||
import { testConfig } from "../../config/test-config";
|
||||
import { z } from "zod";
|
||||
|
||||
interface Provider {
|
||||
55
vercel-ai-sdk/tests/mem0-provider-tests/mem0-openai.test.ts
Normal file
55
vercel-ai-sdk/tests/mem0-provider-tests/mem0-openai.test.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
import { createMem0 } from "../../src";
|
||||
import { generateText, LanguageModelV1Prompt } from "ai";
|
||||
import { testConfig } from "../../config/test-config";
|
||||
|
||||
describe("OPENAI MEM0 Tests", () => {
|
||||
const { userId } = testConfig;
|
||||
jest.setTimeout(30000);
|
||||
let mem0: any;
|
||||
|
||||
beforeEach(() => {
|
||||
mem0 = createMem0({
|
||||
provider: "openai",
|
||||
apiKey: process.env.OPENAI_API_KEY,
|
||||
mem0Config: {
|
||||
user_id: userId
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("should retrieve memories and generate text using Mem0 OpenAI provider", async () => {
|
||||
const messages: LanguageModelV1Prompt = [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ type: "text", text: "Suggest me a good car to buy." },
|
||||
{ type: "text", text: " Write only the car name and it's color." },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
const { text } = await generateText({
|
||||
model: mem0("gpt-4-turbo"),
|
||||
messages: messages
|
||||
});
|
||||
|
||||
// Expect text to be a string
|
||||
expect(typeof text).toBe('string');
|
||||
expect(text.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("should generate text using openai provider with memories", async () => {
|
||||
const prompt = "Suggest me a good car to buy.";
|
||||
|
||||
const { text } = await generateText({
|
||||
model: mem0("gpt-4-turbo"),
|
||||
prompt: prompt
|
||||
});
|
||||
|
||||
expect(typeof text).toBe('string');
|
||||
expect(text.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
import { createMem0, retrieveMemories } from "../../src";
|
||||
import { generateText, LanguageModelV1Prompt } from "ai";
|
||||
import { testConfig } from "../../config/test-config";
|
||||
import { createAnthropic } from "@ai-sdk/anthropic";
|
||||
|
||||
describe("ANTHROPIC MEM0 Tests", () => {
|
||||
const { userId } = testConfig;
|
||||
jest.setTimeout(30000);
|
||||
|
||||
let mem0: any;
|
||||
|
||||
beforeEach(() => {
|
||||
mem0 = createMem0({
|
||||
provider: "anthropic",
|
||||
apiKey: process.env.ANTHROPIC_API_KEY,
|
||||
mem0Config: {
|
||||
user_id: userId
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("should retrieve memories and generate text using ANTHROPIC provider", async () => {
|
||||
const messages: LanguageModelV1Prompt = [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ type: "text", text: "Suggest me a good car to buy." },
|
||||
{ type: "text", text: " Write only the car name and it's color." },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
const { text } = await generateText({
|
||||
// @ts-ignore
|
||||
model: mem0("claude-3-haiku-20240307"),
|
||||
messages: messages,
|
||||
});
|
||||
|
||||
// Expect text to be a string
|
||||
expect(typeof text).toBe('string');
|
||||
expect(text.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("should generate text using ANTHROPIC provider with memories", async () => {
|
||||
const prompt = "Suggest me a good car to buy.";
|
||||
|
||||
const { text } = await generateText({
|
||||
// @ts-ignore
|
||||
model: mem0("claude-3-haiku-20240307"),
|
||||
prompt: prompt,
|
||||
});
|
||||
|
||||
expect(typeof text).toBe('string');
|
||||
expect(text.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
91
vercel-ai-sdk/tests/mem0-toolcalls.test.ts
Normal file
91
vercel-ai-sdk/tests/mem0-toolcalls.test.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
import { addMemories, createMem0 } from "../src";
|
||||
import { generateText, tool } from "ai";
|
||||
import { testConfig } from "../config/test-config";
|
||||
import { z } from "zod";
|
||||
|
||||
describe("Tool Calls Tests", () => {
|
||||
const { userId } = testConfig;
|
||||
jest.setTimeout(30000);
|
||||
|
||||
beforeEach(async () => {
|
||||
await addMemories([{
|
||||
role: "user",
|
||||
content: [{ type: "text", text: "I live in Mumbai" }],
|
||||
}], { user_id: userId });
|
||||
});
|
||||
|
||||
it("should Execute a Tool Call Using OpenAI", async () => {
|
||||
const mem0OpenAI = createMem0({
|
||||
provider: "openai",
|
||||
apiKey: process.env.OPENAI_API_KEY,
|
||||
mem0Config: {
|
||||
user_id: userId,
|
||||
},
|
||||
});
|
||||
|
||||
const result = await generateText({
|
||||
model: mem0OpenAI("gpt-4o"),
|
||||
tools: {
|
||||
weather: tool({
|
||||
description: "Get the weather in a location",
|
||||
parameters: z.object({
|
||||
location: z
|
||||
.string()
|
||||
.describe("The location to get the weather for"),
|
||||
}),
|
||||
execute: async ({ location }) => ({
|
||||
location,
|
||||
temperature: 72 + Math.floor(Math.random() * 21) - 10,
|
||||
}),
|
||||
}),
|
||||
},
|
||||
prompt: "What is the temperature in the city that I live in?",
|
||||
});
|
||||
|
||||
// @ts-ignore
|
||||
const text = result.response.messages[1].content[0].result.location;
|
||||
|
||||
// Expect text to be a string
|
||||
expect(typeof text).toBe("string");
|
||||
expect(text.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("should Execute a Tool Call Using Anthropic", async () => {
|
||||
const mem0Anthropic = createMem0({
|
||||
provider: "anthropic",
|
||||
apiKey: process.env.ANTHROPIC_API_KEY,
|
||||
mem0Config: {
|
||||
user_id: userId,
|
||||
},
|
||||
});
|
||||
|
||||
const result = await generateText({
|
||||
model: mem0Anthropic("claude-3-haiku-20240307"),
|
||||
tools: {
|
||||
weather: tool({
|
||||
description: "Get the weather in a location",
|
||||
parameters: z.object({
|
||||
location: z
|
||||
.string()
|
||||
.describe("The location to get the weather for"),
|
||||
}),
|
||||
execute: async ({ location }) => ({
|
||||
location,
|
||||
temperature: 72 + Math.floor(Math.random() * 21) - 10,
|
||||
}),
|
||||
}),
|
||||
},
|
||||
prompt: "What is the temperature in the city that I live in?",
|
||||
});
|
||||
|
||||
// @ts-ignore
|
||||
const text = result.response.messages[1].content[0].result.location;
|
||||
|
||||
// Expect text to be a string
|
||||
expect(typeof text).toBe("string");
|
||||
expect(text.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
@@ -4,7 +4,7 @@ import { testConfig } from "../config/test-config";
|
||||
|
||||
describe("Memory Core Functions", () => {
|
||||
const { userId } = testConfig;
|
||||
jest.setTimeout(10000);
|
||||
jest.setTimeout(20000);
|
||||
|
||||
describe("addMemories", () => {
|
||||
it("should successfully add memories and return correct format", async () => {
|
||||
|
||||
@@ -57,7 +57,7 @@ describe.each(testConfig.providers)('TEXT/STREAM PROPERTIES: Tests with model %s
|
||||
text, // combined text
|
||||
usage, // combined usage of all steps
|
||||
} = await generateText({
|
||||
model: mem0(provider.activeModel), // Ensure the model name is correct
|
||||
model: mem0.completion(provider.activeModel), // Ensure the model name is correct
|
||||
maxSteps: 5, // Enable multi-step calls
|
||||
experimental_continueSteps: true,
|
||||
prompt:
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
import { retrieveMemories } from "../src";
|
||||
import { retrieveMemories } from "../../src";
|
||||
import { generateText, LanguageModelV1Prompt } from "ai";
|
||||
import { testConfig } from "../config/test-config";
|
||||
import { testConfig } from "../../config/test-config";
|
||||
import { createAnthropic } from "@ai-sdk/anthropic";
|
||||
|
||||
describe("ANTHROPIC Functions", () => {
|
||||
describe("ANTHROPIC Integration Tests", () => {
|
||||
const { userId } = testConfig;
|
||||
jest.setTimeout(30000);
|
||||
|
||||
@@ -36,7 +36,7 @@ describe("ANTHROPIC Functions", () => {
|
||||
// @ts-ignore
|
||||
model: anthropic("claude-3-haiku-20240307"),
|
||||
messages: messages,
|
||||
system: memories,
|
||||
system: memories.length > 0 ? memories : "No Memories Found"
|
||||
});
|
||||
|
||||
// Expect text to be a string
|
||||
@@ -52,7 +52,7 @@ describe("ANTHROPIC Functions", () => {
|
||||
// @ts-ignore
|
||||
model: anthropic("claude-3-haiku-20240307"),
|
||||
prompt: prompt,
|
||||
system: memories
|
||||
system: memories.length > 0 ? memories : "No Memories Found"
|
||||
});
|
||||
|
||||
expect(typeof text).toBe('string');
|
||||
@@ -1,12 +1,12 @@
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
import { retrieveMemories } from "../src";
|
||||
import { retrieveMemories } from "../../src";
|
||||
import { generateText, LanguageModelV1Prompt } from "ai";
|
||||
import { testConfig } from "../config/test-config";
|
||||
import { testConfig } from "../../config/test-config";
|
||||
import { createCohere } from "@ai-sdk/cohere";
|
||||
|
||||
describe("COHERE Functions", () => {
|
||||
describe("COHERE Integration Tests", () => {
|
||||
const { userId } = testConfig;
|
||||
jest.setTimeout(30000);
|
||||
let cohere: any;
|
||||
@@ -1,12 +1,12 @@
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
import { retrieveMemories } from "../src";
|
||||
import { retrieveMemories } from "../../src";
|
||||
import { generateText, LanguageModelV1Prompt } from "ai";
|
||||
import { testConfig } from "../config/test-config";
|
||||
import { testConfig } from "../../config/test-config";
|
||||
import { createGroq } from "@ai-sdk/groq";
|
||||
|
||||
describe("GROQ Functions", () => {
|
||||
describe("GROQ Integration Tests", () => {
|
||||
const { userId } = testConfig;
|
||||
jest.setTimeout(30000);
|
||||
|
||||
@@ -34,7 +34,7 @@ describe("GROQ Functions", () => {
|
||||
|
||||
const { text } = await generateText({
|
||||
// @ts-ignore
|
||||
model: groq("gemma2-9b-it"),
|
||||
model: groq("llama3-8b-8192"),
|
||||
messages: messages,
|
||||
system: memories,
|
||||
});
|
||||
@@ -50,7 +50,7 @@ describe("GROQ Functions", () => {
|
||||
|
||||
const { text } = await generateText({
|
||||
// @ts-ignore
|
||||
model: groq("gemma2-9b-it"),
|
||||
model: groq("llama3-8b-8192"),
|
||||
prompt: prompt,
|
||||
system: memories
|
||||
});
|
||||
@@ -1,12 +1,12 @@
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
import { retrieveMemories } from "../src";
|
||||
import { retrieveMemories } from "../../src";
|
||||
import { generateText, LanguageModelV1Prompt } from "ai";
|
||||
import { testConfig } from "../config/test-config";
|
||||
import { testConfig } from "../../config/test-config";
|
||||
import { createOpenAI } from "@ai-sdk/openai";
|
||||
|
||||
describe("OPENAI Functions", () => {
|
||||
describe("OPENAI Integration Tests", () => {
|
||||
const { userId } = testConfig;
|
||||
jest.setTimeout(30000);
|
||||
let openai: any;
|
||||
Reference in New Issue
Block a user