Skip to content

Commit

Permalink
feat: Updated the subquery generator prompt!
Browse files Browse the repository at this point in the history
  • Loading branch information
amindadgar committed Apr 18, 2024
1 parent 24fd48f commit d8886b9
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
1 change: 0 additions & 1 deletion bot/retrievers/retrieve_similar_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def __init__(
"""Init params."""
self._vector_store = vector_store
self._embed_model = embed_model
print(f"type(embed_model): {type(embed_model)} | embed_model: {embed_model}")
self._similarity_top_k = similarity_top_k

def query_db(
Expand Down
6 changes: 5 additions & 1 deletion subquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from llama_index.llms.openai import OpenAI
from llama_index.question_gen.guidance import GuidanceQuestionGenerator
from tc_hivemind_backend.embeddings.cohere import CohereEmbedding
from utils.query_engine import prepare_discord_engine_auto_filter
from utils.query_engine import (
prepare_discord_engine_auto_filter,
DEFAULT_GUIDANCE_SUB_QUESTION_PROMPT_TMPL,
)


def query_multiple_source(
Expand Down Expand Up @@ -106,6 +109,7 @@ def query_multiple_source(
question_gen = GuidanceQuestionGenerator.from_defaults(
guidance_llm=OpenAIChat("gpt-4"),
verbose=False,
prompt_template_str=DEFAULT_GUIDANCE_SUB_QUESTION_PROMPT_TMPL,
)
s_engine = SubQuestionQueryEngine.from_defaults(
question_gen=question_gen,
Expand Down
1 change: 1 addition & 0 deletions utils/query_engine/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# flake8: noqa
from .prepare_discord_query_engine import prepare_discord_engine_auto_filter
from .subquery_gen_prompt import DEFAULT_GUIDANCE_SUB_QUESTION_PROMPT_TMPL
91 changes: 91 additions & 0 deletions utils/query_engine/subquery_gen_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import json
from typing import Sequence

from llama_index.core.prompts.base import PromptTemplate
from llama_index.core.prompts.guidance_utils import convert_to_handlebars
from llama_index.core.question_gen.types import SubQuestion
from llama_index.core.tools.types import ToolMetadata


# deprecated, kept for backward compatibility
SubQuestionPrompt = PromptTemplate


def build_tools_text(tools: Sequence[ToolMetadata]) -> str:
tools_dict = {}
for tool in tools:
tools_dict[tool.name] = tool.description
return json.dumps(tools_dict, indent=4)


PREFIX = """\
Given a user question, and a list of tools, output a list of relevant sub-questions \
in json markdown that when composed can help answer the full user question. \
Define the sub-questions as search queries that can be used for vector similarity search:
"""


example_query_str = (
"What was decided about the token allocation budget for the "
"next airdrop and what did the community think of this?"
)
example_tools = [
ToolMetadata(
name="Discord",
description="Contains messages and summaries of conversations from the Discord platform of the community",
),
ToolMetadata(
name="Discourse",
description="Contains messages and summaries of discussions from the Discourse platform of the community",
),
]
example_tools_str = build_tools_text(example_tools)
example_output = [
SubQuestion(
sub_question="Decision token allocation budget airdrop", tool_name="Discourse"
),
SubQuestion(
sub_question="Opinion token allocation budget airdrop", tool_name="Discord"
),
]
example_output_str = json.dumps({"items": [x.dict() for x in example_output]}, indent=4)

EXAMPLES = f"""\
# Example 1
<Tools>
```json
{example_tools_str}
```
<User Question>
{example_query_str}
<Output>
```json
{example_output_str}
```
""".replace(
"{", "{{"
).replace(
"}", "}}"
)

SUFFIX = """\
# Example 2
<Tools>
```json
{tools_str}
```
<User Question>
{query_str}
<Output>
"""

DEFAULT_SUB_QUESTION_PROMPT_TMPL = PREFIX + EXAMPLES + SUFFIX
DEFAULT_GUIDANCE_SUB_QUESTION_PROMPT_TMPL = convert_to_handlebars(
DEFAULT_SUB_QUESTION_PROMPT_TMPL
)

0 comments on commit d8886b9

Please sign in to comment.