-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnorag.py
77 lines (62 loc) · 2.22 KB
/
norag.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
import logging
import sys
import psycopg2
from llama_index.core import SimpleDirectoryReader, StorageContext
from llama_index.core import VectorStoreIndex
from llama_index.vector_stores.postgres import PGVectorStore
import textwrap
import os
import dotenv
from sqlalchemy import make_url
try:
os.remove("data/test/generated_answers.txt")
os.remove("generated_answers.txt")
except Exception as e:
pass
# Uncomment to see debug logs
# logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, force=True)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.core import Settings
from llama_index.core import PromptTemplate
from llama_index.core import get_response_synthesizer
from llama_index.core.retrievers import VectorIndexRetriever
from llama_index.core.query_engine import RetrieverQueryEngine
from llama_index.llms.ollama import Ollama
from llama_index.core import Settings
# ollama
llm = Ollama(model="mistral", request_timeout=999999.0, context_window=60000)
# Answering our questions
# print("Answering our questions")
# our_questions = 'data/train/questions.txt'
# questions = []
# with open(our_questions, 'r') as file:
# for line in file:
# # print(f"append question: {question} to list of questions")
# questions.append(line)
# answers = []
# for q in questions:
# response = llm.complete(q)
# answer = textwrap.fill(str(response), 100)
# answer = answer.replace("\n", " ")
# with open('data/test/generated_answers.txt', 'a') as file:
# file.write(answer)
# file.write("\n")
# answers.append(answer)
# Answering released test questions
print("Answering released test questions")
our_questions = 'released_questions.txt'
questions = []
with open(our_questions, 'r') as file:
for line in file:
questions.append(line)
answers = []
for q in questions:
response = llm.complete(q)
answer = textwrap.fill(str(response), 100)
answer = answer.replace("\n", " ")
with open('generated_answers.txt', 'a') as file:
file.write(answer)
file.write("\n")
answers.append(answer)