#having trouble with fs.filestream in OpenAI

52 messages · Page 1 of 1 (latest)

old plover
#

https://platform.openai.com/docs/assistants/tools/file-search/quickstart

Following along I have this code

import OpenAI from "openai";
const openai = new OpenAI();
import path from 'path';
import fs from 'fs';

async function main() {
  const assistant = await openai.beta.assistants.create({
    name: "Financial Analyst Assistant",
    instructions: "You are an expert financial analyst. Use you knowledge base to answer questions about audited financial statements.",
    model: "gpt-4o",
    tools: [{ type: "file_search" }],
  });
}

main();

const fileStreams = ["oil.txt", "secret.txt"].map((path) =>
  fs.createReadStream(path),
);
console.log(fileStreams);

// Create a vector store including our two files.
const account = async () => {
  let vectorStore = await openai.beta.vectorStores.create({
    name: "Financial Statement",
  });
};

account();

//await openai.beta.vectorStores.fileBatches.uploadAndPoll(vectorStore.id, fileStreams)

fs and path are both underlined in red; so i'm not sure i've imported /installed them properly. Secret and oil are in the parent directory, as per the image

I get the following error:

return fs_1.default.createReadStream(path);
                    ^

TypeError: Cannot read properties of undefined (reading 'createReadStream')
at C:\Users\pitts\Desktop\Git\tester assistant\fileRead.js:62:25
at Array.map (<anonymous>)
at Object.<anonymous> (C:\Users\pitts\Desktop\Git\tester assistant\fileRead.js:61:45)

#

the goal for right now is just for consol.log to correctly log the file; the rest of the guide can come after

bright vigil
#

Please don't ping random people for help, I'm not even participating in this thread.

old plover
#

okay sorry

hearty parrot
#

fs and path are node builtin packages

#

you might need to do pnpm i -D @types/node

#

change the command for other package managers

#

@old plover

novel leaf
novel leaf
hearty parrot
novel leaf
#

(also this is a runtime error installing the types wouldn't fix it)

hearty parrot
#

Seems really strange to me

#

There is a run time error. But fixing the types is pretty a good place to start. Though at this point I don't even know if he is using Node 🤷🏻‍♂️

#

I guess bun provides the same packages? Deno?

#

I think Deno has a different API

old plover
#

I am using node

#

Two changes were needed the first import * as FS from 'fs'

Secondly the openai documentation is wrong

hearty parrot
#

OK. Install types, see if the types say your code is right or wrong, and if you still have trouble share your tsconfig.json and package.json "module" value please

old plover
#

I fixed it on my own

hearty parrot
#

Ah cool

old plover
hearty parrot
#

import path from 'path'
import fs from 'fs'

const fileStreams = ["oil.txt", "secret.txt"].map((path) =>
    fs.createReadStream(path),
)
console.log(fileStreams)
#

This works fine for me

#

So I think it probably has to do with package.json and tsconfig.json settings

old plover
#

The actual openai documentation, which is only known documentation on this one month old featue, is the much bigger issue though.

old plover
hearty parrot
#

I get Python docs here

old plover
hearty parrot
#

Oh yeah I see

old plover
#

Top right

#

But the node.js docs are incorrect at per that git thread

hearty parrot
#

I can't see any import fs there

#
const fileStreams = ["edgar/goog-10k.pdf", "edgar/brka-10k.txt"].map((path) =>
  fs.createReadStream(path),
);
 
// Create a vector store including our two files.
let vectorStore = await openai.beta.vectorStores.create({
  name: "Financial Statement",
});
 
await openai.beta.vectorStores.fileBatches.uploadAndPoll(vectorStore.id, fileStreams)
old plover
#

Well, you do need filestream since they use it; why they didn't include it who knows

hearty parrot
#

I think they assume you know how to import it

#

So I don't think we can say that the docs are wrong

old plover
#

UploadAndPoll() is used incorrectly in the docs though

hearty parrot
#

Maybe they didn't include it for this reason. They didn't want to take responsibility for different environments

old plover
#

Which is where i got stuck for almost an entire day

hearty parrot
#

Ah

old plover
#

I actually had the import * thing figured out for a while but forgot it again due to bad version controll on my part

#

The uploadAndPoll() issue had me scrambling and second guessing

old plover
hearty parrot
#

I guess the node types helped to show the problem? Or did they not help at all?

#

I think the types aren't always perfect on imports.

old plover
#

Uh in a sense it isolated the issue yeah

hearty parrot
#

ah

#

Sometimes it is better to do

import { createReadStream } from 'fs'

Then you are only importing the function you need, not everything else too.

#

Especially when bundling for the web, as a bundler can remove the rest of the module. But that's N/A in node.

novel leaf