The Assistant class holds the data plane methods for interacting with Assistants.

This class can be instantiated through a Pinecone object, and is used to interact with a specific assistant.

Example

 import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
const assistant = pc.assistant('assistant-name');

Hierarchy

  • Assistant

Constructors

  • Creates an instance of the Assistant class.

    Parameters

    • assistantName: string

      The name of the assistant.

    • config: PineconeConfiguration

      The Pinecone configuration object containing an API key and other configuration parameters needed for API calls.

    Returns Assistant

    Throws

    An error if no assistant name is provided.

Properties

_chat: ((options) => Promise<ChatModel>)

Type declaration

_chatCompletion: ((options) => Promise<ChatCompletionModel>)

Type declaration

_chatCompletionStream: ((options) => Promise<ChatStream<StreamedChatCompletionResponse>>)

Type declaration

_chatStream: ((options) => Promise<ChatStream<StreamedChatResponse>>)

Type declaration

_context: ((options) => Promise<ContextModel>)

Type declaration

_deleteFile: ((fileId) => Promise<void>)

Type declaration

    • (fileId): Promise<void>
    • Parameters

      • fileId: string

      Returns Promise<void>

_describeFile: ((fileId) => Promise<AssistantFileModel>)

Type declaration

_listFiles: ((options) => Promise<AssistantFilesList>)

Type declaration

_uploadFile: ((options) => Promise<AssistantFileModel>)

Type declaration

assistantName: string

Methods

  • Sends a message to the assistant and receives a response. Retries the request if the server fails.

    Parameters

    • options: ChatOptions

      A ChatOptions object containing the message and optional parameters to send to the assistant.

    Returns Promise<ChatModel>

    A promise that resolves to a ChatModel object containing the response from the assistant.

    Example

    import { Pinecone } from '@pinecone-database/pinecone';
    const pc = new Pinecone();
    const assistantName = 'test1';
    const assistant = pc.assistant(assistantName);
    const chatResp = await assistant.chat({messages: [{role: 'user', content: "What is the capital of France?"}]});
    // {
    // id: '000000000000000023e7fb015be9d0ad',
    // finishReason: 'stop',
    // message: {
    // role: 'assistant',
    // content: 'The capital of France is Paris.'
    // },
    // model: 'gpt-4o-2024-05-13',
    // citations: [ { position: 209, references: [Array] } ],
    // usage: { promptTokens: 493, completionTokens: 38, totalTokens: 531 }
    // }
  • Sends a message to the assistant and receives a response that is compatible with [OpenAI's Chat Completion API](https://platform.openai.com/docs/guides/text-generation. Retries the request if the server fails.

    Parameters

    • options: ChatOptions

      A ChatOptions object containing the message and optional parameters to send to an assistant.

    Returns Promise<ChatCompletionModel>

    A promise that resolves to a ChatCompletionModel object containing the response from the assistant.

    Example

    import { Pinecone } from '@pinecone-database/pinecone';
    const pc = new Pinecone();
    const assistantName = 'test1';
    const assistant = pc.assistant(assistantName);
    const chatCompletion = await assistant.chatCompletion({ messages: [{ role: 'user', content: 'What is the capital of France?' }]});
    console.log(chatCompletion);
    // {
    // id: "response_id",
    // choices: [
    // {
    // finishReason: "stop",
    // index: 0,
    // message: {
    // role: "assistant",
    // content: "The data mentioned is described as \"some temporary data\" [1].\n\nReferences:\n1. [test-chat.txt](https://storage.googleapis.com/knowledge-prod-files/your_file_resource) \n"
    // }
    // }
    // ],
    // model: "gpt-4o-2024-05-13",
    // usage: {
    // promptTokens: 371,
    // completionTokens: 19,
    // totalTokens: 390
    // }
    // }
  • Sends a message to the assistant and receives a streamed response as ChatStream. Response is compatible with [OpenAI's Chat Completion API](https://platform.openai.com/docs/guides/text-generation. Retries the request if the server fails.

    Parameters

    • options: ChatOptions

      A ChatOptions object containing the message and optional parameters to send to an assistant.

    Returns Promise<ChatStream<StreamedChatCompletionResponse>>

    A promise that resolves to a ChatStream.

    Example

    import { Pinecone } from '@pinecone-database/pinecone';
    const pc = new Pinecone();
    const assistantName = 'test1';
    const assistant = pc.assistant(assistantName);
    const chatStream = await assistant.chatCompletionStream({messages: [{role: 'user', content: "What is the capital of France?"}]});

    // stream the response and log each chunk
    for await (const chunk of newStream) {
    if (chunk.choices.length > 0 && chunk.choices[0].delta.content) {
    process.stdout.write(chunk.choices[0].delta.content);
    }
    }
    // { id: 'response_id', choices: [{ index: 0, delta: { role: 'assistant' }, finishReason: null }], model: 'gpt-4o-2024-05-13', usage: null }
    // { id: 'response_id', choices: [{ index: 0, delta: { content: 'The' }}, finishReason: null }], model: 'gpt-4o-2024-05-13', usage: null }
    // { id: 'response_id', choices: [{ index: 0, delta: { content: ' test' }}, finishReason: null }], model: 'gpt-4o-2024-05-13', usage: null }
    // { id: 'response_id', choices: [], model: 'gpt-4o-2024-05-13', usage: { promptTokens: 371, completionTokens: 48, totalTokens: 419 }}
  • Sends a message to the assistant and receives a streamed response as ChatStream. Retries the request if the server fails.

    Parameters

    • options: ChatOptions

      A ChatOptions object containing the message and optional parameters to send to the assistant.

    Returns Promise<ChatStream<StreamedChatResponse>>

    A promise that resolves to a ChatStream.

    Example

    import { Pinecone } from '@pinecone-database/pinecone';
    const pc = new Pinecone();
    const assistantName = 'test1';
    const assistant = pc.assistant(assistantName);
    const chatStream = await assistant.chatStream({ messages: [{ role: 'user', content: 'What is the capital of France?'}]});

    // stream the response and log each chunk
    for await (const chunk of newStream) {
    console.log(chunk);
    }
    // each chunk will have a variable shape depending on the type:
    // { type:"message_start", id:"response_id", model:"gpt-4o-2024-05-13", role:"assistant"}
    // { type:"content_chunk", id:"response_id", model:"gpt-4o-2024-05-13", delta:{ content:"The"}}
    // { type:"content_chunk", id:"response_id", model:"gpt-4o-2024-05-13", delta:{ content:" test"}}
    // { type:"message_end", id:"response_id", model:"gpt-4o-2024-05-13", finishReason:"stop",usage:{ promptTokens:371,completionTokens:48,totalTokens:419}}
  • Retrieves the context snippets used by an assistant during the retrieval process.

    Parameters

    Returns Promise<ContextModel>

    A promise that resolves to a Context object containing the context snippets.

    Example

    import { Pinecone } from '@pinecone-database/pinecone';
    const pc = new Pinecone();
    const assistantName = 'test1';
    const assistant = pc.assistant(assistantName);
    const response = await assistant.context({query: "What is the capital of France?"});
    console.log(response);
    // {
    // snippets: [
    // {
    // type: 'text',
    // content: 'The capital of France is Paris.',
    // score: 0.9978925,
    // reference: [Object]
    // },
    // ],
    // usage: { promptTokens: 527, completionTokens: 0, totalTokens: 527 }
    // }
  • Deletes a file uploaded to an assistant by ID.

    Parameters

    • fileId: string

    Returns Promise<void>

    A promise that resolves to void on success.

    Example

    import { Pinecone } from '@pinecone-database/pinecone';
    const pc = new Pinecone();
    const assistantName = 'test1';
    const assistant = pc.assistant(assistantName);
    const files = await assistant.listFiles();
    let fileId: string;
    if (files.files) {
    fileId = files.files[0].id;
    await assistant.deleteFile({fileId: fileId});
    }
  • Describes a file uploaded to an assistant.

    Parameters

    • fileId: string

    Returns Promise<AssistantFileModel>

    A promise that resolves to a AssistantFileModel object containing the file details.

    Example

    import { Pinecone } from '@pinecone-database/pinecone';
    const pc = new Pinecone();
    const assistantName = 'test1';
    const assistant = pc.assistant(assistantName);
    const files = await assistant.listFiles();
    let fileId: string;
    if (files.files) {
    fileId = files.files[0].id;
    } else {
    fileId = '';
    }
    const resp = await assistant.describeFile({fileId: fileId})
    console.log(resp);
    // {
    // name: 'test-file.txt',
    // id: '1a56ddd0-c6d8-4295-80c0-9bfd6f5cb87b',
    // metadata: undefined,
    // createdOn: 2025-01-06T19:14:21.969Z,
    // updatedOn: 2025-01-06T19:14:36.925Z,
    // status: 'Available',
    // percentDone: 1,
    // signedUrl: undefined,
    // errorMessage: undefined
    // }
  • Lists files (with optional filter) uploaded to an assistant.

    Parameters

    Returns Promise<AssistantFilesList>

    A promise that resolves to a AssistantFilesList object containing a list of files.

    Example

    import { Pinecone } from '@pinecone-database/pinecone';
    const pc = new Pinecone();
    const assistantName = 'test1';
    const assistant = pc.assistant(assistantName);
    const files = await assistant.listFiles({filter: {metadata: {key: 'value'}}});
    console.log(files);
    // {
    // files: [
    // {
    // name: 'temp-file.txt',
    // id: '1a56ddd0-c6d8-4295-80c0-9bfd6f5cb87b',
    // metadata: undefined,
    // createdOn: 2025-01-06T19:14:21.969Z,
    // updatedOn: 2025-01-06T19:14:36.925Z,
    // status: 'Available',
    // percentDone: 1,
    // signedUrl: undefined,
    // errorMessage: undefined
    // }
    // ]
    // }
  • Uploads a file to an assistant.

    Note: This method does not use the generated code from the OpenAPI spec.

    Parameters

    • options: UploadFileOptions

      A UploadFile object containing the file path and optional metadata.

    Returns Promise<AssistantFileModel>

    A promise that resolves to a AssistantFileModel object containing the file details.

    Example

    import { Pinecone } from '@pinecone-database/pinecone';
    const pc = new Pinecone();
    const assistantName = 'test1';
    const assistant = pc.assistant(assistantName);
    await assistant.uploadFile({path: "test-file.txt", metadata: {"test-key": "test-value"}})
    // {
    // name: 'test-file.txt',
    // id: '921ad74c-2421-413a-8c86-fca81ceabc5c',
    // metadata: { 'test-key': 'test-value' },
    // createdOn: Invalid Date, // Note: these dates resolve in seconds
    // updatedOn: Invalid Date,
    // status: 'Processing',
    // percentDone: null,
    // signedUrl: null,
    // errorMessage: null
    // }