-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Agents - Vector stores files & file batches (#31906)
Co-authored-by: Grace Brigham <[email protected]>
- Loading branch information
1 parent
8239038
commit 074f6d9
Showing
12 changed files
with
795 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
60
sdk/ai/ai-projects/samples-dev/agents/vector_store_file_batches.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
sdk/ai/ai-projects/samples-dev/agents/vector_store_files.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.