Skip to content

Commit e0c9033

Browse files
author
ecureuill
committed
🔀✨ (AI) destination`s description text generate by GPT Completition
Merge branch 'feat/openai'
2 parents 2325642 + df8f56f commit e0c9033

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

‎pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
</properties>
1919
<dependencies>
2020

21+
<dependency>
22+
<groupId>com.theokanning.openai-gpt3-java</groupId>
23+
<artifactId>service</artifactId>
24+
<version>0.15.0</version>
25+
</dependency>
26+
2127
<dependency>
2228
<groupId>net.datafaker</groupId>
2329
<artifactId>datafaker</artifactId>

‎src/main/java/ecureuill/milhasapi/domain/destination/Destination.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.math.BigDecimal;
44

5+
import ecureuill.milhasapi.infra.openai.GptGuideService;
56
import jakarta.persistence.Entity;
67
import jakarta.persistence.GeneratedValue;
78
import jakarta.persistence.GenerationType;
@@ -37,7 +38,12 @@ public Destination(@Valid DestinationCreateRecord record) {
3738
this.photo2 = record.photo2();
3839
this.Price = record.price();
3940
this.meta = record.meta();
40-
this.description = record.description();
41+
this.description = record.description();
42+
43+
if(description == null || description == ""){
44+
GptGuideService gpt = new GptGuideService();
45+
this.description = gpt.generate(this.name);
46+
}
4147
}
4248

4349
public void update(@Valid DestinationUpdateRecord record) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ecureuill.milhasapi.infra.openai;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.stereotype.Service;
7+
8+
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
9+
import com.theokanning.openai.completion.chat.ChatMessage;
10+
import com.theokanning.openai.completion.chat.ChatMessageRole;
11+
import com.theokanning.openai.service.OpenAiService;
12+
13+
@Service
14+
public class GptGuideService {
15+
16+
public String generate(String destination){
17+
OpenAiService service = new OpenAiService(System.getenv("OPENAI_KEY"));
18+
19+
List<ChatMessage> messages = new ArrayList<>();
20+
21+
messages.add(new ChatMessage(ChatMessageRole.USER.value(), "I want you to act as a travel guide. I will write you a location and you will write a 200 character text about this location, it's highlights and unique experiences. My first request sugestion is " + destination));
22+
23+
ChatCompletionRequest completion = ChatCompletionRequest
24+
.builder()
25+
.model("gpt-3.5-turbo")
26+
.messages(messages)
27+
.maxTokens(200)
28+
.build();
29+
30+
ChatMessage response = service.createChatCompletion(completion).getChoices().get(0).getMessage();
31+
32+
return response.getContent();
33+
}
34+
}

0 commit comments

Comments
 (0)