Dmitry Kirsanov commited on
Commit
d1d70b6
·
1 Parent(s): 9514ac4

refactorings, 2 fact finders

Browse files
Files changed (3) hide show
  1. app.py +6 -3
  2. llm.py +20 -8
  3. understand.py +22 -10
app.py CHANGED
@@ -8,15 +8,18 @@ import logger
8
  from dotenv import load_dotenv
9
  load_dotenv()
10
 
 
 
11
  def process(filename, model):
12
- doc = understand.Document()
 
13
  return doc.understand(filename)
14
 
15
  gradio_app = gr.Interface(
16
  fn=process,
17
  allow_flagging="auto",
18
- inputs=[gr.File(label="Input document (HTML)"), gr.Dropdown(choices=["gpt-3.5-turbo", "gpt-4", "gpt-4-turbo-preview"], value="gpt-3.5-turbo")],
19
- outputs=[gr.Textbox(label="Parsed", lines=8)])
20
 
21
  if __name__ == "__main__":
22
  logger.info("DU starts!")
 
8
  from dotenv import load_dotenv
9
  load_dotenv()
10
 
11
+ LLM = llm.OpenAI()
12
+
13
  def process(filename, model):
14
+ LLM.set_model(model)
15
+ doc = understand.Document(LLM)
16
  return doc.understand(filename)
17
 
18
  gradio_app = gr.Interface(
19
  fn=process,
20
  allow_flagging="auto",
21
+ inputs=[gr.File(label="Input document (HTML)"), gr.Dropdown(choices=LLM.get_models(), value=LLM.get_models()[0])],
22
+ outputs=[gr.Textbox(label="Document type", lines=1), gr.Textbox(label="Summary", lines=8)])
23
 
24
  if __name__ == "__main__":
25
  logger.info("DU starts!")
llm.py CHANGED
@@ -19,7 +19,10 @@ class LLM:
19
  def count_tokens(self, text):
20
  raise NotImplementedError("Subclass must implement count_tokens method")
21
 
22
- class Claude3Opus(LLM):
 
 
 
23
  def __init__(self):
24
  super().__init__(context_length=200000, api_endpoint="https://api.anthropic.com", max_output_tokens=4096)
25
  anthropic.api_key = "YOUR_ANTHROPIC_API_KEY" #FIXME
@@ -41,7 +44,7 @@ class Claude3Opus(LLM):
41
  encoding = tiktoken.get_encoding("cl100k_base") # FIXME: Claude didn't release its tokenizer; consider using https://github.com/javirandor/anthropic-tokenizer
42
  return len(encoding.encode(text))
43
 
44
- class Gemini15(LLM):
45
  def __init__(self):
46
  super().__init__(context_length=256000, api_endpoint="https://generativelanguage.googleapis.com", max_output_tokens=8192)
47
  palm.configure(api_key="YOUR_GOOGLE_API_KEY") #FIXME
@@ -63,7 +66,7 @@ class Gemini15(LLM):
63
  encoding = tiktoken.get_encoding("p50k_base") # FIXME: Gemini didn't release its tokenizer; as of apr 2024, the only thing i could google is https://www.googlecloudcommunity.com/gc/AI-ML/Please-share-Gemini-tokenize-information/m-p/709495
64
  return len(encoding.encode(text))
65
 
66
- class GPT4Turbo(LLM):
67
  def __init__(self):
68
  super().__init__(context_length=128000, api_endpoint="https://api.openai.com/v1/chat/completions", max_output_tokens=4096)
69
  api_key = os.environ.get("OPENAI_API_KEY")
@@ -75,16 +78,25 @@ class GPT4Turbo(LLM):
75
  def send_question(self, question):
76
  num_tokens = self.count_tokens(question)
77
  if num_tokens > self.context_length - self.max_output_tokens:
78
- raise ValueError(f"Question exceeds context length limit of {self.context_length} tokens")
79
 
80
  response = self.client.chat.completions.create(
81
- model="gpt-4-turbo",
82
  messages=[{"role": "user", "content": question}],
83
  max_tokens=self.max_output_tokens,
84
  )
85
- output_truncated = len(response.choices[0].message.content) == self.max_output_tokens
86
- return response.choices[0].message.content, output_truncated
 
 
87
 
88
  def count_tokens(self, text):
89
  encoding = tiktoken.get_encoding("cl100k_base") # GPT-4 uses the cl100k_base encoding https://github.com/openai/tiktoken
90
- return len(encoding.encode(text))
 
 
 
 
 
 
 
 
19
  def count_tokens(self, text):
20
  raise NotImplementedError("Subclass must implement count_tokens method")
21
 
22
+ def get_models(self):
23
+ raise NotImplementedError("Subclass must implement get_models method")
24
+
25
+ class Anthropic(LLM):
26
  def __init__(self):
27
  super().__init__(context_length=200000, api_endpoint="https://api.anthropic.com", max_output_tokens=4096)
28
  anthropic.api_key = "YOUR_ANTHROPIC_API_KEY" #FIXME
 
44
  encoding = tiktoken.get_encoding("cl100k_base") # FIXME: Claude didn't release its tokenizer; consider using https://github.com/javirandor/anthropic-tokenizer
45
  return len(encoding.encode(text))
46
 
47
+ class Google(LLM):
48
  def __init__(self):
49
  super().__init__(context_length=256000, api_endpoint="https://generativelanguage.googleapis.com", max_output_tokens=8192)
50
  palm.configure(api_key="YOUR_GOOGLE_API_KEY") #FIXME
 
66
  encoding = tiktoken.get_encoding("p50k_base") # FIXME: Gemini didn't release its tokenizer; as of apr 2024, the only thing i could google is https://www.googlecloudcommunity.com/gc/AI-ML/Please-share-Gemini-tokenize-information/m-p/709495
67
  return len(encoding.encode(text))
68
 
69
+ class OpenAI(LLM):
70
  def __init__(self):
71
  super().__init__(context_length=128000, api_endpoint="https://api.openai.com/v1/chat/completions", max_output_tokens=4096)
72
  api_key = os.environ.get("OPENAI_API_KEY")
 
78
  def send_question(self, question):
79
  num_tokens = self.count_tokens(question)
80
  if num_tokens > self.context_length - self.max_output_tokens:
81
+ raise ValueError(f"Question has {num_tokens} tokens, exceeds context length limit of {self.context_length} tokens, allowing for {self.max_output_tokens} tokens of answer")
82
 
83
  response = self.client.chat.completions.create(
84
+ model=self.modelname,
85
  messages=[{"role": "user", "content": question}],
86
  max_tokens=self.max_output_tokens,
87
  )
88
+ truncated = response.choices[0].finish_reason == 'length'
89
+ print (f"Answer was truncated due to length (model name: {self.modelname}, max output tokens: {self.max_output_tokens}). Modify the instructions to produce a shorter answer or to give answer in parts.")
90
+
91
+ return response.choices[0].message.content, truncated
92
 
93
  def count_tokens(self, text):
94
  encoding = tiktoken.get_encoding("cl100k_base") # GPT-4 uses the cl100k_base encoding https://github.com/openai/tiktoken
95
+ return len(encoding.encode(text))
96
+
97
+ def get_models(self):
98
+ return ["gpt-4-32k", "gpt-4", "gpt-4-turbo-preview"]
99
+
100
+ def set_model(self, model):
101
+ self.modelname = model
102
+
understand.py CHANGED
@@ -1,11 +1,10 @@
1
  import parse
2
  import llm
3
 
4
- LLM = llm.GPT4Turbo()
5
-
6
  class Document():
7
 
8
- def __init__(self) -> None:
 
9
  self.documentSoup = None # beautifulSoup
10
  self.fad = None # string
11
  self.doctype = None
@@ -13,23 +12,36 @@ class Document():
13
 
14
  def query_doctype(self):
15
  try:
16
- answer, truncated = LLM.send_question(f"""Please read this document carefully, remember all you know about the concepts it touches, and give a brief description of what this document is. Examples: "Textbook about interpersonal psychology", "State standard on oil rig equipment", "User manual for a smartphone app". Write just one line with the description.
17
 
18
  <document>{self.fad}</document>""")
19
 
20
- if truncated:
21
- print ("Document too long, needs compression")
22
- return None
23
- else:
24
- return answer.strip()
25
 
26
  except Exception as e:
27
  print (e.message, e.args)
28
  return None
29
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def understand(self, filename):
31
  self.documentSoup = parse.parse(filename)
32
  self.fad = self.documentSoup.get_text()
 
 
33
  self.doctype = self.query_doctype()
34
- return self.doctype
 
 
 
35
 
 
1
  import parse
2
  import llm
3
 
 
 
4
  class Document():
5
 
6
+ def __init__(self, llm) -> None:
7
+ self.LLM = llm
8
  self.documentSoup = None # beautifulSoup
9
  self.fad = None # string
10
  self.doctype = None
 
12
 
13
  def query_doctype(self):
14
  try:
15
+ answer, truncated = self.LLM.send_question(f"""Please read this document carefully, remember all you know about the concepts it touches, and give a brief description of what this document is. Examples: "Textbook about interpersonal psychology", "State standard on oil rig equipment", "User manual for a smartphone app". Write just one line with the description.
16
 
17
  <document>{self.fad}</document>""")
18
 
19
+ return answer.strip()
 
 
 
 
20
 
21
  except Exception as e:
22
  print (e.message, e.args)
23
  return None
24
 
25
+ def query_summary(self):
26
+ try:
27
+ answer, truncated = self.LLM.send_question(f"""Below is a {self.doctype}. Please read it carefully, remember all you know about the concepts it touches, and give a one-paragraph summary of its content. Write just one paragraph with the summary.
28
+
29
+ <document>{self.fad}</document>""")
30
+
31
+ return answer.strip()
32
+
33
+ except Exception as e:
34
+ print (e.message, e.args)
35
+ return None
36
+
37
  def understand(self, filename):
38
  self.documentSoup = parse.parse(filename)
39
  self.fad = self.documentSoup.get_text()
40
+
41
+ # fact finders:
42
  self.doctype = self.query_doctype()
43
+ if self.doctype != None:
44
+ self.summary = self.query_summary()
45
+
46
+ return self.doctype, self.summary
47