HI,
I want to use the File Search feature in Azure, like the same which is available in OpenAI. My use case involves having a collection name and its field descriptions stored in a file—let’s say five collections. Users may ask various queries, and I want to perform a vector search to find the best-matched collection details. Then, give that collection details and description which is extracted from file search (vector search) needs to be provide to AI to generate code based on the descriptions in the collection.
I can achieve this with OpenAI's File Search, but I would like to know if Azure offers similar services. Maintaining context is also important for user queries, such as:
- give me the last location of vehicle ED3456?
- forAB1234?
Can you assist me with this?
I can provide the code I used in OpenAI, and I would like to implement the same use case in Azure. Need our assistance in doing configurations in azure and request to provide the sample python code.
import os
from openai import AssistantEventHandler, OpenAI
query_str_2 = '''f"mongodb://{username}:{password}@{host}:{port}""'''
default_prompt = f"""
call this user query {query_str_2} in MongoClient and give me the python code along with the db name and collection names.
At the end of the code instead of printing the results, store the final code result in "result_data" variable strictly,
in case final code result contains more than one record convert to dataframe and assigned to "result_data".
IMPORTANT: Do not provide any explanations in the response, provide python code only.
"""
# Set the OpenAI API key from environment variable
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
def file_upload():
# Create the assistant
assistant1 = client.beta.assistants.create(
name="You Are Mongo Query expert",
instructions=default_prompt,
model="gpt-4o",
tools=[{"type": "file_search"}],
)
# Create a vector store
vector_store1 = client.beta.vector_stores.create(name="Mongocollection_query")
print("vector_store_id",vector_store1.id)
file_paths = [r"prompt_files\test.txt"]
# Use context manager to handle file streams
try:
with open(file_paths[0], "rb") as file_stream:
file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
vector_store_id=vector_store1.id, files=[file_stream]
)
print("file batch status:", file_batch.status)
print(file_batch.file_counts)
uploaded_files = client.beta.vector_stores.files.list(vector_store_id=vector_store1.id)
print(uploaded_files)
uploaded_file_id = uploaded_files.data[0].id
# Update the assistant with the vector store
assistant = client.beta.assistants.update(
assistant_id=assistant1.id,
tool_resources={"file_search": {"vector_store_ids": [vector_store1.id]}},
)
print("assistant_id:", assistant.id)
print("Uploaded file_id:", uploaded_file_id)
return assistant.id, uploaded_file_id
except Exception as e:
print(f"An error occurred: {e}")
assistant_id, uploaded_file_id = file_upload()
thread = client.beta.threads.create(
messages=[
{
"role": "user",
"content": "give me the last known location of vehicle ED3456",
"attachments": [
{"file_id": uploaded_file_id, "tools": [{"type": "file_search"}]}
],
}
]
)
thread_id=thread.id
run = client.beta.threads.runs.create_and_poll(
thread_id=thread_id, assistant_id=assistant_id
)
messages = list(client.beta.threads.messages.list(thread_id=thread_id, run_id=run.id))
message_content = messages[0].content[0].text
annotations = message_content.annotations
citations = []
for index, annotation in enumerate(annotations):
message_content.value = message_content.value.replace(annotation.text, f"[{index}]")
if file_citation := getattr(annotation, "file_citation", None):
cited_file = client.files.retrieve(file_citation.file_id)
citations.append(f"[{index}] {cited_file.filename}")
print(message_content.value)
print("\n".join(citations))
#######################################################################
new_question = "for this vehicle AB1234"
"""Send a new question to an existing thread to maintain conversation context."""
# Create a new message in the existing thread
new_message_response = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=new_question
)
"""Run the thread to process the latest message."""
run_response = client.beta.threads.runs.create_and_poll(
thread_id=thread.id,
assistant_id=assistant_id
)
messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run_response.id))
# message_content = messages[-1].content # Assuming the last message is the response
# Optionally handle annotations and citations as before
message_content = messages[0].content[0].text
annotations = message_content.annotations
citations = []
for index, annotation in enumerate(annotations):
message_content.value = message_content.value.replace(annotation.text, f"[{index}]")
if file_citation := getattr(annotation, "file_citation", None):
cited_file = client.files.retrieve(file_citation.file_id)
citations.append(f"[{index}] {cited_file.filename}")
# Output the response and any citations
print(message_content.value)
print("\n".join(citations))