Skip to content

Commit

Permalink
学习prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
coderyjr committed Jul 23, 2023
1 parent dc54c5b commit 44c8226
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ public static void main(String[] args) {
.init();

var result = chat.predictMessages(
List.of(new HumanMessage("Translate this sentence from English to French. I love programming.")));
List.of(new HumanMessage("Translate this sentence from English to Chinese. I love programming.")
,new HumanMessage("and this sentence. I love swimming.")));
println(result);

var output = chat.predict("Translate this sentence from English to French. I love programming.");
var output = chat.predict("and this sentence. I love swimming.");
println(output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

package com.hw.langchain.examples.prompt.templates;

import com.hw.langchain.chat.models.openai.ChatOpenAI;
import com.hw.langchain.examples.runner.RunnableExample;
import com.hw.langchain.llms.openai.OpenAI;
import com.hw.langchain.prompts.chat.ChatPromptTemplate;
import com.hw.langchain.prompts.chat.HumanMessagePromptTemplate;
import com.hw.langchain.prompts.chat.SystemMessagePromptTemplate;
import com.hw.langchain.schema.BaseMessage;

import java.util.List;
import java.util.Map;
Expand All @@ -35,15 +38,25 @@
public class ChatPromptTemplateExample {

public static void main(String[] args) {

//初始化
var chat = ChatOpenAI.builder()
.temperature(0)
.build()
.init();

var template = "You are a helpful assistant that translates {input_language} to {output_language}.";
var systemMessagePrompt = SystemMessagePromptTemplate.fromTemplate(template);

var humanTemplate = "{text}";
var humanMessagePrompt = HumanMessagePromptTemplate.fromTemplate(humanTemplate);

var chatPrompt = ChatPromptTemplate.fromMessages(List.of(systemMessagePrompt, humanMessagePrompt));
var output = chatPrompt.formatMessages(Map.of("input_language", "English", "output_language", "French",
var chatMessage = chatPrompt.formatMessages(Map.of("input_language", "English", "output_language", "Chinese",
"text", "I love programming."));
println(output);
BaseMessage baseMessage = chat.predictMessages(chatMessage);


println(baseMessage.getContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.hw.langchain.examples.prompt.templates;

import com.hw.langchain.examples.runner.RunnableExample;
import com.hw.langchain.llms.openai.OpenAI;
import com.hw.langchain.prompts.prompt.PromptTemplate;

import java.util.Map;
Expand All @@ -32,8 +33,18 @@
public class LlmPromptTemplateExample {

public static void main(String[] args) {

//初始化
var llm = OpenAI.builder()
.temperature(0.9f)
.build()
.init();
//构造模板
var prompt = PromptTemplate.fromTemplate("What is a good name for a company that makes {product}?");
var output = prompt.format(Map.of("product", "colorful socks"));
println(output);
var output = prompt.format(Map.of("product", "iceCreme"));

//调用API
String predict = llm.predict(output);
println(predict);
}
}

0 comments on commit 44c8226

Please sign in to comment.