From aa77d057212fa9ea659f4da43118900a36b8320d Mon Sep 17 00:00:00 2001 From: Enrique Barcelli Date: Sun, 31 Aug 2025 18:20:09 +0800 Subject: [PATCH] Add functionality for using vector storages and file uploads --- config-sample.py | 2 +- create_model_response.py | 1 + create_vector_store.py | 14 ++++++++++++++ create_vector_store_file.py | 23 +++++++++++++++++++++++ delete_file.py | 19 +++++++++++++++++++ delete_vector_store.py | 19 +++++++++++++++++++ delete_vector_store_file.py | 19 +++++++++++++++++++ list_files.py | 14 ++++++++++++++ list_vector_store_files.py | 19 +++++++++++++++++++ list_vector_stores.py | 14 ++++++++++++++ retrieve_file.py | 19 +++++++++++++++++++ retrieve_file_content.py | 19 +++++++++++++++++++ retrieve_vector_store.py | 19 +++++++++++++++++++ retrieve_vector_store_file.py | 19 +++++++++++++++++++ retrieve_vector_store_file_content.py | 19 +++++++++++++++++++ upload_file.py | 22 ++++++++++++++++++++++ 16 files changed, 260 insertions(+), 1 deletion(-) create mode 100644 create_vector_store.py create mode 100644 create_vector_store_file.py create mode 100644 delete_file.py create mode 100644 delete_vector_store.py create mode 100644 delete_vector_store_file.py create mode 100644 list_files.py create mode 100644 list_vector_store_files.py create mode 100644 list_vector_stores.py create mode 100644 retrieve_file.py create mode 100644 retrieve_file_content.py create mode 100644 retrieve_vector_store.py create mode 100644 retrieve_vector_store_file.py create mode 100644 retrieve_vector_store_file_content.py create mode 100644 upload_file.py diff --git a/config-sample.py b/config-sample.py index 06417fb..3ccff8e 100644 --- a/config-sample.py +++ b/config-sample.py @@ -1,4 +1,4 @@ base_url = "https://api.openai.com/v1/" secret_key = "YOUR-OPENAI-SECRET-KEY-HERE" - +vector_store = "YOUR-VECTOR-STORE-ID-HERE" diff --git a/create_model_response.py b/create_model_response.py index bb55188..62220a5 100644 --- a/create_model_response.py +++ b/create_model_response.py @@ -10,6 +10,7 @@ def create_model_response(conversation,model,message): "model": model, "tools": [ {"type": "web_search"}, + {"type": "file_search", "vector_store_ids" :[vector_store]} ], "input": message, "conversation": conversation diff --git a/create_vector_store.py b/create_vector_store.py new file mode 100644 index 0000000..16afae0 --- /dev/null +++ b/create_vector_store.py @@ -0,0 +1,14 @@ +from config import * +import requests + +url = base_url+"vector_stores" + +payload = {} +headers = { + 'Authorization': 'Bearer '+secret_key, +} + +response = requests.request("POST", url, headers=headers, data=payload) + +print(response.text) + diff --git a/create_vector_store_file.py b/create_vector_store_file.py new file mode 100644 index 0000000..3fb481a --- /dev/null +++ b/create_vector_store_file.py @@ -0,0 +1,23 @@ +from config import * +import requests +import json +import sys + +def create_vector_store_file(vector_store_id,file_id): + url = base_url+"vector_stores/"+vector_store_id+"/files" + + payload = json.dumps({ + "file_id": file_id + }) + headers = { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer '+secret_key, + } + + response = requests.request("POST", url, headers=headers, data=payload) + + print(response.text) + + +if __name__ == '__main__': + create_vector_store_file(str(sys.argv[1]),str(sys.argv[2])) diff --git a/delete_file.py b/delete_file.py new file mode 100644 index 0000000..78f1027 --- /dev/null +++ b/delete_file.py @@ -0,0 +1,19 @@ +from config import * +import requests +import sys + +def delete_file(file_id): + url = base_url+"files/"+file_id + + payload = {} + headers = { + 'Authorization': 'Bearer '+secret_key, + } + + response = requests.request("DELETE", url, headers=headers, data=payload) + + print(response.text) + + +if __name__ == '__main__': + delete_file(str(sys.argv[1])) diff --git a/delete_vector_store.py b/delete_vector_store.py new file mode 100644 index 0000000..df111e2 --- /dev/null +++ b/delete_vector_store.py @@ -0,0 +1,19 @@ +from config import * +import requests +import sys + +def delete_vector_store(vector_store): + url = base_url+"vector_stores/"+vector_store + + payload = {} + headers = { + 'Authorization': 'Bearer '+secret_key, + } + + response = requests.request("DELETE", url, headers=headers, data=payload) + + print(response.text) + + +if __name__ == '__main__': + delete_vector_store(str(sys.argv[1])) diff --git a/delete_vector_store_file.py b/delete_vector_store_file.py new file mode 100644 index 0000000..1215477 --- /dev/null +++ b/delete_vector_store_file.py @@ -0,0 +1,19 @@ +from config import * +import requests +import sys + +def delete_vector_store_file(vector_store,file_id): + url = base_url+"vector_stores/"+vector_store+"/files/"+file_id + + payload = {} + headers = { + 'Authorization': 'Bearer '+secret_key, + } + + response = requests.request("DELETE", url, headers=headers, data=payload) + + print(response.text) + + +if __name__ == '__main__': + delete_vector_store_file(str(sys.argv[1]),str(sys.argv[2])) diff --git a/list_files.py b/list_files.py new file mode 100644 index 0000000..b19653b --- /dev/null +++ b/list_files.py @@ -0,0 +1,14 @@ +from config import * +import requests + +url = base_url+"files" + +payload = {} +headers = { + 'Authorization': 'Bearer '+secret_key, +} + +response = requests.request("GET", url, headers=headers, data=payload) + +print(response.text) + diff --git a/list_vector_store_files.py b/list_vector_store_files.py new file mode 100644 index 0000000..141037a --- /dev/null +++ b/list_vector_store_files.py @@ -0,0 +1,19 @@ +from config import * +import requests +import sys + +def list_vector_store_files(vector_store): + url = base_url+"vector_stores/"+vector_store+"/files" + + payload = {} + headers = { + 'Authorization': 'Bearer '+secret_key, + } + + response = requests.request("GET", url, headers=headers, data=payload) + + print(response.text) + + +if __name__ == '__main__': + list_vector_store_files(str(sys.argv[1])) diff --git a/list_vector_stores.py b/list_vector_stores.py new file mode 100644 index 0000000..6cd4925 --- /dev/null +++ b/list_vector_stores.py @@ -0,0 +1,14 @@ +from config import * +import requests + +url = base_url+"vector_stores" + +payload = {} +headers = { + 'Authorization': 'Bearer '+secret_key, +} + +response = requests.request("GET", url, headers=headers, data=payload) + +print(response.text) + diff --git a/retrieve_file.py b/retrieve_file.py new file mode 100644 index 0000000..931be4a --- /dev/null +++ b/retrieve_file.py @@ -0,0 +1,19 @@ +from config import * +import requests +import sys + +def retrieve_file(file_id): + url = base_url+"files/"+file_id + + payload = {} + headers = { + 'Authorization': 'Bearer '+secret_key, + } + + response = requests.request("GET", url, headers=headers, data=payload) + + print(response.text) + + +if __name__ == '__main__': + retrieve_file(str(sys.argv[1])) diff --git a/retrieve_file_content.py b/retrieve_file_content.py new file mode 100644 index 0000000..dcaaae1 --- /dev/null +++ b/retrieve_file_content.py @@ -0,0 +1,19 @@ +from config import * +import requests +import sys + +def retrieve_file_content(file_id): + url = base_url+"files/"+file_id+"/content" + + payload = {} + headers = { + 'Authorization': 'Bearer '+secret_key, + } + + response = requests.request("GET", url, headers=headers, data=payload) + + print(response.text) + + +if __name__ == '__main__': + retrieve_file_content(str(sys.argv[1])) diff --git a/retrieve_vector_store.py b/retrieve_vector_store.py new file mode 100644 index 0000000..8b0ecc3 --- /dev/null +++ b/retrieve_vector_store.py @@ -0,0 +1,19 @@ +from config import * +import requests +import sys + +def retrieve_vector_store(vector_store): + url = base_url+"vector_stores/"+vector_store + + payload = {} + headers = { + 'Authorization': 'Bearer '+secret_key, + } + + response = requests.request("GET", url, headers=headers, data=payload) + + print(response.text) + + +if __name__ == '__main__': + retrieve_vector_store(str(sys.argv[1])) diff --git a/retrieve_vector_store_file.py b/retrieve_vector_store_file.py new file mode 100644 index 0000000..05a1ff2 --- /dev/null +++ b/retrieve_vector_store_file.py @@ -0,0 +1,19 @@ +from config import * +import requests +import sys + +def retrieve_vector_store_file(vector_store,file_id): + url = base_url+"vector_stores/"+vector_store+"/files/"+file_id + + payload = {} + headers = { + 'Authorization': 'Bearer '+secret_key, + } + + response = requests.request("GET", url, headers=headers, data=payload) + + print(response.text) + + +if __name__ == '__main__': + retrieve_vector_store_file(str(sys.argv[1]),str(sys.argv[2])) diff --git a/retrieve_vector_store_file_content.py b/retrieve_vector_store_file_content.py new file mode 100644 index 0000000..af01795 --- /dev/null +++ b/retrieve_vector_store_file_content.py @@ -0,0 +1,19 @@ +from config import * +import requests +import sys + +def retrieve_vector_store_file_content(vector_store,file_id): + url = base_url+"vector_stores/"+vector_store+"/files/"+file_id+"/content" + + payload = {} + headers = { + 'Authorization': 'Bearer '+secret_key, + } + + response = requests.request("GET", url, headers=headers, data=payload) + + print(response.text) + + +if __name__ == '__main__': + retrieve_vector_store_file_content(str(sys.argv[1]),str(sys.argv[2])) diff --git a/upload_file.py b/upload_file.py new file mode 100644 index 0000000..13637a2 --- /dev/null +++ b/upload_file.py @@ -0,0 +1,22 @@ +from config import * +import requests +import sys + +def upload_file(filename, path): + url = base_url+"files" + + payload = {'purpose': 'user_data'} + files=[ + ('file',(filename,open(path,'rb'),'application/pdf')) + ] + headers = { + 'Authorization': 'Bearer '+secret_key, + } + + response = requests.request("POST", url, headers=headers, data=payload, files=files) + + print(response.text) + + +if __name__ == '__main__': + upload_file(str(sys.argv[1]),str(sys.argv[2]))