|
import requests |
|
import asyncio |
|
|
|
|
|
def get_docs(question: str, top_k: int, encoder, pinecone_index) -> list[str]: |
|
|
|
xq = encoder([question]) |
|
|
|
res = pinecone_index.query(vector=xq, top_k=top_k, include_metadata=True) |
|
|
|
print(res) |
|
docs = [x["metadata"] for x in res["matches"]] |
|
return docs |
|
|
|
def generate(query: str, docs: list[str], groq_client, messages): |
|
docs = "\n---\n".join(docs) |
|
system_message =f''' |
|
You are a real state assistant that answers questions about properties in Dubai using the |
|
context provided below that is you information. |
|
then please generate the response like this schema |
|
[ANS] |
|
```json |
|
{{ |
|
answer: HERE THE RESPONSE OF LLM |
|
}}``` |
|
[\ANS] |
|
if the context may not have the answer of the question please |
|
ask user to provide you more information |
|
\n\n |
|
CONTEXT:\n |
|
{docs} |
|
''' |
|
|
|
|
|
chat_response = groq_client.chat.completions.create( |
|
model="llama3-70b-8192", |
|
messages=messages, |
|
stream=True |
|
) |
|
print(chat_response) |
|
for chunk in chat_response: |
|
return chunk.choices[0].delta.content |
|
|
|
|
|
def oracle_db(query:str, top_k:int) -> list[dict]: |
|
import oracledb |
|
connection = oracledb.connect(user="ai", password="testtest",dsn="91.75.21.131:9522/FREEPDB1") |
|
cursor = connection.cursor() |
|
exist = cursor.execute("""SELECT v.vector_id, prop.*, t.*, |
|
VECTOR_DISTANCE(v.vector,TO_VECTOR(VECTOR_EMBEDDING(ALL_MINILM_L12_V2 USING :query as data)), COSINE) AS distance |
|
FROM |
|
ai.prop_vectors v |
|
JOIN ai.dld_property prop ON prop.property_id = v.property_id |
|
JOIN ai.dld_trans t ON t.prop_id = v.property_id |
|
ORDER BY distance ASC |
|
FETCH FIRST :top_k ROWS ONLY""", query=query, top_k=top_k) |
|
columns = [col[0] for col in cursor.description] |
|
cursor.rowfactory = lambda *args: dict(zip(columns, args)) |
|
exist = cursor.fetchall() |
|
print(query) |
|
print(exist) |
|
connection.close() |
|
return exist |
|
|
|
|
|
async def question_suggestion_api(message:str)-> list: |
|
questions = [] |
|
|
|
|
|
url = "http://91.75.21.131:9080/ords/ai/rag/question_suggestion" |
|
|
|
|
|
payload = { |
|
'response': message |
|
} |
|
|
|
|
|
response = requests.post(url, params=payload) |
|
await asyncio.sleep(2) |
|
|
|
print(response.text) |
|
|
|
|
|
if response.status_code == 200: |
|
|
|
data = response.json() |
|
print(data) |
|
|
|
|
|
questions = data.get('expected_responses', []) |
|
|
|
if questions: |
|
print("Questions:") |
|
for idx, question in enumerate(questions): |
|
print(f"{idx}. {question}") |
|
else: |
|
print("No questions found in the response.") |
|
else: |
|
print(f"Request failed with status code: {response.status_code}") |
|
|
|
return questions |
|
|
|
|
|
|
|
|
|
async def send_chatbot_request(question, cohere_api_key)-> dict: |
|
|
|
url = "http://91.75.21.131:9080/ords/ai/rag/chatbot" |
|
|
|
|
|
payload = { |
|
'question': question, |
|
'cohere_api_key': cohere_api_key |
|
} |
|
|
|
|
|
try: |
|
response = requests.post(url, params=payload,timeout=26) |
|
await asyncio.sleep(3) |
|
print("hello") |
|
|
|
|
|
|
|
if response.status_code == 200: |
|
|
|
data = response.json() |
|
generations = data.get("generations",[])[0] |
|
print(generations) |
|
result = generations.get('text','') |
|
return result |
|
else: |
|
return f"Request failed with status code: {response.status_code}" |
|
|
|
except ValueError: |
|
return "Error: Unable to parse JSON response." |
|
except requests.Timeout: |
|
print("The request timed out. Please try again.") |
|
except requests.RequestException as e: |
|
print(f"An error occurred: {e}") |
|
|
|
|