From 34735d3cfdf10411fc472288bbe582747921c342 Mon Sep 17 00:00:00 2001 From: Alkid Date: Fri, 29 Nov 2024 16:36:10 +0100 Subject: [PATCH] added LLM generated response using the RAG assistance --- general_working_directory/llm_with_rag.py | 79 +++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 general_working_directory/llm_with_rag.py diff --git a/general_working_directory/llm_with_rag.py b/general_working_directory/llm_with_rag.py new file mode 100644 index 0000000..7fcf1dc --- /dev/null +++ b/general_working_directory/llm_with_rag.py @@ -0,0 +1,79 @@ +from openai import OpenAI +import pandas as pd +import numpy as np +from numpy.linalg import norm +from owlapy.iri import IRI +from owlapy.owl_individual import OWLNamedIndividual +from owlapy.owl_ontology_manager import OntologyManager +from owlapy.owl_property import OWLDataProperty +from owlapy.owl_reasoner import StructuralReasoner + +manager = OntologyManager() +ontology = manager.load_ontology(IRI.create("file://fashionpedia-third-generation.owl")) +reasoner = StructuralReasoner(ontology=ontology) +dprop1 = OWLDataProperty(IRI.create("http://example.org/hasDescription")) +dprop2 = OWLDataProperty(IRI.create("http://example.org/hasLLMDescription")) + +llm_client = OpenAI(base_url="http://tentris-ml.cs.upb.de:8501/v1", api_key="token-tentris-upb") + + +def get_result(query, docs): + return llm_client.chat.completions.create( + model="tentris", + messages=[ + { + "role": "user", + "content": + [ + { + "type": "text", + "text": "You are a apparel-loving AI and your focus is to give information about apparels. You should find similar points on the information provided to you and present them in a short paragraph tailored to the following query: " + f"'{query}'" + f"The information is as follows: {docs}" + } + ] + } + ], + temperature=0.1, + seed=1 + ).choices[0].message.content + + +query = input("What would you like to wear?\n") + +df = pd.read_csv("embeddings_third_kg.csv", index_col=0, nrows=None) +iris = df.index.values.tolist() + +embbeding_client = OpenAI(base_url="http://tentris-ml.cs.upb.de:8502/v1", api_key="token-tentris-upb") + +docs = np.array(df.values) +qr = np.array(embbeding_client.embeddings.create(input=[query], model="tentris").data[0].embedding) + +docs_norms = docs / norm(docs, axis=1, keepdims=True) +qr_norms = qr / norm(qr) + +cosine_similarities = (docs_norms @ qr_norms).flatten() + +best_match_index = np.argmax(cosine_similarities) +best_similarity = cosine_similarities[best_match_index] + +indexes = np.argpartition(cosine_similarities, -10)[-10:] +merged_documents = "" +for index in indexes: + + iri = iris[index] + image_ind = OWLNamedIndividual(iri) + + llm_description = str(list(reasoner.data_property_values(image_ind, dprop2))[0].get_literal()) + if len(llm_description) > 800: + llm_description = llm_description[:800] + + all_descriptions = "" + for d in list(reasoner.data_property_values(image_ind, dprop1)): + all_descriptions = all_descriptions + d.get_literal() + "\n" + + merged_documents += llm_description + " \n" + all_descriptions + +result = get_result(query, merged_documents) + +print(result) \ No newline at end of file