Skip to content

Commit eed163f

Browse files
Merge branch 'dev'
2 parents 3d49f26 + c501c9d commit eed163f

File tree

7 files changed

+156
-46
lines changed

7 files changed

+156
-46
lines changed

DocToolsLLM/DocToolsLLM.py

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
class DocToolsLLM_class:
7979
"This docstring is dynamically replaced by the content of DocToolsLLM/docs/USAGE.md"
8080

81-
VERSION: str = "0.38"
81+
VERSION: str = "0.39"
8282

8383
#@optional_typecheck
8484
@typechecked
@@ -113,7 +113,7 @@ def __init__(
113113
query_condense_question: Union[bool, int] = True,
114114

115115
summary_n_recursion: int = 1,
116-
summary_language: str = "[same as input]",
116+
summary_language: str = "the same language as the document",
117117

118118
llm_verbosity: Union[bool, int] = False,
119119
debug: Union[bool, int] = False,
@@ -427,7 +427,9 @@ def ntfy(text: str) -> str:
427427

428428
if self.task == "summary_then_query":
429429
whi("Done summarizing. Switching to query mode.")
430-
if self.modelbackend == "openai":
430+
if "logit_bias" in litellm.get_supported_openai_params(
431+
model=f"{self.modelbackend}/{self.modelname}",
432+
):
431433
del self.llm.model_kwargs["logit_bias"]
432434
else:
433435
whi("Done summarizing. Exiting.")
@@ -473,7 +475,9 @@ def _summary_task(self) -> dict:
473475
else:
474476
red(f"Cost estimate > limit but the api_base was modified so not crashing.")
475477

476-
if self.modelbackend == "openai":
478+
if "logit_bias" in litellm.get_supported_openai_params(
479+
model=f"{self.modelbackend}/{self.modelname}",
480+
):
477481
# increase likelyhood that chatgpt will use indentation by
478482
# biasing towards adding space.
479483
logit_val = 3
@@ -510,8 +514,17 @@ def _summary_task(self) -> dict:
510514
56899: logit_val, # " "
511515
98517: logit_val, # " "
512516
}
517+
if "frequency_penalty" in litellm.get_supported_openai_params(
518+
model=f"{self.modelbackend}/{self.modelname}",
519+
):
513520
self.llm.model_kwargs["frequency_penalty"] = 0.0
521+
if "presence_penalty" in litellm.get_supported_openai_params(
522+
model=f"{self.modelbackend}/{self.modelname}",
523+
):
514524
self.llm.model_kwargs["presence_penalty"] = 0.0
525+
if "temperature" in litellm.get_supported_openai_params(
526+
model=f"{self.modelbackend}/{self.modelname}",
527+
):
515528
self.llm.model_kwargs["temperature"] = 0.0
516529

517530
@optional_typecheck
@@ -717,7 +730,61 @@ def summarize_documents(
717730
red(f"Cost discrepancy? Tokens used according to the callback: '{llmcallback.total_tokens}' (${total_cost:.5f})")
718731
return results
719732

720-
def prepare_query_task(self):
733+
@optional_typecheck
734+
def prepare_query_task(self) -> None:
735+
# set argument that are better suited for querying
736+
if "logit_bias" in litellm.get_supported_openai_params(
737+
model=f"{self.modelbackend}/{self.modelname}",
738+
):
739+
# increase likelyhood that chatgpt will use indentation by
740+
# biasing towards adding space.
741+
logit_val = 3
742+
self.llm.model_kwargs["logit_bias"] = {
743+
12: logit_val, # '-'
744+
# 220: logit_val, # ' '
745+
# 532: logit_val, # ' -'
746+
# 9: logit_val, # '*'
747+
# 1635: logit_val, # ' *'
748+
# 197: logit_val, # '\t'
749+
334: logit_val, # '**'
750+
# 25: logit_val, # ':'
751+
# 551: logit_val, # ' :'
752+
# 13: -1, # '.'
753+
# logit bias for indentation, the number of space, because it consumes less token than using \t
754+
257: logit_val, # " "
755+
260: logit_val, # " "
756+
1835: logit_val, # " "
757+
338: logit_val, # " "
758+
3909: logit_val, # " "
759+
5218: logit_val, # " "
760+
6663: logit_val, # " "
761+
792: logit_val, # " "
762+
10812: logit_val, # " "
763+
13137: logit_val, # " "
764+
15791: logit_val, # " "
765+
19273: logit_val, # " "
766+
25343: logit_val, # " "
767+
29902: logit_val, # " "
768+
39584: logit_val, # " "
769+
5341: logit_val, # " "
770+
52168: logit_val, # " "
771+
38244: logit_val, # " "
772+
56899: logit_val, # " "
773+
98517: logit_val, # " "
774+
}
775+
if "frequency_penalty" in litellm.get_supported_openai_params(
776+
model=f"{self.modelbackend}/{self.modelname}",
777+
):
778+
self.llm.model_kwargs["frequency_penalty"] = 0.0
779+
if "presence_penalty" in litellm.get_supported_openai_params(
780+
model=f"{self.modelbackend}/{self.modelname}",
781+
):
782+
self.llm.model_kwargs["presence_penalty"] = 0.0
783+
if "temperature" in litellm.get_supported_openai_params(
784+
model=f"{self.modelbackend}/{self.modelname}",
785+
):
786+
self.llm.model_kwargs["temperature"] = 0.0
787+
721788
# load embeddings for querying
722789
self.loaded_embeddings, self.embeddings = load_embeddings(
723790
embed_model=self.embed_model,

DocToolsLLM/docs/USAGE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
* If `--out_file` is used, each intermediate summary will be saved
151151
with the name `{out_file}.n.md` with n being the n-1th recursive summary.
152152

153-
* `--summary_language`: str, default `"[same as input]"`
153+
* `--summary_language`: str, default `"the same language as the document"`
154154
* When writing a summary, the LLM will write using the language
155155
specified in this argument. If it's `[same as input]`, the LLM
156156
will not translate.

DocToolsLLM/utils/llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from langchain_core.outputs.llm_result import LLMResult
1818
from langchain_community.llms import FakeListLLM
1919
from langchain_community.chat_models import ChatLiteLLM
20-
from langchain_community.chat_models import ChatOpenAI
20+
from langchain_openai import ChatOpenAI
2121
from langchain_community.cache import SQLiteCache
2222

2323
from .logger import whi, red, yel

DocToolsLLM/utils/prompts.py

Lines changed: 69 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,34 @@
77
# PROMPT FOR SUMMARY TASKS
88
BASE_SUMMARY_PROMPT = ChatPromptTemplate.from_messages(
99
[
10-
("system", """You are Alfred, my best journalist. Your job today is to summarize in a specific way a text section I just sent you, but I'm not interested simply in high level takeaways. What I'm interested in is the thought process of the author(s), the reasonning, the arguments used etc. Your summary has to be as quick and easy to read as possible while following the rules.
11-
This is very important to me so if you succeed, I'll tip you up to $2000!
10+
("system", """You are Alfred, the best of my team. Your task today is to summarize in a specific way a text section I just sent you, but I'm not only interested in high level takeaways. I also need the thought process present in the document, the reasonning followed, the arguments used etc. But your summary has to be as quick and easy to read as possible while following specific instructions.
11+
This is very important to me so if you succeed, I'll pay you up to $2000 depending on how well you did!
1212
13-
- Detailed instructions:
14-
```
13+
Detailed instructions:
14+
```
15+
- Take a deep breath before answering
1516
- Include:
16-
- All noteworthy information, anecdotes, facts, insights, definitions, clarifications, explanations, ideas, technical details, etc.
17+
- All noteworthy information, anecdotes, facts, insights, definitions, clarifications, explanations, ideas, technical details, etc
18+
- Epistemic indicators: you need to make explicit what markers of uncertainty for each information
1719
- Exclude:
18-
- Sponsors, advertisements, etc.
19-
- Jokes, ramblings.
20-
- End of page references, tables of content, sources, links etc.
21-
- When in doubt, keep the information in your summary.
20+
- Sponsors, advertisements, etc
21+
- Jokes, ramblings
22+
- End of page references and sources, tables of content, links etc
23+
- When in doubt about wether to include an information, include it
2224
- Format:
23-
- Use markdown format: that means logical indentation, bullet points, bold etc. Don't use headers.
24-
- Don't use complete sentence, I'm in a hurry and need bullet points.
25-
- Use one bullet point per information, with the use of logical indentation this makes the whole piece quick and easy to skim.
26-
- Use bold for important concepts (i.e. "- Mentions that **dietary supplements are healty** because ...")
27-
- Write in {language}.
28-
- Reformulate direct quotes to be concise, but stay faithful to the tone of the author.
29-
- Avoid repetitions: e.g. don't start several bullet points by 'The author thinks that', just say it once then use indentation to make it implied..
25+
- Use markdown format: that means logical indentation, bullet points, bold etc
26+
- Don't use headers
27+
- Use bold for important concepts, and italic for epistemic markers
28+
- ie "- *In his opinion*, **dietary supplements** are **healty** because ..."
29+
- Stay faithful to the tone of the author
30+
- You don't always have to use full sentences: you can ignore end of line punctuation etc
31+
- BUT it is more important to be unambiguous and truthful than concise
32+
- EVERY TIME POSSIBLE: use direct quote, 'formatted like that'
33+
- Use one bullet point per information
34+
- With the use of logical indentation this makes the whole piece quick and easy to skim
35+
- Write your summary in {language}
36+
- Avoid repetitions
37+
- eg don't start several bullet points by 'The author thinks that', just say it once then use indented children bullet points to make it implicit
3038
```"""),
3139
("human", """{recursion_instruction}{metadata}{previous_summary}
3240
@@ -37,7 +45,7 @@
3745
],
3846
)
3947
# if the summary is recursive, add those instructions
40-
RECURSION_INSTRUCTION = "\nBut today, I'm giving you back your own summary because it was too long and contained repetition. I want you to rewrite it as closely as possible while removing repetitions and fixing the logical indentation. Of course you have to remve the 'Chunk' indicator if present, to curate the logical indentation. You can reorganize the text freely as long as you don't lose relevant information and follow the instructions I gave you. This is important."
48+
RECURSION_INSTRUCTION = "Actually, I'm giving you back your own summary from last time because it was too long and contained repetitions. I want you to rewrite it as closely as possible while removing repetitions and fixing the logical indentation. Of course you have to remove the 'Chunk' indicator if present, to curate the logical indentation. You can reorganize the text freely as long as you don't lose relevant information and follow the instructions I gave you before and right now. This is important."
4149

4250
# PROMPT FOR QUERY TASKS
4351
PR_CONDENSE_QUESTION = ChatPromptTemplate.from_messages(
@@ -57,30 +65,57 @@
5765

5866
PR_ANSWER_ONE_DOC = ChatPromptTemplate.from_messages(
5967
[
60-
("system", """You are an assistant for question-answering tasks.
61-
You are given a piece of document and a question to answer.
62-
If the document is ENTIRELY irrelevant to the question, answer directly 'IRRELEVANT' without anything else and no other formatting.
63-
Otherwise, use a maximum of 3 md bulletpoints to answer the question using only information from the provided document.
64-
Use markdown formatting for easier reading, but don't wrap your answer in a code block or anything like that: reply instantly without acknowledging those rules.
65-
Doing all that you have to remain VERY concise while remaining truthful to the document content.
66-
But DON'T interpret the question too strictly, e.g. the question can be implicit because phrased as an instruction like "give me all information about such and such", use common sense!"""),
68+
("system", """Given a piece of document and a question, your task is to answer the question while following specific instructions.
69+
70+
Detailed instructions:
71+
```
72+
- Use markdown formatting
73+
- Use bullet points, but no headers, bold, italic etc.
74+
- Use logic based indentation for the bullet points.
75+
- DON'T wrap your answer in a code block or anything like that.
76+
- Take a deep breath before answering.
77+
- But then reply directly without acknowledging your task.
78+
- Use a maximum of 5 markdown bullet points to answer the question.
79+
- If the document is ENTIRELY irrelevant to the question, answer simply 'IRRELEVANT' and NOTHING ELSE (especially no formatting).
80+
- EVERY TIME POSSIBLE: use direct quote from the document, 'formatted like that'.
81+
- DON'T use your own knowledge of the subject, only use the document.
82+
- Remain as concise as possible, you can use [...] in your quotes to remove unecessary text.
83+
- DON'T interpret the question too strictly:
84+
- eg: if the question is phrased as an instruction like "give me all information about such and such", use common sense and satisfy the instruction!
85+
```"""),
6786
("human", "Question: '{question_to_answer}'\nContext:\n```\n{context}\n```\nWhat's your reply?")
6887
]
6988
)
7089

7190
PR_COMBINE_INTERMEDIATE_ANSWERS = ChatPromptTemplate.from_messages(
7291
[
73-
("system", """Given some statements and an answer, your task it to first answer directly the question in a md bullet point, then combine all additional information as additional bullet points. You must only use information from the statements.
74-
BUT, and above all: if the statements are not enough to answer the question you MUST start your answer by: 'OPINION:' followed by your answer using your own knowledge to let me know the source is you!
75-
No redundant bullet points must remain: you must combine redundant bullet points into a single more complicated bullet point.
92+
("system", """Given some statements and an answer, your task is to:
93+
1. answer directly the question using markdown bullet points
94+
2. then combine all additional information as additional bullet points.
7695
77-
Ignore statements that are completely irrelevant to the question.
78-
Don't narrate, just do what I asked without acknowledging those rules.
79-
If the question contains acronyms, reuse them without specifying what they mean.
80-
Use markdown format, with bullet points and indentation etc.
81-
Be concise but don't omit ANY information from the statements.
82-
Answer in the same language as the question.
83-
But DON'T interpret the question too strictly, for example if the question makes reference to "documents" consider that it's what I call here "statements" for example. For example, if the question is rather an instruction like "give me all information about such and such", use common sense and don't be too strict!"""),
96+
Detailed instructions:
97+
```
98+
- Take a deep breath before answering.
99+
- Format:
100+
- Use markdown format, with bullet points.
101+
- IMPORTANT: use logical indentation to organize information hierarchically.
102+
- The present instructions are a good example of proper formatting.
103+
- Don't narrate, just do what I asked without acknowledging those rules.
104+
- Reuse acronyms without specifying what they mean.
105+
- Be concise but don't omit only irrelevant information from the statements.
106+
- Answer in the same language as the question.
107+
- What to include:
108+
- Only use information from the provided statements.
109+
- IMPORTANT: if the statements are insufficient to answer the question you MUST start your answer by: 'OPINION:' followed by your own answer.
110+
- This way I know the source is you!
111+
- Ignore statements that are completely irrelevant to the question.
112+
- Semi relevant statements can be included, especially if related to possible followup questions.
113+
- No redundant information must remain.
114+
- eg: fix redundancies with one parent bullet point and several indented children.
115+
- DON'T interpret the question too strictly:
116+
- eg: if the question makes reference to "documents" consider that it's what I call here "statements" for example.
117+
- eg: if the question is phrased as an instruction like "give me all information about such and such", use common sense and satisfy the instruction!
118+
```"""),
84119
("human", "Question: `{question}`\nStatements:\n```\n{intermediate_answers}\n```\nYour answer?""")
85120
]
86121
)

0 commit comments

Comments
 (0)