momegas commited on
Commit
fcd5226
β€’
1 Parent(s): c22f276

πŸ• Minor stuff

Browse files
.gitignore CHANGED
@@ -5,4 +5,5 @@ qnabot.egg-info
5
  dist
6
  build
7
  **.pickle
 
8
  .env
 
5
  dist
6
  build
7
  **.pickle
8
+ **.pkl
9
  .env
examples/example.ipynb β†’ example.ipynb RENAMED
@@ -2,7 +2,7 @@
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
- "execution_count": 15,
6
  "metadata": {},
7
  "outputs": [
8
  {
@@ -10,32 +10,33 @@
10
  "output_type": "stream",
11
  "text": [
12
  "Using model: gpt-3.5-turbo\n",
13
- "Creating index...\n"
14
  ]
15
  }
16
  ],
17
  "source": [
18
- "from qnabot.QnABot import QnABot\n",
19
- "import os, sys\n",
20
  "\n",
21
- "os.environ[\"OPENAI_API_KEY\"] = \"your api key\"\n",
22
  "\n",
23
- "bot = QnABot(directory=\"./files\")"
 
24
  ]
25
  },
26
  {
27
  "cell_type": "code",
28
- "execution_count": 18,
29
  "metadata": {},
30
  "outputs": [
31
  {
32
  "name": "stdout",
33
  "output_type": "stream",
34
  "text": [
35
- "The first roster of the Avengers in comics included Iron Man, Thor, Hulk, Ant-Man, and the Wasp.\n",
36
- "SOURCES: files/facts.txt\n",
37
  "Vision is an android superhero who was created by Ultron but ultimately joined the Avengers and became an important member of the team.\n",
38
- "SOURCES: files/facts.txt\n"
39
  ]
40
  }
41
  ],
 
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
+ "execution_count": 4,
6
  "metadata": {},
7
  "outputs": [
8
  {
 
10
  "output_type": "stream",
11
  "text": [
12
  "Using model: gpt-3.5-turbo\n",
13
+ "Loading path from disk...\n"
14
  ]
15
  }
16
  ],
17
  "source": [
18
+ "from qnabot import QnABot\n",
19
+ "from dotenv import load_dotenv\n",
20
  "\n",
21
+ "load_dotenv()\n",
22
  "\n",
23
+ "bot = QnABot(directory=\"./examples/files\", index=\"./index.pkl\", verbose=True)\n",
24
+ "# bot.save_index(\"./index.pkl\")"
25
  ]
26
  },
27
  {
28
  "cell_type": "code",
29
+ "execution_count": 2,
30
  "metadata": {},
31
  "outputs": [
32
  {
33
  "name": "stdout",
34
  "output_type": "stream",
35
  "text": [
36
+ "The first roster of Avengers in comics included Iron Man, Thor, Hulk, Ant-Man, and the Wasp.\n",
37
+ "SOURCES: examples/files/facts.txt\n",
38
  "Vision is an android superhero who was created by Ultron but ultimately joined the Avengers and became an important member of the team.\n",
39
+ "SOURCES: examples/files/facts.txt\n"
40
  ]
41
  }
42
  ],
qnabot/QnABot.py CHANGED
@@ -1,4 +1,3 @@
1
- # Import necessary libraries and modules
2
  from langchain.llms import OpenAI
3
  from langchain.chat_models import ChatOpenAI
4
  from langchain.embeddings import OpenAIEmbeddings
@@ -7,6 +6,7 @@ from langchain.chains.qa_with_sources import load_qa_with_sources_chain
7
  from langchain.vectorstores.faiss import FAISS
8
  import pickle
9
  import os
 
10
 
11
 
12
  class QnABot:
@@ -15,7 +15,8 @@ class QnABot:
15
  directory: str,
16
  index: str | None = None,
17
  model: str | None = None,
18
- temperature=0,
 
19
  ):
20
  # Initialize the QnABot by selecting a model, creating a loader,
21
  # and loading or creating an index
@@ -24,7 +25,7 @@ class QnABot:
24
  self.load_or_create_index(index)
25
 
26
  # Load the question-answering chain for the selected model
27
- self.chain = load_qa_with_sources_chain(self.llm)
28
 
29
  def select_model(self, model: str | None, temperature: float):
30
  # Select and set the appropriate model based on the provided input
@@ -60,26 +61,14 @@ class QnABot:
60
  with open(index_path, "wb") as f:
61
  pickle.dump(self.search_index, f)
62
 
63
- def print_answer(self, question, k=1):
64
  # Retrieve and print the answer to the given question
65
  input_documents = self.search_index.similarity_search(question, k=k)
66
- print(
67
- self.chain(
68
- {
69
- "input_documents": input_documents,
70
- "question": question,
71
- },
72
- return_only_outputs=True,
73
- )["output_text"]
74
- )
75
 
76
- def get_answer(self, question, k=1) -> str:
77
  # Retrieve the answer to the given question and return it
78
  input_documents = self.search_index.similarity_search(question, k=k)
79
- return self.chain(
80
- {
81
- "input_documents": input_documents,
82
- "question": question,
83
- },
84
- return_only_outputs=True,
85
- )["output_text"]
 
 
1
  from langchain.llms import OpenAI
2
  from langchain.chat_models import ChatOpenAI
3
  from langchain.embeddings import OpenAIEmbeddings
 
6
  from langchain.vectorstores.faiss import FAISS
7
  import pickle
8
  import os
9
+ from langchain.chains.combine_documents.stuff import StuffDocumentsChain
10
 
11
 
12
  class QnABot:
 
15
  directory: str,
16
  index: str | None = None,
17
  model: str | None = None,
18
+ verbose: bool = False,
19
+ temperature: int = 0,
20
  ):
21
  # Initialize the QnABot by selecting a model, creating a loader,
22
  # and loading or creating an index
 
25
  self.load_or_create_index(index)
26
 
27
  # Load the question-answering chain for the selected model
28
+ self.chain = load_qa_with_sources_chain(self.llm, verbose=verbose)
29
 
30
  def select_model(self, model: str | None, temperature: float):
31
  # Select and set the appropriate model based on the provided input
 
61
  with open(index_path, "wb") as f:
62
  pickle.dump(self.search_index, f)
63
 
64
+ def print_answer(self, question: str, k=1):
65
  # Retrieve and print the answer to the given question
66
  input_documents = self.search_index.similarity_search(question, k=k)
67
+ a = self.chain.run(input_documents=input_documents, question=question)
68
+ print(a)
 
 
 
 
 
 
 
69
 
70
+ def get_answer(self, question: str, k=1) -> str:
71
  # Retrieve the answer to the given question and return it
72
  input_documents = self.search_index.similarity_search(question, k=k)
73
+ answer = self.chain.run(input_documents=input_documents, question=question)
74
+ return answer