langchain-ai / langchain

πŸ¦œπŸ”— Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.4k stars 14.77k forks source link

Issue: I cannot seem to find how to make a System role message in my chain. #10608

Closed DonTizi closed 8 months ago

DonTizi commented 1 year ago

Issue you'd like to raise.

Hi ,

I'm trying to see how I can put a system message for my chain to tell him that for example "Your name is XX".

I've tried a lot of things and saw a lot of issues resolved and documentations but they never worked... Any help will be appreciated. here is the code of my chain.ts: ` import {OpenAI} from "langchain/llms/openai"; import {pinecone} from "@/utils/pinecone-client"; import {PineconeStore} from "langchain/vectorstores/pinecone"; import {OpenAIEmbeddings} from "langchain/embeddings/openai"; import {ConversationalRetrievalQAChain} from "langchain/chains"; import { PromptTemplate } from "langchain/prompts"; import { ChatOpenAI } from "langchain/chat_models/openai";

async function initChain() { const model = new ChatOpenAI({ modelName: "gpt-3.5-turbo", temperature: 0, });
const pineconeIndex = pinecone.Index('canada'); const vectorStore = await PineconeStore.fromExistingIndex( new OpenAIEmbeddings({}), { pineconeIndex: pineconeIndex, textKey: 'text', }, ); return ConversationalRetrievalQAChain.fromLLM( model, vectorStore.asRetriever(), {returnSourceDocuments: true} ); return ConversationalRetrievalQAChain.fromLLM( model, vectorStore.asRetriever(), {returnSourceDocuments: true}, ); }

export const chain = await initChain()`

Suggestion:

No response

dosubot[bot] commented 1 year ago

πŸ€–

Hello,

From your code, it seems you're using TypeScript. However, the LangChain framework is written in Python, and the solutions provided in the context are also in Python.

In Python, to include a system message in the chain, you would typically use the SystemMessagePromptTemplate.from_template() method. This method is used to create a system message from a string template. Here's an example of how it's used:

# Create a system message template
system_message_template = SystemMessagePromptTemplate.from_template("This is a system message")

# Now you can use this template to format a system message
formatted_message = system_message_template.format()

However, since you're using TypeScript, the equivalent functionality might not be directly available. The LangChain framework doesn't currently have a TypeScript version, and I couldn't find a TypeScript equivalent of the SystemMessagePromptTemplate.from_template() method in the LangChain repository.

You might need to implement a similar functionality in TypeScript yourself. You could create a function that takes a string template as an argument and returns a formatted system message. Here's a rough example of how you could do this:

function formatSystemMessage(template: string): string {
    // Format the template into a system message
    let formattedMessage = template;
    // Add any additional formatting logic here
    return formattedMessage;
}

// Use the function to format a system message
let systemMessage = formatSystemMessage("This is a system message");

Please note that this is a very basic example and you might need to add additional logic to handle more complex templates.

I hope this helps! If you have any other questions, feel free to ask.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a πŸ‘ if this is helpful and πŸ‘Ž if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

dosubot[bot] commented 9 months ago

Hi, @DonTizi,

I'm helping the LangChain team manage their backlog and am marking this issue as stale. The issue you raised about adding a system message to a chain seems to have been resolved with guidance from @dosubot on creating a system message in Python using the LangChain framework and a basic approach for implementing similar functionality in TypeScript. Relevant sources for further reference have also been shared.

Could you please confirm if this issue is still relevant to the latest version of the LangChain repository? If it is, please let the LangChain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or the issue will be automatically closed in 7 days.

Thank you!