Hello,
I want to create a custom FAQ chatbot where I can make an assistant. Then train that assistant with links, adding questions and answers, and uploading any file types. The assistant will answer only from these trained items. Otherwise, it will reply to the users, I don’t know the answer. I can have multiple assistants and each assistant will have different data to be trained. I am trying using open AI API but not getting good results. Is there any way? Please help. Give me some suggestion which apis should I call step by step. Any help will be appreciated.
Hi @spaul1 and welcome to the community!
This is a very typical use case for Assistants API.
In the simplest sense you would:
- Upload documents/files that contain the background knowledge
- Specify the role of the assistant, including the rules for “conversation constraining” (so it only handles queries related to the uploaded knowledge)
- Use the resulting Assistant API in your web application
In practice of course it takes a bit of effort and refining to get this to work exactly as per your expectations.
When you say you are not getting good results - can you provide some examples of how it’s not working, and what your expectations are?
Hi, Can I create an FAQ using open AI API? I want to feed q & a from db, upload files and links then I want to ask questions from these files. Is it possible using open ai API and PHP?
basically, yes. that is one of the typical use case of the APIs.
if you want to do it on your own, read about Embeddings and Function Calling. these are the basic things you will need. here is a cookbook for Q&A (it is in python though).
then, there is the Assistants API. it allows you upload files. data from your DB will be submitted through function calling. it cannot use links though. you can only use links for image processing (vision). you can make your own function to scrape data from the link and pass it to the API using function calling. if you already have openAI API account, you can already test the Assistants API in the playground.
you can use PHP. if you read the API reference page, select curl from the dropdown, to see how you can interact with the APIs using curl. but there are also PHP libraries you can use.
if you have no openai api account yet, you will need to make one and pay a minimum of $5 prepaid for token usage to start using the API.
- creating vector store using https://api.openai.com/v1/vector_stores
- creating assistant using https://api.openai.com/v1/assistants passing verctor_store_id from previous API
‘tool_resources’ => [
‘file_search’ => [
‘vector_store_ids’ => [$vectorStoreId]
]
], - uploading files using https://api.openai.com/v1/files this API
- associateFileWithAssistant using this API https://api.openai.com/v1/assistants/{$assistant_id}/files
- storeFileInVectorStore using this API https://api.openai.com/v1/vector_stores/{$vectorStoreId}/file_batches
- associateFileWithVectorStore using https://api.openai.com/v1/vector_stores/{$vectorStoreId}/files
- then ask the question to assistant using https://api.openai.com/v1/chat/completions
but the file is not uploading in the assistant. what am I missing?
- creating vector store using https://api.openai.com/v1/vector_stores
- creating assistant using https://api.openai.com/v1/assistants passing verctor_store_id from previous API
‘tool_resources’ => [
‘file_search’ => [
‘vector_store_ids’ => [$vectorStoreId]
]
], - uploading files using https://api.openai.com/v1/files this API
- associateFileWithAssistant using this API https://api.openai.com/v1/assistants/{$assistant_id}/files
- storeFileInVectorStore using this API https://api.openai.com/v1/vector_stores/{$vectorStoreId}/file_batches
- associateFileWithVectorStore using https://api.openai.com/v1/vector_stores/{$vectorStoreId}/files
- then ask the question to assistant using https://api.openai.com/v1/chat/completions
but the file is not uploading in the assistant. what am I missing?
You’re getting stuck at step 4. Here’s the process worked for me:
- Create a vector store.
- Create an assistant, linking it to the vector store from step 1.
- Upload files to OpenAI’s storage.
- Assign the uploaded file to the vector store using its file ID.
- Ask questions to the assistant via thread, based on the files you’ve uploaded.
This sequence ensures proper file assignment and interaction with the assistant.
These are the initial steps you should follow when setting up the application. Once completed, you can simply upload files, assign them to the vector store, and create threads to ask the assistant questions using thread. There’s no need to create a new assistant or vector store each time you upload files.
Hi, I am getting an error when assigning a file to the vector store. I am sharing my postman error
"message": "Client error:
POST https://api.openai.com/v1/vector_stores/vs_MtE2KrNGSJZ0Li5Y46UNuG1p/files` resulted in a 400 Bad Request
response:\n{\n "error": {\n "message": "Files with extensions [.tmp] are not supported for retrieval. See https://platform.openai (truncated…)\n",`
After uploading JSONL and Txt whatever file upload I got .tmp in response like the below
{
“object”: “file”,
“id”: “file-Q0WJIzGl5Z5fisS0zP9F97Tk”,
“purpose”: “fine-tune”,
“filename”: “phpB5AF.tmp”,
“bytes”: 4782,
“created_at”: 1727446558,
“status”: “processed”,
“status_details”: null
}
do I need to call any other API to convert this file? I want to upload pdf, doc, csv, xlsx. but only jsonl is supported. what to do?
Hi, I am getting an error when assigning a file to the vector store. I am sharing my postman error
"message": "Client error:
POST [https://api.openai.com/v1/vector_stores/vs_MtE2KrNGSJZ0Li5Y46UNuG1p/files`](https://api.openai.com/v1/vector_stores/vs_MtE2KrNGSJZ0Li5Y46UNuG1p/files`) resulted in a 400 Bad Request
response:\n{\n “error”: {\n “message”: “Files with extensions [.tmp] are not supported for retrieval. See https://platform.openai (truncated…)\n”,`
do I need to call any other API to convert this file? I want to upload pdf, doc, csv, xlsx. but only jsonl is supported. what to do?
The Batch API only supports .jsonl
files up to 100 MB in size.
File Search option supports these many file formats.
You mentioned you’re using an assistant, so the purpose of the file should be set to “assistants”.
“purpose”: “assistants”
Regarding your question about why the file assignment to the vector store is failing, could you provide a screenshot of the API call where you’re attempting to upload and assign the file to the vector store?
this is for uploading file. currently I am unable to upload any other format.
And this is for assigning file to vector store
And after changing the purpose to assistants as per your suggestion getting this error even if I am giving csv file
The error message clearly states Invalid extension tmp
, which indicates you’re attempting to upload a .tmp
file, a format not supported by file search. I noticed the same in the first screenshot.
By the way, I was asking for a screenshot of the code where you made the API call, not only postman.
public function handleFAQBot(Request $request, $action)
{
$client = new Client();
$apiKey = env('OPENAI_API_KEY');
$response = null;
switch ($action) {
case 'create-vector-store':
// Call to create the vector store
$response = $client->post('https://api.openai.com/v1/vector_stores', [
'headers' => [
'Authorization' => 'Bearer ' . $apiKey,
'Content-Type' => 'application/json',
'OpenAI-Beta' => 'assistants=v2'
],
'json' => ['name' => 'Support FAQ']
]);
break;
case 'create-assistant':
// Call to create the assistant
$response = $client->post('https://api.openai.com/v1/assistants', [
'headers' => [
'Authorization' => 'Bearer ' . $apiKey,
'Content-Type' => 'application/json',
'OpenAI-Beta' => 'assistants=v2'
],
'json' => [
'instructions' => 'You are an HR bot...',
'tools' => [['type' => 'file_search']],
'tool_resources' => ['file_search' => ['vector_store_ids' => [$request->vector_store_id]]],
'model' => 'gpt-4o',
'name' => 'HR Helper'
]
]);
break;
case 'upload-file':
// Call to upload a file
$response = $client->post('https://api.openai.com/v1/files', [
'headers' => ['Authorization' => 'Bearer ' . $apiKey],
'multipart' => [
['name' => 'purpose', 'contents' => 'assistants'],
['name' => 'file', 'contents' => fopen($request->file('file'), 'r')]
]
]);
break;
case 'assign-file':
// Call to assign the file to the vector store
$response = $client->post('https://api.openai.com/v1/vector_stores/' . $request->vector_store_id . '/files', [
'headers' => [
'Authorization' => 'Bearer ' . $apiKey,
'Content-Type' => 'application/json',
'OpenAI-Beta' => 'assistants=v2'
],
'json' => ['file_id' => $request->file_id]
]);
break;
case 'ask-question':
// Call to ask the assistant a question
$response = $client->post('https://api.openai.com/v1/threads/runs', [
'headers' => [
'Authorization' => 'Bearer ' . $apiKey,
'Content-Type' => 'application/json',
'OpenAI-Beta' => 'assistants=v2'
],
'json' => [
'assistant_id' => $request->assistant_id,
'thread' => [
'messages' => [
['role' => 'user', 'content' => $request->question]
]
]
]
]);
break;
default:
return response()->json(['error' => 'Invalid action'], 400);
}
return response()->json(json_decode($response->getBody(), true));
}```
Here you go
Try passing filename
with ‘fine-tune’.
case 'upload-file':
$response = $client->post('https://api.openai.com/v1/files', [
'headers' => [
'Authorization' => 'Bearer ' . $apiKey,
],
'multipart' => [
[
'name' => 'purpose',
'contents' => 'fine-tune',
],
[
'name' => 'file',
'contents' => fopen($filePath, 'r'),
'filename' => $fileName,
]
]
]);
Are you able to see .tmp file in OpenAI store?
Hello, and welcome.
TMP is not a supported file type.
You will need to convert it to something else in order to load it to the VS.
Thanks. This resolved my issue. But is it possible to upload CSV, xlsx, and doc type files? and download that file again?