| 50 | |
| 51 | |
| 52 | class MessageManage: |
| 53 | def __init__(self, conversation_ids: list[str]): |
| 54 | self.conversation_ids = conversation_ids |
| 55 | |
| 56 | self.conversation_map = {} |
| 57 | for conversation_id in self.conversation_ids: |
| 58 | self.conversation_map[conversation_id] = ConversationManager.query_by_idWithSort(conversation_id=conversation_id) |
| 59 | |
| 60 | self.memory_map = {} |
| 61 | for conversation_id, conversations in self.conversation_map.items(): |
| 62 | memory = ConversationMemory() |
| 63 | for conversation in conversations: |
| 64 | memory.addQuestion(conversation.content) |
| 65 | memory.addAnswer(conversation.content) |
| 66 | self.memory_map[conversation_id] = memory |
| 67 | |
| 68 | self.llmChain_map = {} |
| 69 | for conversation_id, conversations in self.conversation_map.items(): |
| 70 | template = """You are a chatbot having a conversation with a human. |
| 71 | |
| 72 | {chat_history} |
| 73 | Human: {human_input} |
| 74 | Chatbot:""" |
| 75 | |
| 76 | prompt = PromptTemplate( |
| 77 | input_variables=["chat_history", "human_input"], template=template |
| 78 | ) |
| 79 | memory = ConversationBufferMemory(memory_key="chat_history") |
| 80 | llm = OpenAI( |
| 81 | temperature=st.session_state["temperature"], |
| 82 | openai_api_key=st.session_state["openai_api_key"]) |
| 83 | llm_chain = LLMChain( |
| 84 | llm=llm, |
| 85 | prompt=prompt, |
| 86 | verbose=True, |
| 87 | memory=memory, |
| 88 | ) |
| 89 | self.llmChain_map[conversation_id] = llm_chain |
| 90 | |
| 91 | def add_conversation_id(self, conversation_id: str): |
| 92 | conversations = ConversationManager.query_by_idWithSort(conversation_id=conversation_id) |
| 93 | self.conversation_map[conversation_id] = conversations |
| 94 | |
| 95 | def add_conversation(self, conversation_id: str, role_name: str, content: str): |
| 96 | conversation_id_res = ConversationManager.add(conversation_id=conversation_id, role_name=role_name, content=content) |
| 97 | conversation_id_tmp = ConversationManager.query_latest_id(conversation_id=conversation_id) |
| 98 | if conversation_id_res == conversation_id_tmp: |
| 99 | self.conversation_map[conversation_id] = ConversationManager.query_by_idWithSort(conversation_id=conversation_id) |
| 100 | |
| 101 |
nothing calls this directly
no outgoing calls
no test coverage detected