This repository has been archived by the owner on Jan 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #399 from openchatai/fix/socrates_is_mortal
Adding extra llm call to create a standalone consolidated prompt
- Loading branch information
Showing
13 changed files
with
97 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,51 @@ | ||
from typing import List, cast | ||
from langchain.schema import BaseMessage, AIMessage, HumanMessage, SystemMessage | ||
from utils.get_chat_model import get_chat_model | ||
|
||
|
||
def get_last_4(arr): | ||
if arr is None or len(arr) == 0: | ||
return [] | ||
if len(arr) <= 4: | ||
return arr[-len(arr):] | ||
return arr[-4:] | ||
|
||
async def get_consolidate_question( | ||
conversation_history: List[BaseMessage], user_input: str | ||
) -> str: | ||
if len(conversation_history) == 0: | ||
return user_input | ||
|
||
conversation_str = "" | ||
for message in get_last_4(conversation_history): | ||
if message.type == "ai": | ||
conversation_str += f"Assistant: {message.content} \n" | ||
if message.type == "Human": | ||
conversation_str += f"Human: {message.content} \n" | ||
|
||
messages: List[BaseMessage] = [] | ||
messages.append(SystemMessage(content="You are a chat inspector, everytime you look at a new line of chat, you are able to piece references in the chat with information you looked at earlier, and output them.")) | ||
messages.append( | ||
HumanMessage( | ||
content="""Given a conversation history, replace occurrences of references like "it," "they," etc. in the latest input with their actual values. Remember, you are not supposed to answer the user question, merely enhance the latest user input | ||
Conversation history: | ||
--- | ||
User: Can you recommend a good restaurant nearby? | ||
Assistant: Sure, how about trying that Italian place on Main Street? It has great pasta. | ||
User input: What do they serve? | ||
--- | ||
Output: "What does the Italian place on Main Street serve?" | ||
""" | ||
) | ||
) | ||
|
||
messages.append(HumanMessage(content=f"Here's the conversation history: ```{conversation_history}```")) | ||
messages.append(HumanMessage(content=f"{user_input}")) | ||
|
||
chat = get_chat_model() | ||
result = await chat.ainvoke(messages) | ||
|
||
return cast(str, result.content) |
This file contains 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 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 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 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 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 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 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 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 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