Skip to content

Commit

Permalink
Improving prompts - Summarization - 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Saporetti Junior committed Mar 13, 2024
1 parent a8884a5 commit 25fe495
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
13 changes: 11 additions & 2 deletions src/main/askai/core/component/internet_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from askai.core.askai_events import AskAiEvents
from askai.core.askai_messages import msg
from askai.core.support.langchain_support import lc_llm


class InternetService(metaclass=Singleton):
Expand Down Expand Up @@ -53,8 +54,16 @@ def search_google(self, query: str, *sites: str) -> Optional[str]:
search_results += str(self._tool.run(f"{query} site: {url}"))
else:
search_results += str(self._tool.run(f"{query}"))
log.debug(f"Internet search returned: %s", search_results)
return os.linesep.join(search_results) if isinstance(search_results, list) else search_results
log.debug(f"Internet search output: %s", search_results)

return search_results


assert (internet := InternetService().INSTANCE) is not None


if __name__ == '__main__':
q = 'What is the whether like in Belo Horizonte now'
embeddings = lc_llm.create_embeddings()
c = internet.search_google(q)
eq = embeddings.embed_query(q)
12 changes: 8 additions & 4 deletions src/main/askai/core/component/summarizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def path(self) -> str:

@lru_cache
def generate(self, folder: str | Path, glob: str = None) -> None:
"""TODO"""
"""Generate a summarization of the folder contents.
:param folder: The base folder of the summarization.
:param glob: The glob pattern or file of the summarization.
"""
self._folder: str = str(folder).replace("~", os.getenv("HOME")).strip()
self._glob: str = glob.strip()
AskAiEvents.ASKAI_BUS.events.reply.emit(message=msg.summarizing(self.path))
Expand All @@ -77,7 +80,7 @@ def generate(self, folder: str | Path, glob: str = None) -> None:
llm=lc_llm.create_model(), chain_type="stuff", retriever=v_store.as_retriever())

def query(self, *queries: str) -> Optional[List[SummaryResult]]:
"""TODO"""
"""Answer questions about the summarized content."""
check_argument(len(queries) > 0)
if self._retriever is not None:
results: List[SummaryResult] = []
Expand All @@ -89,7 +92,7 @@ def query(self, *queries: str) -> Optional[List[SummaryResult]]:

@lru_cache
def query_one(self, query_string: str) -> Optional[SummaryResult]:
"""TODO"""
"""Query the AI about a given query based on the summarized content."""
check_argument(len(query_string) > 0)
if result := self._retriever.invoke({"query": query_string}):
return SummaryResult(
Expand All @@ -107,4 +110,5 @@ def query_one(self, query_string: str) -> Optional[SummaryResult]:
"What is HomeSetup?",
"How can I install HomeSetup?",
"How can I configure my Starship prompt?",
"How can change the starship preset?"))
"How can change the starship preset?",
"How can I use the punch application?"))
2 changes: 1 addition & 1 deletion src/main/askai/core/model/chat_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def join(self, *keys: str) -> ContextRaw:
content = ' '.join([t['content'] for t in self.get(key)])
token_length += len(content or '')
if token_length > self._token_limit:
raise TokenLengthExceeded(f"Required token length={token_length} limit={self._token_limit}")
raise TokenLengthExceeded(f"Required token length={token_length}k limit={self._token_limit}k")
if content:
context.extend(self.get(key))
return context
Expand Down
2 changes: 1 addition & 1 deletion src/main/askai/core/processor/processor_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def process(self, question: str) -> Tuple[bool, QueryResponse]:
final_prompt = msg.translate(template.format(query_types=self.query_types))
shared.context.set("SETUP", final_prompt, "system")
shared.context.set("QUESTION", question)
context: ContextRaw = shared.context.join("CONTEXT", "SETUP", "QUESTION")
context: ContextRaw = shared.context.join("INTERNET", "SUMMARY", "CONTEXT", "SETUP", "QUESTION")
log.info("Ask::[QUESTION] '%s' context=%s", question, context)

if (response := shared.engine.ask(context, temperature=0.0, top_p=0.0)) and response.is_success:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ Before responding to the user, it is imperative that you follow the step-by-step

- Start your response with the phrase: Analysing the provided data\n

- Wrap up your reply by offering a summarized analysis about the content; prefix with: 'Analysis:'.
- Wrap up your reply by offering a summarized analysis about the content; prefix with: \n'Analysis:'.
12 changes: 8 additions & 4 deletions src/main/askai/resources/assets/prompts/generic-prompt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ Before responding to the user, it is imperative that you follow the step-by-step

- The user's name is '{user}'. When addressing him, kindly utilize his name.

- Start your response with the correct answer for the question, never with a joke, fun fact or advice.
- When you receive a prompt containing an internet search output, start the answer with: According to Google .

- Sometimes, you can wrap up your response by dropping a fun fact about query or response; prefix with: 'Fun Fact:'.
- When you receive a prompt containing a summarized output, start the answer with: According to the summarized content .

- When the query doesn't necessitate seriousness, swap out the fun fact with a joke; prefix with: 'Joke:'.
- Start your response with the correct answer for the question, NEVER with a joke, fun fact or advice.

- When the query necessitate seriousness, swap out the fun fact or joke with an advice; prefix with: 'Advice:'.
- Sometimes, you can wrap up your response by dropping a fun fact about query or response; prefix with: \n'Fun Fact:'.

- When the query doesn't necessitate seriousness, swap out the fun fact with a joke; prefix with: \n'Joke:'.

- When the query necessitate seriousness, swap out the fun fact or joke with an advice; prefix with: \n'Advice:'.
2 changes: 1 addition & 1 deletion src/main/askai/resources/assets/prompts/output-prompt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ Before responding to the user, it is imperative that you follow the step-by-step

- Start your response with the phrase: Here is a summarized version of the provided data\n\n

- Wrap up your reply by offering a succinct hint or tip related to the topic; prefix with: 'Hints:'.
- Wrap up your reply by offering a succinct hint or tip related to the topic; prefix with: \n'Hints:'.

0 comments on commit 25fe495

Please sign in to comment.