added modules file
Browse files- modules/__init__.py +0 -0
- modules/knowledge_graph.py +21 -0
- modules/online_search.py +46 -0
- modules/validation.py +6 -0
modules/__init__.py
ADDED
|
File without changes
|
modules/knowledge_graph.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
import faiss
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
def search_kg(query, index_path, dataset_path, top_k=5):
|
| 7 |
+
index = faiss.read_index(index_path)
|
| 8 |
+
df = pd.read_json(dataset_path, lines=True)
|
| 9 |
+
|
| 10 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 11 |
+
query_embedding = model.encode([query], convert_to_tensor=True).cpu().numpy()
|
| 12 |
+
distances, indices = index.search(query_embedding, top_k)
|
| 13 |
+
|
| 14 |
+
results = []
|
| 15 |
+
for i in range(top_k):
|
| 16 |
+
idx = indices[0][i]
|
| 17 |
+
if 0 <= idx < len(df):
|
| 18 |
+
headline = df.iloc[idx]['headline']
|
| 19 |
+
description = df.iloc[idx]['short_description']
|
| 20 |
+
results.append(f"{headline}. {description}")
|
| 21 |
+
return " ".join(results)
|
modules/online_search.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
|
| 3 |
+
def search_online(query, api_key, base_url, model):
|
| 4 |
+
messages = [
|
| 5 |
+
{
|
| 6 |
+
"role": "system",
|
| 7 |
+
"content": (
|
| 8 |
+
"You are an artificial intelligence assistant and you need to "
|
| 9 |
+
"engage in a helpful, detailed, polite conversation with a user."
|
| 10 |
+
),
|
| 11 |
+
},
|
| 12 |
+
{"role": "user", "content": query},
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
client = OpenAI(api_key=api_key, base_url=base_url)
|
| 16 |
+
response = client.chat.completions.create(
|
| 17 |
+
model=model,
|
| 18 |
+
messages=messages,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# print(type(response))
|
| 22 |
+
# print(response)
|
| 23 |
+
# print(vars(response))
|
| 24 |
+
result = process_result(response)
|
| 25 |
+
return result
|
| 26 |
+
|
| 27 |
+
def process_result(response):
|
| 28 |
+
# Create a dictionary to hold all the individual pieces of information
|
| 29 |
+
response_dict = {
|
| 30 |
+
# 'finish_reason': response.choices[0].finish_reason,
|
| 31 |
+
# 'index': response.choices[0].index,
|
| 32 |
+
'message_content': response.choices[0].message.content,
|
| 33 |
+
# 'delta_role': response.choices[0].delta['role'] if response.choices[0].delta else None,
|
| 34 |
+
# 'delta_content': response.choices[0].delta['content'] if response.choices[0].delta else None,
|
| 35 |
+
# 'created': response.created,
|
| 36 |
+
# 'object': response.object,
|
| 37 |
+
# 'usage_completions_tokens': response.usage.completion_tokens,
|
| 38 |
+
'citations': response.citations # Assuming `citations` is part of the response object
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
return response_dict
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# result = search_online("", "", "", "")
|
| 46 |
+
# print(result)
|
modules/validation.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
def calculate_truthfulness_score(info, context):
|
| 4 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 5 |
+
result = classifier(info, [context], multi_label=False)
|
| 6 |
+
return result['scores'][0]
|