API should read through file, linked with URL

Hello everyone! Currently I am working on a school project which is using the OpenAI-API. Basically I am working on a website where you can upload files that are then used to generate a quiz about the file’s content.
At the moment, the users can upload files to a NodeJS server. Now I would like to call the API with an URL linked to a file, so that the API can use the file to generate JSON with Questions & Answers to the file’s content.
Summarized:

  1. Users uploads files to NodeJS-Server
  2. API-Call with an URL that leads to the uploaded files
  3. API returns JSON with Questions & Answers about the file’s content

So, how can I code this?

Welcome to the community!

That’s not really how it works, unfortunately. If you want to work with files, you will need to fiddle around with assistants.

Here are the docs to get you started on that:
https://platform.openai.com/docs/api-reference/assistants
here’s the playground if you wanna jump right in:
https://platform.openai.com/playground?mode=assistant

If I may suggest an alternative:

Using the chat/completion endpoint is much, much simpler if your documents fit into the context window. You’d just extract the text from your document, and send them to the endpoint as part of the context.

here’s the docs to that:
https://platform.openai.com/docs/api-reference/chat
playground:
https://platform.openai.com/playground?mode=chat


for returning JSON, you can try to use JSON mode (https://platform.openai.com/docs/guides/text-generation/json-mode)

However, json mode is not really necessary and ocasionally bugs out for unknowable reasons. It’s often enough just telling the model something like this:

You are part of a bigger system.
You must generate a pure json output. No chatter. Anything other than json with the provided schema will crash the system.

Format your response using this schema:

{
   question: string, // the question
   answer: string, // a true answer to the question
   traps: string[]  // 3 wrong answers that sound plausible
}[]

Begin your reply with {

do you think that’s enough to get started?

2 Likes

And IF you use chat completion and embed your documents into a vector store, you can then add the file URLs to these embeddings (in the metadata), so that each time a document is returned in your cosine similarity search, it also contains it’s URL link.