Skip to content

Commit

Permalink
Agents - Vector stores files & file batches (#31906)
Browse files Browse the repository at this point in the history
Co-authored-by: Grace Brigham <[email protected]>
  • Loading branch information
GraceBrigham and Grace Brigham authored Nov 25, 2024
1 parent 8239038 commit 074f6d9
Show file tree
Hide file tree
Showing 12 changed files with 795 additions and 6 deletions.
27 changes: 27 additions & 0 deletions sdk/ai/ai-projects/review/ai-projects.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export type AgentsNamedToolChoiceTypeOutput = string;
// @public (undocumented)
export interface AgentsOperations {
cancelRun: (threadId: string, runId: string, requestParams?: OptionalRequestParameters) => Promise<ThreadRunOutput>;
cancelVectorStoreFileBatch: (vectorStoreId: string, batchId: string, requestParams?: OptionalRequestParameters) => Promise<VectorStoreFileBatchOutput>;
createAgent: (model: string, options?: Omit<CreateAgentOptions, "model">, requestParams?: OptionalRequestParameters) => Promise<AgentOutput>;
createMessage: (threadId: string, options: ThreadMessageOptions, requestParams?: OptionalRequestParameters) => Promise<ThreadMessageOutput>;
createRun: (threadId: string, assistantId: string, options?: Omit<CreateRunOptions, "assistant_id">, requestParams?: OptionalRequestParameters) => Promise<ThreadRunOutput>;
Expand All @@ -106,22 +107,29 @@ export interface AgentsOperations {
createThreadAndRun: (assistantId: string, options?: Omit<CreateAndRunThreadOptions, "assistant_id">, requestParams?: OptionalRequestParameters) => Promise<ThreadRunOutput>;
createThreadAndRunStreaming: (assistantId: string, options?: Omit<CreateAndRunThreadOptions, "assistant_id">, requestParams?: OptionalRequestParameters) => Promise<AgentEventMessageStream>;
createVectorStore: (options?: VectorStoreOptions, requestParams?: OptionalRequestParameters) => Promise<VectorStoreOutput>;
createVectorStoreFile: (vectorStoreId: string, options?: CreateVectorStoreFileOptions, requestParams?: OptionalRequestParameters) => Promise<VectorStoreFileOutput>;
createVectorStoreFileBatch: (vectorStoreId: string, options?: CreateVectorStoreFileBatchOptions, requestParams?: OptionalRequestParameters) => Promise<VectorStoreFileBatchOutput>;
deleteAgent: (assistantId: string, requestParams?: OptionalRequestParameters) => Promise<AgentDeletionStatusOutput>;
deleteFile: (fileId: string, requestParams?: OptionalRequestParameters) => Promise<FileDeletionStatusOutput>;
deleteThread: (threadId: string, requestParams?: OptionalRequestParameters) => Promise<ThreadDeletionStatusOutput>;
deleteVectorStore: (vectorStoreId: string, requestParams?: OptionalRequestParameters) => Promise<VectorStoreDeletionStatusOutput>;
deleteVectorStoreFile: (vectorStoreId: string, fileId: string, requestParams?: OptionalRequestParameters) => Promise<VectorStoreFileDeletionStatusOutput>;
getAgent: (assistantId: string, requestParams?: OptionalRequestParameters) => Promise<AgentOutput>;
getFile: (fileId: string, requestParams?: OptionalRequestParameters) => Promise<OpenAIFileOutput>;
getFileContent: (fileId: string, requestParams?: OptionalRequestParameters) => Promise<string>;
getRun: (threadId: string, runId: string, requestParams?: OptionalRequestParameters) => Promise<ThreadRunOutput>;
getRunStep: (threadId: string, runId: string, stepId: string, requestParams?: OptionalRequestParameters) => Promise<RunStepOutput>;
getThread: (threadId: string, requestParams?: OptionalRequestParameters) => Promise<AgentThreadOutput>;
getVectorStore: (vectorStoreId: string, requestParams?: OptionalRequestParameters) => Promise<VectorStoreOutput>;
getVectorStoreFile: (vectorStoreId: string, fileId: string, requestParams?: OptionalRequestParameters) => Promise<VectorStoreFileOutput>;
getVectorStoreFileBatch: (vectorStoreId: string, batchId: string) => Promise<VectorStoreFileBatchOutput>;
listAgents: (options?: ListQueryParameters, requestParams?: OptionalRequestParameters) => Promise<OpenAIPageableListOfAgentOutput>;
listFiles: (purpose?: FilePurpose, requestParams?: OptionalRequestParameters) => Promise<FileListResponseOutput>;
listMessages: (threadId: string, runId?: string, options?: ListQueryParameters, requestParams?: OptionalRequestParameters) => Promise<OpenAIPageableListOfThreadMessageOutput>;
listRuns: (threadId: string, options?: ListQueryParameters, requestParams?: OptionalRequestParameters) => Promise<OpenAIPageableListOfThreadRunOutput>;
listRunSteps: (threadId: string, runId: string, options?: ListQueryParameters, requestParams?: OptionalRequestParameters) => Promise<OpenAIPageableListOfRunStepOutput>;
listVectorStoreFileBatchFiles: (vectorStoreId: string, batchId: string, options?: ListQueryParameters & FileStatusFilter, requestParams?: OptionalRequestParameters) => Promise<OpenAIPageableListOfVectorStoreFileOutput>;
listVectorStoreFiles: (vectorStoreId: string, options?: ListQueryParameters & FileStatusFilter, requestParams?: OptionalRequestParameters) => Promise<OpenAIPageableListOfVectorStoreFileOutput>;
listVectorStores: (options?: ListQueryParameters, requestParams?: OptionalRequestParameters) => Promise<OpenAIPageableListOfVectorStoreOutput>;
modifyVectorStore: (vectorStoreId: string, options?: VectorStoreUpdateOptions, requestParams?: OptionalRequestParameters) => Promise<VectorStoreOutput>;
submitToolOutputsToRun: (threadId: string, runId: string, tool_outputs: Array<ToolOutput>, stream?: boolean | null, options?: OptionalRequestParameters) => Promise<ThreadRunOutput>;
Expand Down Expand Up @@ -323,6 +331,20 @@ export interface CreateRunOptions {
truncation_strategy?: TruncationObject | null;
}

// @public
export interface CreateVectorStoreFileBatchOptions {
chunkingStrategy?: VectorStoreChunkingStrategyRequest;
dataSources?: VectorStoreDataSource[];
fileIds?: string[];
}

// @public
export interface CreateVectorStoreFileOptions {
chunkingStrategy?: VectorStoreChunkingStrategyRequest;
dataSources?: Array<VectorStoreDataSource>;
fileId?: string;
}

// @public
export interface CredentialsApiKeyAuthOutput {
key: string;
Expand Down Expand Up @@ -500,6 +522,11 @@ export interface FileSearchToolResourceOutput {
// @public
export type FileStateOutput = string;

// @public
export interface FileStatusFilter {
filter?: VectorStoreFileStatusFilter;
}

// @public
export type Frequency = string;

Expand Down
55 changes: 55 additions & 0 deletions sdk/ai/ai-projects/samples-dev/agents/run_steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { AIProjectsClient } from "@azure/ai-projects";
import { DefaultAzureCredential } from "@azure/identity";
import * as dotenv from "dotenv";
dotenv.config();

const connectionString = process.env["AZURE_AI_PROJECTS_CONNECTION_STRING"] || "<endpoint>;<subscription>;<resource group>;<project>";

async function main(): Promise<void> {
const client = AIProjectsClient.fromConnectionString(connectionString, new DefaultAzureCredential());

// Create agent
const agent = await client.agents.createAgent("gpt-4o", { name: "my-agent", instructions: "You are a helpful agent" });
console.log(`Created agent, agent ID: ${agent.id}`);

// Create thread
const thread = await client.agents.createThread();
console.log(`Created thread, thread ID: ${thread.id}`);

// Create message
const message = await client.agents.createMessage(thread.id, { role: "user", content: "hello, world!" });
console.log(`Created message, message ID: ${message.id}`);

// Create run
let run = await client.agents.createRun(thread.id, agent.id);
console.log(`Created run, run ID: ${run.id}`);

// Wait for run to complete
while (["queued", "in_progress", "requires_action"].includes(run.status)) {
await new Promise(resolve => setTimeout(resolve, 1000));
run = await client.agents.getRun(thread.id, run.id);
console.log(`Run status: ${run.status}`);
}

// List run steps
const runSteps = await client.agents.listRunSteps(thread.id, run.id);
console.log(`Listed run steps, run ID: ${run.id}`);

// Get specific run step
const stepId = runSteps.data[0].id;
const step = await client.agents.getRunStep(thread.id, run.id, stepId);
console.log(`Retrieved run step, step ID: ${step.id}`);

// Clean up
await client.agents.deleteThread(thread.id);
console.log(`Deleted thread, thread ID: ${thread.id}`);
await client.agents.deleteAgent(agent.id);
console.log(`Deleted agent, agent ID: ${agent.id}`);
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
60 changes: 60 additions & 0 deletions sdk/ai/ai-projects/samples-dev/agents/vector_store_file_batches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { AIProjectsClient } from "@azure/ai-projects";
import { DefaultAzureCredential } from "@azure/identity";
import * as dotenv from "dotenv";
import { Readable } from "stream";
dotenv.config();

const connectionString = process.env["AZURE_AI_PROJECTS_CONNECTION_STRING"] || "<endpoint>>;<subscription>;<resource group>;<project>";

export async function main(): Promise<void> {
const client = AIProjectsClient.fromConnectionString(connectionString || "", new DefaultAzureCredential());

// Create vector store
const vectorStore = await client.agents.createVectorStore();
console.log(`Created vector store, vector store ID: ${vectorStore.id}`);

// Create and upload first file
const file1Content = "Hello, Vector Store!";
const readable1 = new Readable();
readable1.push(file1Content);
readable1.push(null); // end the stream
const file1 = await client.agents.uploadFile(readable1, "assistants", "vector-file1.txt");
console.log(`Uploaded file1, file ID: ${file1.id}`);

// Create and upload second file
const file2Content = "This is another file for the Vector Store!";
const readable2 = new Readable();
readable2.push(file2Content);
readable2.push(null); // end the stream
const file2 = await client.agents.uploadFile(readable2, "assistants", "vector-file2.txt");
console.log(`Uploaded file2, file ID: ${file2.id}`);

// Create vector store file batch
const vectorStoreFileBatch = await client.agents.createVectorStoreFileBatch(vectorStore.id, { fileIds: [file1.id, file2.id] });
console.log(`Created vector store file batch, vector store file batch ID: ${vectorStoreFileBatch.id}`);

// Retrieve vector store file batch
const _vectorStoreFileBatch = await client.agents.getVectorStoreFileBatch(vectorStore.id, vectorStoreFileBatch.id);
console.log(`Retrieved vector store file batch, vector store file batch ID: ${_vectorStoreFileBatch.id}`);

// List vector store files in the batch
const vectorStoreFiles = await client.agents.listVectorStoreFileBatchFiles(vectorStore.id, vectorStoreFileBatch.id);
console.log(`List of vector store files in the batch: ${vectorStoreFiles.data.map(f => f.id).join(", ")}`);

// Delete files
await client.agents.deleteFile(file1.id);
console.log(`Deleted file1, file ID: ${file1.id}`);
await client.agents.deleteFile(file2.id);
console.log(`Deleted file2, file ID: ${file2.id}`);

// Delete vector store
await client.agents.deleteVectorStore(vectorStore.id);
console.log(`Deleted vector store, vector store ID: ${vectorStore.id}`);
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
54 changes: 54 additions & 0 deletions sdk/ai/ai-projects/samples-dev/agents/vector_store_files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { AIProjectsClient } from "@azure/ai-projects";
import { DefaultAzureCredential } from "@azure/identity";
import * as dotenv from "dotenv";
import { Readable } from "stream";
dotenv.config();

const connectionString = process.env["AZURE_AI_PROJECTS_CONNECTION_STRING"] || "<endpoint>>;<subscription>;<resource group>;<project>";

export async function main(): Promise<void> {
const client = AIProjectsClient.fromConnectionString(connectionString || "", new DefaultAzureCredential());

// Create vector store
const vectorStore = await client.agents.createVectorStore();
console.log(`Created vector store, vector store ID: ${vectorStore.id}`);

// Create and upload file
const fileContent = "Hello, Vector Store!";
const readable = new Readable();
readable.push(fileContent);
readable.push(null); // end the stream
const file = await client.agents.uploadFile(readable, "assistants", "vector-file.txt");
console.log(`Uploaded file, file ID: ${file.id}`);

// Create vector store file
const vectorStoreFile = await client.agents.createVectorStoreFile(vectorStore.id, { fileId: file.id });
console.log(`Created vector store file, vector store file ID: ${vectorStoreFile.id}`);

// Retrieve vector store file
const _vectorStoreFile = await client.agents.getVectorStoreFile(vectorStore.id, vectorStoreFile.id);
console.log(`Retrieved vector store file, vector store file ID: ${_vectorStoreFile.id}`);

// List vector store files
const vectorStoreFiles = await client.agents.listVectorStoreFiles(vectorStore.id);
console.log(`List of vector store files: ${vectorStoreFiles.data.map(f => f.id).join(", ")}`);

// Delete vector store file
await client.agents.deleteVectorStoreFile(vectorStore.id, vectorStoreFile.id);
console.log(`Deleted vector store file, vector store file ID: ${vectorStoreFile.id}`);

// Delete file
await client.agents.deleteFile(file.id);
console.log(`Deleted file, file ID: ${file.id}`);

// Delete vector store
await client.agents.deleteVectorStore(vectorStore.id);
console.log(`Deleted vector store, vector store ID: ${vectorStore.id}`);
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Loading

0 comments on commit 074f6d9

Please sign in to comment.