Py.Cafe

sharath.s0220/

ai-text-embedding-retrieval

AI Text Embedding and Retrieval System

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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)