-
Notifications
You must be signed in to change notification settings - Fork 183
feat: Make suggest next questions configurable #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0c60f27
add env settings for suggest questions feature
leehuwuj 2221b50
add changeset
leehuwuj a82424a
fix linting
leehuwuj a9ff7d0
refactor
leehuwuj 0d7077e
remove structurellm
leehuwuj 05d646f
update for ts
leehuwuj c18a517
constraint name
leehuwuj 1c91107
remove pydantic settings
leehuwuj 9b788f4
refactor: clean code
marcusschiesser 06a201e
update wrong prompt placeholder pattern
leehuwuj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"create-llama": patch | ||
--- | ||
|
||
Add env config for next questions feature |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 7 additions & 19 deletions
26
templates/components/llamaindex/typescript/streaming/suggestion.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
leehuwuj marked this conversation as resolved.
Show resolved
Hide resolved
|
File renamed without changes.
leehuwuj marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import logging | ||
import os | ||
import re | ||
from typing import List, Optional | ||
|
||
from app.api.routers.models import Message | ||
from llama_index.core.prompts import PromptTemplate | ||
from llama_index.core.settings import Settings | ||
|
||
logger = logging.getLogger("uvicorn") | ||
|
||
|
||
class NextQuestionSuggestion: | ||
""" | ||
Suggest the next questions that user might ask based on the conversation history | ||
Disable this feature by removing the NEXT_QUESTION_PROMPT environment variable | ||
""" | ||
|
||
@classmethod | ||
def get_configured_prompt(cls) -> Optional[str]: | ||
prompt = os.getenv("NEXT_QUESTION_PROMPT", None) | ||
if not prompt: | ||
return None | ||
return PromptTemplate(prompt) | ||
|
||
@classmethod | ||
async def suggest_next_questions_all_messages( | ||
cls, | ||
messages: List[Message], | ||
) -> Optional[List[str]]: | ||
""" | ||
Suggest the next questions that user might ask based on the conversation history | ||
Return None if suggestion is disabled or there is an error | ||
""" | ||
prompt_template = cls.get_configured_prompt() | ||
if not prompt_template: | ||
return None | ||
|
||
try: | ||
# Reduce the cost by only using the last two messages | ||
last_user_message = None | ||
last_assistant_message = None | ||
for message in reversed(messages): | ||
if message.role == "user": | ||
last_user_message = f"User: {message.content}" | ||
elif message.role == "assistant": | ||
last_assistant_message = f"Assistant: {message.content}" | ||
if last_user_message and last_assistant_message: | ||
break | ||
conversation: str = f"{last_user_message}\n{last_assistant_message}" | ||
|
||
# Call the LLM and parse questions from the output | ||
prompt = prompt_template.format(conversation=conversation) | ||
output = await Settings.llm.acomplete(prompt) | ||
questions = cls._extract_questions(output.text) | ||
|
||
return questions | ||
except Exception as e: | ||
logger.error(f"Error when generating next question: {e}") | ||
return None | ||
|
||
@classmethod | ||
def _extract_questions(cls, text: str) -> List[str]: | ||
content_match = re.search(r"```(.*?)```", text, re.DOTALL) | ||
content = content_match.group(1) if content_match else "" | ||
return content.strip().split("\n") | ||
|
||
@classmethod | ||
async def suggest_next_questions( | ||
cls, | ||
chat_history: List[Message], | ||
response: str, | ||
) -> List[str]: | ||
""" | ||
Suggest the next questions that user might ask based on the chat history and the last response | ||
""" | ||
messages = chat_history + [Message(role="assistant", content=response)] | ||
return await cls.suggest_next_questions_all_messages(messages) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 0 additions & 60 deletions
60
templates/types/multiagent/fastapi/app/api/services/suggestion.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.