forked from TelosLabs/langchainrb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_and_manage_prompt_templates.rb
25 lines (19 loc) · 1.33 KB
/
create_and_manage_prompt_templates.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
require "langchain"
# Create a prompt with one input variable
prompt = Langchain::Prompt::PromptTemplate.new(template: "Tell me a {adjective} joke.", input_variables: ["adjective"])
prompt.format(adjective: "funny") # "Tell me a funny joke."
# Create a prompt with multiple input variables
prompt = Langchain::Prompt::PromptTemplate.new(template: "Tell me a {adjective} joke about {content}.", input_variables: ["adjective", "content"])
prompt.format(adjective: "funny", content: "chickens") # "Tell me a funny joke about chickens."
# Creating a PromptTemplate using just a prompt and no input_variables
prompt = Langchain::Prompt::PromptTemplate.from_template("Tell me a {adjective} joke about {content}.")
prompt.input_variables # ["adjective", "content"]
prompt.format(adjective: "funny", content: "chickens") # "Tell me a funny joke about chickens."
# Save prompt template to JSON file
prompt.save(file_path: "spec/fixtures/prompt/prompt_template.json")
# Loading a new prompt template using a JSON file
prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/prompt_template.json")
prompt.input_variables # ["adjective", "content"]
# Loading a new prompt template using a YAML file
prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/prompt_template.yaml")
prompt.input_variables # ["adjective", "content"]