Skip to content

Commit

Permalink
local file sample changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary King committed Nov 18, 2024
1 parent 3940d0e commit 2c7f334
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
4 changes: 2 additions & 2 deletions sdk/ai/ai-projects/review/ai-projects.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import { ClientOptions } from '@azure-rest/core-client';
import { Paged } from '@azure/core-paging';
import { Pipeline } from '@azure/core-rest-pipeline';
import { ReadableStreamReader } from 'stream/web';
import { RequestParameters } from '@azure-rest/core-client';
import { StreamableMethod } from '@azure-rest/core-client';
import { TokenCredential } from '@azure/core-auth';

// @public
Expand Down Expand Up @@ -104,7 +104,7 @@ export interface AgentsOperations {
deleteVectorStore: (vectorStoreId: string, requestParams?: OptionalRequestParameters) => Promise<VectorStoreDeletionStatusOutput>;
getAgent: (assistantId: string) => Promise<AgentOutput>;
getFile: (fileId: string, requestParams?: OptionalRequestParameters) => Promise<OpenAIFileOutput>;
getFileContent: (fileId: string, requestParams?: OptionalRequestParameters) => Promise<ReadableStreamReader<string>>;
getFileContent: (fileId: string, requestParams?: OptionalRequestParameters) => Promise<StreamableMethod>;
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>;
Expand Down
18 changes: 13 additions & 5 deletions sdk/ai/ai-projects/samples-dev/agents/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,28 @@ export async function main(): Promise<void> {
const file = await client.agents.uploadFile(readable, "assistants", "my-file");
console.log(`Uploaded file, file ID : ${file.id}`);

// List files
// List uploaded files
const files = await client.agents.listFiles();

console.log(`List of files : ${files}`);
console.log(`List of files : ${files.data[0].filename}`);

// Retrieve file
const _file = await client.agents.getFile(file.id);

console.log(`Retrieved file, file ID : ${_file.id}`);

// Retrieve file content
const content = await client.agents.getFileContent(file.id);

console.log(`Retrieved file content : ${content.toString()}`);
const content = (await client.agents.getFileContent(file.id)).asNodeStream();
const chunks: Uint8Array[] = [];
let result;
while (!(result = await content.read(new Uint8Array(1024))).done) {
chunks.push(result.value);
}
const byteArray = Buffer.concat(chunks);
console.log(byteArray);
const decoder = new TextDecoder("utf-8");
const data = decoder.decode(byteArray);
console.log(`Retrieved file content : ${data}`);

// Delete file
await client.agents.deleteFile(file.id);
Expand Down
19 changes: 11 additions & 8 deletions sdk/ai/ai-projects/samples-dev/agents/local_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ export async function main(): Promise<void> {

const localContent = await client.agents.getFileContent(localFile.id);

let localContentString = '';
if (localContent instanceof Readable) {
for await (const chunk of localContent) {
localContentString += chunk;
}
} else {
localContentString = localContent.toString();
console.log(localContent.values());

const localChunks: Uint8Array[] = [];
let result;
while (!(result = await localContent.read(new Uint8Array(1024))).done) {
localChunks.push(result.value);
}
const byteArray = Buffer.concat(localChunks);
console.log(byteArray);
const decoder = new TextDecoder("utf-8");
const localData = decoder.decode(byteArray);

console.log(`Retrieved local file content: ${localContentString}`);
console.log(`Retrieved local file content: ${localData}`);

// Delete local file
await client.agents.deleteFile(localFile.id);
Expand Down
5 changes: 2 additions & 3 deletions sdk/ai/ai-projects/src/agents/files.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { Client, createRestError } from "@azure-rest/core-client";
import { Client, createRestError, StreamableMethod } from "@azure-rest/core-client";
import { FileDeletionStatusOutput, FileListResponseOutput, OpenAIFileOutput } from "../generated/src/outputModels.js";
import { DeleteFileParameters, GetFileContentParameters, GetFileParameters, ListFilesParameters, UploadFileParameters } from "../generated/src/parameters.js";
import { ReadableStreamReader } from "stream/web";

const expectedStatuses = ["200"];

Expand Down Expand Up @@ -67,7 +66,7 @@ export async function getFileContent(
context: Client,
fileId: string,
options?: GetFileContentParameters,
): Promise<ReadableStreamReader<string>> {
): Promise<StreamableMethod> {
const result = await context
.path("/files/{fileId}", fileId)
.get(options);
Expand Down
5 changes: 2 additions & 3 deletions sdk/ai/ai-projects/src/agents/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { Client } from "@azure-rest/core-client";
import { Client, StreamableMethod } from "@azure-rest/core-client";
import { AgentDeletionStatusOutput, AgentOutput, AgentThreadOutput, FileDeletionStatusOutput, FileListResponseOutput, OpenAIFileOutput, OpenAIPageableListOfAgentOutput, OpenAIPageableListOfRunStepOutput, OpenAIPageableListOfThreadMessageOutput, OpenAIPageableListOfThreadRunOutput, OpenAIPageableListOfVectorStoreOutput, RunStepOutput, ThreadDeletionStatusOutput, ThreadMessageOutput, ThreadRunOutput, VectorStoreDeletionStatusOutput, VectorStoreOutput } from "../generated/src/outputModels.js";
import { ListAgentsQueryParamProperties, ListMessagesQueryParamProperties, ListRunsQueryParamProperties, ListRunStepsQueryParamProperties, ListVectorStoresQueryParamProperties, SubmitToolOutputsToRunParameters } from "../generated/src/parameters.js";
import { createAgent, deleteAgent, getAgent, listAgents, updateAgent } from "./assistants.js";
Expand All @@ -16,7 +16,6 @@ import { UpdateMessageOptions } from "./messagesModels.js";
import { OptionalRequestParameters, UpdateRunOptions } from "./inputOutputs.js";
import { createVectorStore, deleteVectorStore, getVectorStore, listVectorStores, modifyVectorStore } from "./vectorStores.js";
import { getRunStep, listRunSteps } from "./runSteps.js";
import { ReadableStreamReader } from "stream/web";

export interface AgentsOperations {
/** Creates a new agent. */
Expand Down Expand Up @@ -157,7 +156,7 @@ export interface AgentsOperations {
getFileContent: (
fileId: string,
requestParams?: OptionalRequestParameters
) => Promise<ReadableStreamReader<string>>;
) => Promise<StreamableMethod>;

/** Returns a list of vector stores. */
listVectorStores: (
Expand Down

0 comments on commit 2c7f334

Please sign in to comment.