langchain
langchain copied to clipboard
Issue: Is it possible to add system message with the prompt?
Issue you'd like to raise.
I created an AgentExecutor with the ConversationalChatAgent and I could pass a system message as I initialize the agent executor. Is it possible to add system messages to individual prompts, not just one in the beginning? My code:
from langchain import PromptTemplate
from langchain.agents import ConversationalChatAgent, Tool, AgentExecutor
import pickle
import os
import datetime
import logging
# from controllers.user_controller import UserController
from langchain.llms import OpenAI
from langchain.document_loaders import DirectoryLoader
from langchain.text_splitter import CharacterTextSplitter
# from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chains import RetrievalQA
class ChatController(object):
def __init__(self):
self._create_chat_agent()
def _create_chat_agent(self):
self.llm = OpenAI(temperature=0, top_p=0.2, presence_penalty=0.4, frequency_penalty=0.2)
embeddings = OpenAIEmbeddings()
persist_directory = 'myvectordb'
vectorstore = Chroma(persist_directory=persist_directory, embedding_function = embeddings)
prompt_template = """If the context is not relevant,
please answer the question by using your own knowledge about the topic
{context}
Question: {question}
"""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
chain_type_kwargs = {"prompt": PROMPT}
# Initialise Langchain - QA chain
qa = RetrievalQA.from_chain_type(llm=self.llm,
chain_type="stuff",
retriever=vectorstore.as_retriever(),
chain_type_kwargs=chain_type_kwargs)
tools = [
Tool(
name="Document tool",
func=qa.run,
description="useful for when you need to answer questions."
),
]
system_msg = "You are a helpful assistant."
agent = ConversationalChatAgent.from_llm_and_tools(
llm=self.llm,
tools=tools,
system_message=system_msg
)
self.chat_agent = AgentExecutor.from_agent_and_tools(
agent=agent, tools=tools, verbose=True, memory=ConversationBufferMemory(memory_key="chat_history",
return_messages=True)
)
def askAI(self, prompt: str):
response = self.chat_agent.run(input=prompt)
return {"answer": response}
Suggestion:
No response
I do not completely understand what are you trying to solve, so I can not give you an answer, but check out Steercode, which can answer you questions about LangChain repository.
Hi, @pelyhe! I'm Dosu, and I'm here to help the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.
From what I understand, you opened this issue asking if it is possible to add system messages to individual prompts in the ConversationalChatAgent. You mentioned that you would like to add it to each prompt, rather than just one in the beginning. There has been one comment from votrumar, who suggests checking out Steercode for answers about the LangChain repository.
Before we close this issue, we wanted to check with you if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself or it will be automatically closed in 7 days.
Thank you for your contribution to the LangChain repository!