Replies: 1 comment
-
Hi @jmandel, as a React developer myself, I find your proposal interesting. In today's AI workflows, prompts are becoming increasingly more complex. And a prompt might have many variants and subsections, so you might want to approach buiding a prompt the same way you would build a piece of UI with React. With the tooling that we currently have at Langchain, this is how I would build a complex prompt
Here is an example: const chain = new RunnablePassthrough<{
favorite: string;
isPromptEngineer: boolean;
}>()
.pipe({
question1: () => "What is your name?",
question2: PromptTemplate.fromTemplate("What is your favorite {favorite}"),
question3: RunnableLambda.from(({ isPromptEngineer }) =>
isPromptEngineer
? "What is your favorite LLM?"
: "What is your favorite color?"
),
})
.pipe(
PromptTemplate.fromTemplate(
"Act as a developer. Answer the following questions: {question1}, {question2}, {question3}"
)
); If you need to use a different prompt altogether based on some logic, you might want to use https://js.langchain.com/docs/how_to/routing#using-a-custom-function |
Beta Was this translation helpful? Give feedback.
-
I've been having a lot of fun getting started with langchainjs. I'm working on some chains + tools for validating coded terms in healthcare data.
In some situations, text-based prompt templates feel over-constrained, or hard to use idiomatically. Maybe it's from playing with web frameworks like React, but I've been thinking about "prompts as a function of state". I've got a simple demo of this working (using langchainjs
BasePromptTemplate
under the hood) where the prompt code is just the "function from state to chat history". Here's an example to show:In the end you use it like any other template, e.g.
Is this an approach worth pursing? Is it already covered by a core component that I'm missing, or could there be room for something like it langchain's core abstractions? Or am I just struggling because I haven't yet seen the light? :-) This can be hard to tell when first picking up a new framework... but at the same time, it's hard to look at a framework "fresh eyes" after you've been in it for a while.
Beta Was this translation helpful? Give feedback.
All reactions