import openai
from flask_cors import CORS
from flask import Flask, request, jsonify
import logging
import http.client
import ssl
import json
from langchain.text_splitter import CharacterTextSplitter
app = Flask(__name__)
CORS(app)
# Create an SSL context that does not verify certificates
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
conn = http.client.HTTPSConnection("in03-fcdd5149ab8f545.serverless.gcp-us-west1.cloud.zilliz.com", context=context)
# OpenAI and Milvus configuration
MODEL_NAME = "text-embedding-ada-002"
DIMENSION = 1536
COLLECTION_NAME = "ai_book"
# Initialize OpenAI client
openaiapi_key="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"
openai.api_key=openaiapi_key
# Function to generate embeddings using OpenAI
def generate_embedding(text):
return openai.Embedding.create(input=[text], model=MODEL_NAME).data[0].embedding
# Function to search similar text in Milvus
def search_similar_text(query_text, top_n=3):
query_vector = generate_embedding(query_text)
payload = json.dumps({
"collectionName": COLLECTION_NAME,
"data": [query_vector], # Ensure query_vector is passed as a list
"anns_field": "vector",
"param": {"metric_type": "COSINE", "params": {"nprobe": 10}},
"limit": top_n
})
headers = {
'Authorization': "Bearer 6d048b0bf1ded099c23420076752330645a449e218673e8fe273317512a48d3618981b5c9ed2f6a65f910c1c070e1e7bd5374f73",
'Accept': "application/json",
'Content-Type': "application/json"
}
conn.request("POST", "/v2/vectordb/entities/search", payload, headers)
res = conn.getresponse()
data = res.read()
data = json.loads(data.decode("utf-8")) # Decode bytes and parse JSON
data_array = data.get("data", []) # Extract the 'data' array
return data_array
# Function to fetch text by ID from Milvus
def fetch_text_by_id(entity_id):
# Convert entity_id to a string to avoid concatenation issues
payloadforquery = json.dumps({
"collectionName": COLLECTION_NAME,
"filter": f"id in [{entity_id}]"
})
headers = {
'Authorization': "Bearer 6d048b0bf1ded099c23420076752330645a449e218673e8fe273317512a48d3618981b5c9ed2f6a65f910c1c070e1e7bd5374f73",
'Accept': "application/json",
'Content-Type': "application/json"
}
conn.request("POST", "/v2/vectordb/entities/query", payloadforquery, headers)
res = conn.getresponse()
data = res.read()
data = json.loads(data.decode("utf-8")) # Decode bytes and parse JSON
data_array = data.get("data", [])
data_text = data_array[0].get("text", "Text not found") # Extract the 'data' array
return data_text
# Function to process search results
def process_results(results):
processed_results = []
for result in results:
result_data = {
"id": result['id'],
"distance": result['distance'],
"text": fetch_text_by_id(result['id'])
}
processed_results.append(result_data)
return processed_results
@app.route('/generate', methods=['POST'])
def generate():
try:
data = request.json
print(data)
query = data.get('query')
results = search_similar_text(query, top_n=3)
processed_results = process_results(results)
# Display results
response = "Answer based on the following text only\n"
for result in processed_results:
response += "\n".join(result['text'])
response += "\n now ".join(query)
currentmodel='gpt-4o'
model_max_tokens=1000
refinedresponse = openai.ChatCompletion.create(
model=currentmodel,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": response}
],
max_tokens=model_max_tokens
)
return jsonify({'response': refinedresponse['choices'][0]['message']['content']})
except Exception as e:
logging.error("Error occurred: %s", e)
return jsonify({'error': 'Internal Server Error'}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)