Skip to content

Commit

Permalink
🔀✨ (AI) destination`s description text generate by GPT Completition
Browse files Browse the repository at this point in the history
Merge branch 'feat/openai'
  • Loading branch information
ecureuill committed Aug 11, 2023
2 parents 2325642 + df8f56f commit e0c9033
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
</properties>
<dependencies>

<dependency>
<groupId>com.theokanning.openai-gpt3-java</groupId>
<artifactId>service</artifactId>
<version>0.15.0</version>
</dependency>

<dependency>
<groupId>net.datafaker</groupId>
<artifactId>datafaker</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;

import ecureuill.milhasapi.infra.openai.GptGuideService;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand Down Expand Up @@ -37,7 +38,12 @@ public Destination(@Valid DestinationCreateRecord record) {
this.photo2 = record.photo2();
this.Price = record.price();
this.meta = record.meta();
this.description = record.description();
this.description = record.description();

if(description == null || description == ""){
GptGuideService gpt = new GptGuideService();
this.description = gpt.generate(this.name);
}
}

public void update(@Valid DestinationUpdateRecord record) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ecureuill.milhasapi.infra.openai;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.theokanning.openai.completion.chat.ChatCompletionRequest;
import com.theokanning.openai.completion.chat.ChatMessage;
import com.theokanning.openai.completion.chat.ChatMessageRole;
import com.theokanning.openai.service.OpenAiService;

@Service
public class GptGuideService {

public String generate(String destination){
OpenAiService service = new OpenAiService(System.getenv("OPENAI_KEY"));

List<ChatMessage> messages = new ArrayList<>();

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));

ChatCompletionRequest completion = ChatCompletionRequest
.builder()
.model("gpt-3.5-turbo")
.messages(messages)
.maxTokens(200)
.build();

ChatMessage response = service.createChatCompletion(completion).getChoices().get(0).getMessage();

return response.getContent();
}
}

0 comments on commit e0c9033

Please sign in to comment.