code
stringlengths
419
138k
apis
sequencelengths
1
8
extract_api
stringlengths
67
7.3k
package com.siwonkh.cleangpt_v1.controller; import com.siwonkh.cleangpt_v1.dto.SearchSessionDto; import com.siwonkh.cleangpt_v1.entity.SearchSession; import com.siwonkh.cleangpt_v1.model.CreatorVideo; import com.siwonkh.cleangpt_v1.model.VideoComment; import com.siwonkh.cleangpt_v1.model.CreatorProfile; import com.siwonkh.cleangpt_v1.repository.SearchSessionRepository; import com.siwonkh.cleangpt_v1.service.SearchService; import com.siwonkh.cleangpt_v1.util.SearchCommentThread; import com.siwonkh.cleangpt_v1.util.SearchCreatorProfile; import com.siwonkh.cleangpt_v1.util.SearchCreatorVideo; 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; import org.json.JSONArray; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.List; @Controller public class SearchController { @Value("${youtubeapi.key}") private String APIKey; @Value("${openai.key}") private String OpenAIKey; @Autowired private SearchService searchService; @Autowired private SearchSessionRepository searchSessionRepository; @GetMapping("search/creator") public String searchCreator(Model model, @RequestParam("creator") String creator) throws Exception { SearchCreatorProfile searchCreatorProfile = new SearchCreatorProfile(APIKey); searchCreatorProfile.setChannel(creator); JSONObject channels = searchCreatorProfile.getApiResponse(); JSONArray jsonArray = channels.getJSONArray("items"); List<String> searchedTitles = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("snippet"); searchedTitles.add(jsonObject.getString("title")); } List<String> searchedDesc = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("snippet"); searchedDesc.add(jsonObject.getString("description")); } List<String> searchedImages = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("default"); searchedImages.add(jsonObject.getString("url")); } List<String> searchedChannelIds = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("snippet"); searchedChannelIds.add(jsonObject.getString("channelId")); } List<String> searchedPublishedAt = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("snippet"); searchedPublishedAt.add(jsonObject.getString("publishedAt")); } List<CreatorProfile> creatorProfiles = new ArrayList<>(); for (int i = 0; i < searchedTitles.size(); i++) { String title = searchedTitles.get(i); String desc = searchedDesc.get(i); String url = searchedImages.get(i); String channelId = searchedChannelIds.get(i); String publishedAt = searchedPublishedAt.get(i); CreatorProfile creatorProfile = new CreatorProfile(title, desc, url, channelId, publishedAt.substring(0, 10)); creatorProfiles.add(creatorProfile); } model.addAttribute("profile", creatorProfiles.get(0)); return "searchResult"; } @GetMapping("search/comment") public String searchComments(Model model, @CookieValue("TOKEN") String token, @RequestParam("video") String video, @RequestParam("title") String title, @RequestParam("thumbnail") String thumbnail, @RequestParam(value = "filter", required = false, defaultValue = "") String filterParams, @RequestParam(value = "cfilter", required = false, defaultValue = "") String cFilter) throws Exception { SearchCommentThread searchCommentThread = new SearchCommentThread(APIKey); searchCommentThread.setVideo(video); JSONObject comments = searchCommentThread.getApiResponse(); JSONArray jsonArray = comments.getJSONArray("items"); List<String> searchedComments = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("snippet").getJSONObject("topLevelComment").getJSONObject("snippet"); searchedComments.add(jsonObject.getString("textOriginal").replaceAll(",", "")); } String commentsStr = String.join(",", searchedComments); SearchSession searchSession = searchService.getSearchSessionByToken(token); String filter = searchSession.getFilters(); // String filter = filterParams + "," + cFilter; System.out.println(filter); List<String> filterArray = new ArrayList<>(); if (filter != null) { if (filter.length() > 3) { filterArray = List.of(filter.split(",")); ChatMessage chatMessage = new ChatMessage(); chatMessage.setRole(ChatMessageRole.SYSTEM.value()); chatMessage.setContent("You should filter list of comments based on the filter list. And output should be same format as before."); ChatMessage chatMessage2 = new ChatMessage(); chatMessage2.setRole(ChatMessageRole.USER.value()); chatMessage2.setContent( "filter list: " + "Spam,Hate speech,comments more than 40 chars" + "\n" + "comments: " + "I really don't like them.,https://asdhioe.com come to this link!,1020304050607080900010203040506070809000ad,This video is cool!,lets gooo,wow" ); ChatMessage chatMessage3 = new ChatMessage(); chatMessage3.setRole(ChatMessageRole.ASSISTANT.value()); chatMessage3.setContent("This video is cool!,lets gooo,wow"); ChatMessage chatMessage4 = new ChatMessage(); chatMessage4.setRole(ChatMessageRole.USER.value()); chatMessage4.setContent( "filter list: " + filter + "\n" + "comments: " + commentsStr ); List<ChatMessage> chatMessages = new ArrayList<>(); chatMessages.add(chatMessage); chatMessages.add(chatMessage2); chatMessages.add(chatMessage3); chatMessages.add(chatMessage4); OpenAiService service = new OpenAiService(OpenAIKey); ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() .model("gpt-3.5-turbo-16k") .messages(chatMessages) .build(); String reply = service.createChatCompletion(completionRequest).getChoices().get(0).getMessage().getContent(); System.out.println(reply); // Type type = new TypeToken<List<VideoComment>>() {}.getType(); // videoComments = new Gson().fromJson(videoCommentsJson, type); searchedComments = List.of(reply.split(",")); model.addAttribute("customs", filter.replace("spam,", "").replace("abuse,", "").replace("hatespeech,", "").split(",")); } } List<String> searchedCommentAuthors = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("snippet").getJSONObject("topLevelComment").getJSONObject("snippet"); searchedCommentAuthors.add(jsonObject.getString("authorDisplayName")); } List<String> searchedCommentAuthorImages = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("snippet").getJSONObject("topLevelComment").getJSONObject("snippet"); searchedCommentAuthorImages.add(jsonObject.getString("authorProfileImageUrl")); } List<VideoComment> videoComments = new ArrayList<>(); for (int i = 0; i < searchedComments.size()-1; i++) { String comment = searchedComments.get(i); String author = searchedCommentAuthors.get(i); String url = searchedCommentAuthorImages.get(i); VideoComment videoComment = new VideoComment(comment, author, url); videoComments.add(videoComment); } model.addAttribute("comments", videoComments); model.addAttribute("videoTitle", title); model.addAttribute("videoThumbnail", thumbnail); model.addAttribute("commentCount", videoComments.size()); model.addAttribute("hasSpam", filterArray.contains("spam")); model.addAttribute("hasAbuse", filterArray.contains("abuse")); model.addAttribute("hasHatespeech", filterArray.contains("hatespeech")); return "comment"; } @PostMapping("search/start") public String createSearchSession(HttpServletResponse response, @RequestParam("creatorId") String creatorId) throws Exception { SearchSessionDto searchSessionDto = new SearchSessionDto(); searchSessionDto.setCreatorId(creatorId); searchSessionDto = searchService.createSearchSession(searchSessionDto); Cookie cookie = new Cookie("TOKEN", searchSessionDto.getSearchSessionToken()); cookie.setPath("/"); response.addCookie(cookie); return "redirect:/videos"; } @PostMapping("search/comment/filter/add") public void addFilter(HttpServletResponse response, @CookieValue("TOKEN") String token, @RequestParam("filter") String filter) throws Exception { SearchSession searchSession = searchService.getSearchSessionByToken(token); String filters = searchSession.getFilters(); List<String> filterArray = new ArrayList<>(); if (filters != null) { filterArray = new ArrayList<>(List.of(filters.split(","))); } if (!filterArray.contains(filter)) { filterArray.add(filter); } searchSession.setFilters(String.join(",", filterArray)); searchSessionRepository.save(searchSession); } @PostMapping("search/comment/filter/remove") public void removeFilter(HttpServletResponse response, @CookieValue("TOKEN") String token, @RequestParam("filter") String filter) throws Exception { SearchSession searchSession = searchService.getSearchSessionByToken(token); String filters = searchSession.getFilters(); List<String> filterArray = new ArrayList<>(); if (filters != null) { filterArray = new ArrayList<>(List.of(filters.split(","))); } filterArray.remove(filter); searchSession.setFilters(String.join(",", filterArray)); searchSessionRepository.save(searchSession); } @GetMapping("videos") public String searchCreatorVideos(Model model, @CookieValue("TOKEN") String token) throws Exception { SearchSession searchSession = searchService.getSearchSessionByToken(token); String creatorId = searchSession.getCreatorId(); SearchCreatorVideo searchCreatorVideo = new SearchCreatorVideo(APIKey); searchCreatorVideo.setChannelId(creatorId); JSONObject videos = searchCreatorVideo.getApiResponse(); JSONArray jsonArray = videos.getJSONArray("items"); List<String> searchedVideoIds = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("id"); searchedVideoIds.add(jsonObject.getString("videoId")); } List<String> searchedTitles = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("snippet"); searchedTitles.add(jsonObject.getString("title")); } List<String> searchedDescription = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("snippet"); searchedDescription.add(jsonObject.getString("description")); } List<String> searchedThumbnails = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("medium"); searchedThumbnails.add(jsonObject.getString("url")); } List<String> searchedPublishedAt = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("snippet"); searchedPublishedAt.add(jsonObject.getString("publishedAt")); } List<CreatorVideo> creatorVideos = new ArrayList<>(); for (int i = 0; i < searchedTitles.size(); i++) { String videoIds = searchedVideoIds.get(i); String title = searchedTitles.get(i); String description = searchedDescription.get(i); String thumbnail = searchedThumbnails.get(i); String publishedAt = searchedPublishedAt.get(i); CreatorVideo creatorVideo = new CreatorVideo(videoIds, title, description, thumbnail, publishedAt.substring(0, 10)); creatorVideos.add(creatorVideo); } model.addAttribute("videos", creatorVideos); return "video"; } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((5939, 5969), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((6220, 6248), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((6700, 6733), 'com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value'), ((6914, 6942), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((7512, 7676), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((7512, 7643), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((7512, 7595), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package com.tailoredshapes.boobees.repositories; import com.amazonaws.xray.AWSXRay; import com.amazonaws.xray.AWSXRayRecorderBuilder; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.tailoredshapes.boobees.model.Prompt; import com.theokanning.openai.completion.chat.ChatCompletionChoice; 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.embedding.Embedding; import com.theokanning.openai.embedding.EmbeddingRequest; import com.theokanning.openai.embedding.EmbeddingResult; import com.theokanning.openai.service.OpenAiService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; import static com.tailoredshapes.underbar.ocho.UnderBar.map; public class Assistant { public static final String MODEL = System.getenv("MODEL") != null ? System.getenv("MODEL") : "gpt-3.5-turbo"; static { AWSXRayRecorderBuilder builder = AWSXRayRecorderBuilder.standard(); AWSXRay.setGlobalRecorder(builder.build()); } private static final Logger LOG = LogManager.getLogger(Assistant.class); private final OpenAiService openAIClient; private final String systemPrompt; public final String failMessage; private final MessageRepo repo; public Assistant(OpenAiService openAIClient, String failMessage, MessageRepo repo, String systemPrompt) { this.repo = repo; this.openAIClient = openAIClient; this.systemPrompt = systemPrompt; this.failMessage = failMessage; LOG.info("Using: %s".formatted(MODEL)); } public String answer(List<String> prompts, Long chatId) { ChatMessage systemPrompt = new ChatMessage(ChatMessageRole.SYSTEM.value(), this.systemPrompt); ChatMessage formatPrompt = new ChatMessage(ChatMessageRole.SYSTEM.value(), "Please use markdown for formatting and emphasis, feel free to use emoji."); LOG.info("Using personality: %s".formatted(systemPrompt)); List<Prompt> lastN = repo.findN(chatId, 30, prompts.get(prompts.size() - 1)); Collections.reverse(lastN); LOG.info("Found %d items for context".formatted(lastN.size())); List<ChatMessage> chatPrompts = map(prompts, (m) -> new ChatMessage(ChatMessageRole.USER.value(), m)); List<ChatMessage> aiPrompts = lastN.stream().map( (p) -> new ChatMessage(ChatMessageRole.valueOf(p.role().toUpperCase()).value(), p.prompt())).collect(Collectors.toList()); aiPrompts.add(formatPrompt); aiPrompts.add(systemPrompt); aiPrompts.addAll(chatPrompts); try { LOG.debug("Prompts sent to AI: \n" + new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(aiPrompts)); } catch (JsonProcessingException e) { LOG.error("Can't display prompts", e); } ChatCompletionRequest completionRequest = ChatCompletionRequest.builder().model(MODEL).messages(aiPrompts).build(); String message = failMessage; try(var ss = AWSXRay.beginSubsegment("Calling OpenAI")){ try { List<ChatCompletionChoice> choices = openAIClient.createChatCompletion(completionRequest).getChoices(); if(choices.size() > 0){ ChatMessage answer = choices.get(0).getMessage(); message = answer.getContent(); chatPrompts.add(answer); } List<Prompt> ps = chatPrompts.stream().map((cm) -> new Prompt(cm.getRole(), cm.getContent())).toList(); repo.createAll(chatId, ps); } catch (Exception e) { LOG.error("OpenAI is screwing around again", e); ss.addException(e); } } return message; } public CompletableFuture<String> answerAsync(List<String> prompts, Long chatId) { return CompletableFuture.supplyAsync(() -> answer(prompts, chatId)); } public List<List<Double>> embed(List<Prompt> prompt) { EmbeddingRequest embeddingRequest = EmbeddingRequest.builder().model("text-embedding-ada-002").input(prompt.stream().map(Prompt::prompt).toList()).build(); EmbeddingResult embeddings = openAIClient.createEmbeddings(embeddingRequest); List<Embedding> data = embeddings.getData(); return data.stream().map(Embedding::getEmbedding).toList(); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder", "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.embedding.EmbeddingRequest.builder", "com.theokanning.openai.completion.chat.ChatMessageRole.valueOf", "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value" ]
[((2013, 2043), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((2116, 2146), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((2567, 2595), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((2684, 2739), 'com.theokanning.openai.completion.chat.ChatMessageRole.valueOf'), ((3203, 3275), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3203, 3267), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3203, 3247), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4377, 4495), 'com.theokanning.openai.embedding.EmbeddingRequest.builder'), ((4377, 4487), 'com.theokanning.openai.embedding.EmbeddingRequest.builder'), ((4377, 4435), 'com.theokanning.openai.embedding.EmbeddingRequest.builder')]
package cn.shu.wechat; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.theokanning.openai.OpenAiApi; import com.theokanning.openai.OpenAiService; import com.theokanning.openai.completion.CompletionChoice; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.completion.CompletionResult; import okhttp3.*; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.jackson.JacksonConverterFactory; import java.io.IOException; import java.util.Optional; import java.util.concurrent.TimeUnit; import static java.time.Duration.ofSeconds; public class AI2 { private static final String BASE_URL = "https://api.openai.com/"; public static void main(String[] args) { String token = "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"; ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request() .newBuilder() .header("Authorization", "Bearer " + token) .build(); return chain.proceed(request); } }) .connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS)) .readTimeout(ofSeconds(20).toMillis(), TimeUnit.MILLISECONDS) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .client(client) .addConverterFactory(JacksonConverterFactory.create(mapper)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); OpenAiService service = new OpenAiService(retrofit.create(OpenAiApi.class)); CompletionRequest completionRequest = CompletionRequest.builder() .prompt("导师call我了") .maxTokens(1024) .model("text-davinci-003") .echo(true) .build(); CompletionResult completion = service.createCompletion(completionRequest); Optional<String> result = completion.getChoices().stream().findFirst().map(CompletionChoice::getText); if (result.isPresent()) { String[] split = result.get().split("\n\n"); System.out.println(split[1]); } completion.getChoices().forEach(System.out::println); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((2467, 2667), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2467, 2642), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2467, 2614), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2467, 2571), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2467, 2538), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package io.github.lynbean.lynbot.cogs.openai.chatbox; import java.io.BufferedInputStream; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessageRole; import io.github.lynbean.lynbot.cogs.openai.chatbox.pojo.ChatBoxPreset; import io.reactivex.functions.BiConsumer; import lombok.Getter; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import net.dv8tion.jda.api.interactions.components.buttons.Button; import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; public class ChatBox { private static final Logger LOG = LoggerFactory.getLogger(ChatBox.class); private static final String CHATBOX_PREFIX = "ChatBox"; private static final String IGNORE_ANNOTATION = "> ` @IGNORE `"; private @Getter final ThreadChannel channel; private @Getter ChatBoxPreset preset; public ChatBox(ThreadChannel channel) { this.channel = channel; try { retrievePresetFromChat(); } catch (RuntimeException e) { LOG.error("Failed to retrieve preset from chat, will use default preset instead.", e); channel.sendMessage(getIgnoreAnnotatedMessage("Failed to retrieve preset, will use default preset instead.")) .queue(); this.preset = ChatBoxDatabaseManager.getDefaultChatBoxPreset(); } } public ChatBox(ThreadChannel channel, String json) { this.channel = channel; this.preset = ChatBoxPreset.fromJson(json); } public ChatBox(ThreadChannel channel, ChatBoxPreset preset) { this.channel = channel; this.preset = preset; } public ChatBox(ThreadChannel channel, String title, String description, String personality, String characterIconUrl, String characterName) { this.channel = channel; this.preset = new ChatBoxPreset() .setTitle(title) .setDescription(description) .setPersonality(personality) .setCharacterIconUrl(characterIconUrl) .setCharacterName(characterName); } public static ChatBox create(Message message, User user) { return create(message, user, ChatBoxDatabaseManager.getDefaultChatBoxPreset()); } public static ChatBox create(Message message, User user, String presetId) { ChatBoxPreset preset = ChatBoxDatabaseManager.findChatBoxPreset(String.valueOf(presetId)); return create(message, user, preset); } public static ChatBox create(Message message, User user, ChatBoxPreset preset) { ThreadChannel channel = message.createThreadChannel(CHATBOX_PREFIX).complete(); ChatBox chatBox = new ChatBox(channel, preset); chatBox.lockChannel(); channel.addThreadMember(user).queue(); channel.getManager().setSlowmode(5).queue(); // A way to prevent spamming and causing error occurred channel.sendMessage( new MessageCreateBuilder() .setContent(getIgnoreAnnotatedMessage("Do NOT delete this message. Created by " + user.getAsMention() + ".")) .setActionRow(Button.danger("openai.chatbox.delete_channel", "If you hate me, click here to delete me.")) .setFiles(preset.getJsonFile(message.getId())) .build() ) .queue(); chatBox.unLockChannel(); return chatBox; } private void retrievePresetFromChat() { Optional<Message> chatPreset = channel.getHistoryFromBeginning(5) .complete() .getRetrievedHistory() .stream() .filter( message -> { if (!message.getAttachments().isEmpty()) return message.getAttachments() .get(0) .getFileName() .startsWith(CHATBOX_PREFIX); return false; } ) .findFirst(); chatPreset.get() .getAttachments() .get(0) .getProxy() .download() .thenAccept( inputStream -> { String json; try ( BufferedInputStream bis = new BufferedInputStream(inputStream) ) { byte[] bytes = new byte[10240]; int bytesRead = bis.read(bytes, 0, bytes.length); json = new String(bytes, 0, bytesRead); } catch (Exception e) { throw new RuntimeException(e); } this.preset = ChatBoxPreset.fromJson(json); } ) .join(); } private List<Message> retrieveMessages() { return channel.getIterableHistory() // TODO: Integrate this into the preset message to let user configure this .takeAsync(15) // Prevents exceeding model's max token limit .thenApply(messages -> messages.stream().collect(Collectors.toList())) .join(); } private List<ChatMessage> getDialogues() { if (preset == null) { retrievePresetFromChat(); } List<ChatMessage> dialogues = preset.getPresetMessages(); retrieveMessages() .stream() .filter(message -> !message.getContentRaw().startsWith(IGNORE_ANNOTATION) && // Exclude out not ChatBox messages !message.getAuthor().isSystem() && // Exclude out system messages !message.isEphemeral() && // Exclude out ephemeral messages ( // Exclude out messages without content or embeds !message.getContentRaw().isEmpty() || !message.getEmbeds().isEmpty() ) ) .sorted((a, b) -> a.getTimeCreated().compareTo(b.getTimeCreated())) .forEachOrdered( message -> { ChatMessage chatMessage = new ChatMessage(); try { chatMessage.setContent( message.getAuthor().isBot() ? message.getEmbeds().get(0).getDescription() // Probably only one embed, it's hardcoded : message.getContentRaw() ); chatMessage.setRole( message.getAuthor().isBot() ? ChatMessageRole.ASSISTANT.value() : ChatMessageRole.USER.value() ); dialogues.add(chatMessage); } catch (Exception e) { // We will just ignore this message so that it won't break the chat // But we will log it for debugging purposes e.printStackTrace(); } } ); return dialogues; } public void execute(BiConsumer<List<ChatMessage>, ChatBoxPreset> action) { lockChannel(); try { action.accept(getDialogues(), preset); } catch (Exception e) { throw new RuntimeException(e); } finally { unLockChannel(); } } public static String getIgnoreAnnotatedMessage(String message) { return String.format("%s (%s)", IGNORE_ANNOTATION, message); } private void lockChannel() { channel.getManager() .setLocked(true) .queue(); } private void unLockChannel() { channel.getManager() .setLocked(false) .queue(); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value" ]
[((6833, 6866), 'com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value'), ((6901, 6929), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value')]
package com.chatgpt.example.dto.request; import com.theokanning.openai.completion.CompletionRequest; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @Getter @NoArgsConstructor @AllArgsConstructor public class GPTCompletionRequest { private String model; private String prompt; private Integer maxToken; public static CompletionRequest of(GPTCompletionRequest restRequest) { return CompletionRequest.builder() .model(restRequest.getModel()) .prompt(restRequest.getPrompt()) .maxTokens(restRequest.getMaxToken()) .build(); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((441, 611), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((441, 594), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((441, 548), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((441, 507), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package me.acclashcorporation; // [START speech_transcribe_infinite_streaming] import com.google.api.gax.rpc.ClientStream; import com.google.api.gax.rpc.ResponseObserver; import com.google.api.gax.rpc.StreamController; import com.google.cloud.speech.v1p1beta1.*; import com.google.cloud.texttospeech.v1.*; import com.google.protobuf.ByteString; import com.google.protobuf.Duration; import com.theokanning.openai.OpenAiApi; import com.theokanning.openai.OpenAiService; import com.theokanning.openai.completion.CompletionRequest; import me.acclashcorporation.utils.FineTune; import me.acclashcorporation.utils.SampleTTS; import javax.sound.sampled.*; import javax.sound.sampled.DataLine.Info; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; public class JamesGPT { private static final int STREAMING_LIMIT = 290000; // ~5 minutes public static final String RED = "\033[0;31m"; public static final String GREEN = "\033[0;32m"; public static final String YELLOW = "\033[0;33m"; // Creating shared object private static volatile BlockingQueue<byte[]> sharedQueue = new LinkedBlockingQueue(); private static TargetDataLine targetDataLine; private static int BYTES_PER_BUFFER = 6400; // buffer size in bytes private static int restartCounter = 0; private static ArrayList<ByteString> audioInput = new ArrayList<ByteString>(); private static ArrayList<ByteString> lastAudioInput = new ArrayList<ByteString>(); private static int resultEndTimeInMS = 0; private static int isFinalEndTime = 0; private static int finalRequestEndTime = 0; private static boolean newStream = true; private static double bridgingOffset = 0; private static boolean lastTranscriptWasFinal = false; private static StreamController referenceToStreamController; private static ByteString tempByteString; private static StringBuilder conversation = new StringBuilder(); public static void main(String[] args) throws Exception { //FineTune.fineTune(); startConversation(); //SampleTTS.sampleTest(); } public static String convertMillisToDate(double milliSeconds) { long millis = (long) milliSeconds; DecimalFormat format = new DecimalFormat(); format.setMinimumIntegerDigits(2); return String.format( "%s:%s /", format.format(TimeUnit.MILLISECONDS.toMinutes(millis)), format.format( TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)))); } /** * Performs infinite streaming speech recognition */ public static void infiniteStreamingRecognize() throws Exception { // Microphone Input buffering class MicBuffer implements Runnable { @Override public void run() { System.out.println(YELLOW); System.out.println("Start speaking...Press Ctrl-C to stop"); targetDataLine.start(); byte[] data = new byte[BYTES_PER_BUFFER]; while (targetDataLine.isOpen()) { try { int numBytesRead = targetDataLine.read(data, 0, data.length); if ((numBytesRead <= 0) && (targetDataLine.isOpen())) { continue; } sharedQueue.put(data.clone()); } catch (InterruptedException e) { System.out.println("Microphone input buffering interrupted : " + e.getMessage()); } } } } // Creating microphone input buffer thread MicBuffer micrunnable = new MicBuffer(); Thread micThread = new Thread(micrunnable); ResponseObserver<StreamingRecognizeResponse> responseObserver = null; try (SpeechClient client = SpeechClient.create()) { ClientStream<StreamingRecognizeRequest> clientStream; responseObserver = new ResponseObserver<StreamingRecognizeResponse>() { ArrayList<StreamingRecognizeResponse> responses = new ArrayList<>(); public void onStart(StreamController controller) { referenceToStreamController = controller; } public void onResponse(StreamingRecognizeResponse response) { responses.add(response); StreamingRecognitionResult result = response.getResultsList().get(0); Duration resultEndTime = result.getResultEndTime(); resultEndTimeInMS = (int) ((resultEndTime.getSeconds() * 1000) + (resultEndTime.getNanos() / 1000000)); double correctedTime = resultEndTimeInMS - bridgingOffset + (STREAMING_LIMIT * restartCounter); SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); if (result.getIsFinal()) { System.out.print(GREEN); System.out.print("\033[2K\r"); System.out.printf( "%s: %s [confidence: %.2f]\n", convertMillisToDate(correctedTime), alternative.getTranscript(), alternative.getConfidence()); isFinalEndTime = resultEndTimeInMS; lastTranscriptWasFinal = true; // Handle human's response if (alternative.getTranscript().toLowerCase().contains("james") || conversation.toString().endsWith("?")) { addMessage(alternative.getTranscript()); try { getResponse(); } catch (Exception e) { throw new RuntimeException(e); } } } else { System.out.print(RED); System.out.print("\033[2K\r"); System.out.printf( "%s: %s", convertMillisToDate(correctedTime), alternative.getTranscript()); lastTranscriptWasFinal = false; } } public void onComplete() {} public void onError(Throwable t) {} }; clientStream = client.streamingRecognizeCallable().splitCall(responseObserver); //ArrayList<String> languageList = new ArrayList<>(); //languageList.add("de-DE"); //languageList.add("es-MX"); RecognitionConfig recognitionConfig = RecognitionConfig.newBuilder() .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16) .setLanguageCode("en-US") //.addAllAlternativeLanguageCodes(languageList) .setSampleRateHertz(16000) .build(); StreamingRecognitionConfig streamingRecognitionConfig = StreamingRecognitionConfig.newBuilder() .setConfig(recognitionConfig) .setInterimResults(true) .build(); StreamingRecognizeRequest request = StreamingRecognizeRequest.newBuilder() .setStreamingConfig(streamingRecognitionConfig) .build(); // The first request in a streaming call has to be a config clientStream.send(request); try { // SampleRate:16000Hz, SampleSizeInBits: 16, Number of channels: 1, Signed: true, // bigEndian: false AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false); DataLine.Info targetInfo = new Info( TargetDataLine.class, audioFormat); // Set the system information to read from the microphone audio // stream if (!AudioSystem.isLineSupported(targetInfo)) { System.out.println("Microphone not supported"); System.exit(0); } // Target data line captures the audio stream the microphone produces. targetDataLine = (TargetDataLine) AudioSystem.getLine(targetInfo); targetDataLine.open(audioFormat); micThread.start(); long startTime = System.currentTimeMillis(); while (true) { long estimatedTime = System.currentTimeMillis() - startTime; if (estimatedTime >= STREAMING_LIMIT) { clientStream.closeSend(); referenceToStreamController.cancel(); // remove Observer if (resultEndTimeInMS > 0) { finalRequestEndTime = isFinalEndTime; } resultEndTimeInMS = 0; lastAudioInput = null; lastAudioInput = audioInput; audioInput = new ArrayList<ByteString>(); restartCounter++; if (!lastTranscriptWasFinal) { System.out.print('\n'); } newStream = true; clientStream = client.streamingRecognizeCallable().splitCall(responseObserver); request = StreamingRecognizeRequest.newBuilder() .setStreamingConfig(streamingRecognitionConfig) .build(); System.out.println(YELLOW); System.out.printf("%d: RESTARTING REQUEST\n", restartCounter * STREAMING_LIMIT); startTime = System.currentTimeMillis(); } else { if ((newStream) && (lastAudioInput.size() > 0)) { // if this is the first audio from a new request // calculate amount of unfinalized audio from last request // resend the audio to the speech client before incoming audio double chunkTime = STREAMING_LIMIT / lastAudioInput.size(); // ms length of each chunk in previous request audio arrayList if (chunkTime != 0) { if (bridgingOffset < 0) { // bridging Offset accounts for time of resent audio // calculated from last request bridgingOffset = 0; } if (bridgingOffset > finalRequestEndTime) { bridgingOffset = finalRequestEndTime; } int chunksFromMs = (int) Math.floor((finalRequestEndTime - bridgingOffset) / chunkTime); // chunks from MS is number of chunks to resend bridgingOffset = (int) Math.floor((lastAudioInput.size() - chunksFromMs) * chunkTime); // set bridging offset for next request for (int i = chunksFromMs; i < lastAudioInput.size(); i++) { request = StreamingRecognizeRequest.newBuilder() .setAudioContent(lastAudioInput.get(i)) .build(); clientStream.send(request); } } newStream = false; } tempByteString = ByteString.copyFrom(sharedQueue.take()); request = StreamingRecognizeRequest.newBuilder().setAudioContent(tempByteString).build(); audioInput.add(tempByteString); } clientStream.send(request); } } catch (Exception e) { System.out.println(e); } } } public static void startConversation() throws Exception { AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("startup.wav").getAbsoluteFile()); Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); clip.start(); conversation = new StringBuilder(""" The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly. The assistant's name is James. He was created by AC Clash. AI: I am an AI created by AC Clash. How can I help you today? Human: Hi, who are you? AI:"""); getResponse(); try { infiniteStreamingRecognize(); } catch (Exception e) { throw new RuntimeException(e); } } public static void getResponse() throws Exception { OpenAiService service = new OpenAiService("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", 0); CompletionRequest request = CompletionRequest.builder() .prompt(conversation.toString()) .model("gpt-4") //Use the latest davinci model .temperature(0.90) //How creative the AI should be .maxTokens(150) //How many tokens the AI should generate. Tokens are words, punctuation, etc. .topP(1.0) //How much diversity the AI should have. 1.0 is the most diverse .frequencyPenalty(0.0) //How much the AI should avoid repeating itself .presencePenalty(0.6) //How much the AI should avoid repeating the same words .stop(List.of("Human:", "AI:")) //Stop the AI from generating more text when it sees these words .build(); var choices = service.createCompletion(request).getChoices(); var response = choices.get(0).getText(); //what the AI responds with conversation.append(response.stripLeading()); System.out.print(GREEN); System.out.println(response.stripLeading()); speak(response.stripLeading()); } public static void addMessage(String transcript) { conversation.append("\nHuman:").append(transcript).append("\nAI:"); } public static void speak(String output) throws Exception { // Instantiates a client try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { // Set the text input to be synthesized SynthesisInput input = SynthesisInput.newBuilder().setText(output).build(); // Build the voice request, select the language code ("en-US") and the ssml voice gender // ("neutral") VoiceSelectionParams voice = VoiceSelectionParams.newBuilder() .setLanguageCode("en-GB") .setName("en-GB-Neural2-B") .setSsmlGender(SsmlVoiceGender.MALE) .build(); // Select the type of audio file you want returned AudioConfig audioConfig = AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.LINEAR16).build(); // Perform the text-to-speech request on the text input with the selected voice parameters and // audio file type SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice, audioConfig); // Get the audio contents from the response ByteString audioContents = response.getAudioContent(); // Write the response to the output file. try (OutputStream out = new FileOutputStream("output.wav")) { out.write(audioContents.toByteArray()); //System.out.println("Audio content written to file \"output.wav\""); } AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("output.wav").getAbsoluteFile()); Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); clip.start(); } } } // [END speech_transcribe_infinite_streaming]
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((2627, 2666), 'java.util.concurrent.TimeUnit.MILLISECONDS.toMinutes'), ((2724, 2763), 'java.util.concurrent.TimeUnit.MILLISECONDS.toSeconds'), ((2798, 2865), 'java.util.concurrent.TimeUnit.MINUTES.toSeconds'), ((2825, 2864), 'java.util.concurrent.TimeUnit.MILLISECONDS.toMinutes'), ((14651, 15378), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((14651, 15288), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((14651, 15184), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((14651, 15098), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((14651, 14994), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((14651, 14889), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((14651, 14825), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((14651, 14759), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((14651, 14727), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package wood.util; import com.theokanning.openai.OpenAiService; import com.theokanning.openai.completion.CompletionChoice; import com.theokanning.openai.completion.CompletionRequest; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; import java.util.List; import java.util.Optional; /** A wrapper class for com.theokanning.openai. Requires GPTRequest.apiKey to be set. */ @Slf4j public class GPTRequest { // ----------- static fields ----------- /** The OpenAI API key to use for all requests. Can set using the testAndSetApiKey method. */ public static String apiKey = ""; /** Language models */ public static final String davinci = "davinci", curie = "curie", babbage = "babbage", ada = "ada", inDavinci = "text-davinci-002", inCurie = "text-curie-001", inBabbage = "text-babbage-001", inAda = "text-ada-001"; /** counter for how many tokens have been used by each language model (irrespective of Base series vs Instruct) */ private static int davinciTokenCounter = 0, curieTokenCounter = 0, babbageTokenCounter = 0, adaTokenCounter = 0; // ----------- instance fields ----------- private final OpenAiService service; private final CompletionRequest completionRequest; private final CompletionRequest.CompletionRequestBuilder completionRequestBuilder; /** The prompt to use for this API request */ @Getter private final String prompt; /** Language Model to use for this API request */ @Getter private final String model; /** Maximum number of tokens use in the API request (including the prompt). */ @Getter private final int maxTokens; /** (default .7) a value 0-1 with 1 being very creative, 0 being very factual/deterministic */ @Getter private final double temperature; /** (default 1) between 0-1 where 1.0 means "use all tokens in the vocabulary" * while 0.5 means "use only the 50% most common tokens" */ @Getter private final double topP; /** (default 0) 0-1, lowers the chances of a word being selected again the more times that word has already been used */ @Getter private final double frequencyPenalty; /** (default 0) 0-1, lowers the chances of topic repetition */ @Getter private final double presencePenalty; /** Echo back the prompt in addition to the completion. */ @Getter private final boolean echoPrompt; /** (default 1), queries GPT-3 this many times, then selects the 'best' generation to return */ @Getter private final int bestOf; /** The Strings that GPT-3 will stop generating after (can have 4 stop sequences max) */ @Getter private final List<String> stopSequences; public GPTRequest(GPTRequestBuilder builder) { this.prompt = builder.prompt; this.model = builder.model; this.maxTokens = builder.maxTokens; this.temperature = builder.temperature; this.topP = builder.topP; this.frequencyPenalty = builder.frequencyPenalty; this.presencePenalty = builder.presencePenalty; this.echoPrompt = builder.echoPrompt; this.bestOf = builder.bestOf; this.stopSequences = builder.stopSequences; service = new OpenAiService(apiKey); completionRequestBuilder = CompletionRequest.builder() .prompt(prompt); completionRequestBuilder.maxTokens(maxTokens); completionRequestBuilder.temperature(temperature); completionRequestBuilder.topP(topP); completionRequestBuilder.frequencyPenalty(frequencyPenalty); completionRequestBuilder.presencePenalty(presencePenalty); completionRequestBuilder.echo(echoPrompt); if(stopSequences != null) completionRequestBuilder.stop(stopSequences); completionRequest = completionRequestBuilder.build(); } /** * Tests the API key, and sets it if it's valid * API key validity is tested by a 1 token API request to the Ada model. * @param apiKey An OpenAI API key * @return Whether the API key is valid */ public static boolean testAndSetApiKey(String apiKey) { String originalAPIKey = GPTRequest.apiKey; try { GPTRequest.apiKey = apiKey; new GPTRequestBuilder(ada, "", 1, false).build().request(); return true; }catch(Exception e) { GPTRequest.apiKey = originalAPIKey; return false; } } /** * Makes an OpenAI API request. * @return If echoPrompt is true, returns the prompt + completion, else the completion is returned. */ public String request() { logTokenUsage(maxTokens); List<CompletionChoice> outputList = service.createCompletion(model, completionRequest).getChoices(); return outputList.get(0).getText(); } /** * Makes an OpenAI API request. * @param endAtLastPunctuationMark Whether the completion should be cut off after the last punctuation mark * @return If echoPrompt is true, returns the prompt + completion, else the completion is returned. */ public String request(boolean endAtLastPunctuationMark) { String output = request(); if(endAtLastPunctuationMark) { // get the index of the last punctuation mark inside the completion (omitting the prompt) Optional<Integer> lastPunctuationIndex = StringUtil.lastIndexOf(output, "[.!?]", echoPrompt ? prompt.length() : 0); if(lastPunctuationIndex.isPresent()) return output.substring(0, lastPunctuationIndex.get() + 1); } return output; } public static class GPTRequestBuilder { /** Language Model to use for this API request */ @Getter private String model; /** The prompt to use for this API request */ @Getter private String prompt; /** Maximum number of tokens use in the API request (including the prompt). */ @Getter private int maxTokens; /** (default false) Echo back the prompt in addition to the completion. */ @Getter private boolean echoPrompt; /** (default .7) a value 0-1 with 1 being very creative, 0 being very factual/deterministic */ @Getter private double temperature; /** (default 1) between 0-1 where 1.0 means "use all tokens in the vocabulary" * while 0.5 means "use only the 50% most common tokens" */ @Getter private double topP; /** (default 0) 0-1, lowers the chances of a word being selected again the more times that word has already been used */ @Getter private double frequencyPenalty; /** (default 0) 0-1, lowers the chances of topic repetition */ @Getter private double presencePenalty; /** (default 1), queries GPT-3 this many times, then selects the 'best' generation to return */ @Getter private int bestOf; /** The Strings that GPT-3 will stop generating after (can have 4 stop sequences max) */ @Getter private List<String> stopSequences; /** * Starts to build an API request for the given language model * * @param model Language model to use for this API request. Valid Base Series models: * UtilGPT.davinci, UtilGPT.curie, UtilGPT.babbage, UtilGPT.ada * Valid Instruct Series models: * UtilGPT.inDavinci, UtilGPT.inCurie, UtilGPT.inBabbage, UtilGPT.inAda * @param prompt Prompt sent to the language model * @param maxTokens Maximum number of tokens use in the API request */ public GPTRequestBuilder(String model, String prompt, int maxTokens) { this.model = model; this.prompt = prompt; this.maxTokens = maxTokens; this.temperature = .7; this.topP = 1; this.frequencyPenalty = 0; this.presencePenalty = 0; this.echoPrompt = false; this.bestOf = 1; } /** * Starts to build an API request for the given language model * * @param model Language model to use for this API request. Valid Base Series models: * UtilGPT.davinci, UtilGPT.curie, UtilGPT.babbage, UtilGPT.ada * Valid Instruct Series models: * UtilGPT.inDavinci, UtilGPT.inCurie, UtilGPT.inBabbage, UtilGPT.inAda * @param prompt Prompt sent to the language model * @param maxTokens Maximum number of tokens use in the API request * @param addPromptTokensToMaxTokens Whether the number of tokens in the prompt should be added to maxTokens */ public GPTRequestBuilder(String model, String prompt, int maxTokens, boolean addPromptTokensToMaxTokens) { this.model = model; this.prompt = prompt; this.maxTokens = addPromptTokensToMaxTokens ? maxTokens + GPTUtil.countTokens(prompt) : maxTokens; this.temperature = .7; this.topP = 1; this.frequencyPenalty = 0; this.presencePenalty = 0; this.echoPrompt = false; this.bestOf = 1; } public GPTRequest build() { return new GPTRequest(this); } /** @param prompt Prompt sent to the language model * @return This GPTRequestBuilder, for chaining */ public GPTRequestBuilder prompt(String prompt) { this.prompt = prompt; return this; } /** @param prompt Prompt sent to the language model * @param maxTokens Maximum number of tokens use in the API request * @param addPromptTokensToMaxTokens Whether the number of tokens in the prompt should be added to maxTokens * @return This GPTRequestBuilder, for chaining */ public GPTRequestBuilder promptAndTokens(String prompt, int maxTokens, boolean addPromptTokensToMaxTokens) { this.prompt = prompt; this.maxTokens = addPromptTokensToMaxTokens ? maxTokens + GPTUtil.countTokens(prompt) : maxTokens; return this; } /** * @param model Language model to use for this API request. Valid Base Series models: * UtilGPT.davinci, UtilGPT.curie, UtilGPT.babbage, UtilGPT.ada * Valid Instruct Series models: * UtilGPT.inDavinci, UtilGPT.inCurie, UtilGPT.inBabbage, UtilGPT.inAda * @return This GPTRequestBuilder, for chaining */ public GPTRequestBuilder model(String model) { this.model = model; return this; } /** * @param maxTokens The total number of tokens use in the API request (including the prompt). * @return This GPTRequestBuilder, for chaining */ public GPTRequestBuilder maxTokens(int maxTokens) { this.maxTokens = maxTokens; return this; } /** @param temperature (default .7) a value 0-1 with 1 being very creative, 0 being very factual/deterministic * @return This GPTRequestBuilder, for chaining */ public GPTRequestBuilder temperature(double temperature) { this.temperature = temperature; return this; } /** @param topP (default 1) between 0-1 where 1.0 means "use all tokens in the vocabulary" * while 0.5 means "use only the 50% most common tokens" * @return This GPTRequestBuilder, for chaining */ public GPTRequestBuilder topP(double topP) { this.topP = topP; return this; } /** @param frequencyPenalty (default 0) 0-1, lowers the chances of a word being selected again * the more times that word has already been used * @return This GPTRequestBuilder, for chaining */ public GPTRequestBuilder frequencyPenalty(double frequencyPenalty) { this.frequencyPenalty = frequencyPenalty; return this; } /** @param presencePenalty (default 0) 0-1, lowers the chances of topic repetition * @return This GPTRequestBuilder, for chaining */ public GPTRequestBuilder presencePenalty(double presencePenalty) { this.presencePenalty = presencePenalty; return this; } /** @param bestOf (default 1), queries GPT-3 this many times, then selects the 'best' generation to return * @return This GPTRequestBuilder, for chaining */ public GPTRequestBuilder bestOf(int bestOf) { this.bestOf = bestOf; return this; } /** * set the stop sequence, the String that GPT-3 will stop generating after * (can have 4 stop sequences max) * @param stopSequences The Strings that GPT-3 will stop generating after (can have 4 stop sequences max) * @return This GPTRequestBuilder, for chaining */ public GPTRequestBuilder stopSequences(List<String> stopSequences) { if(stopSequences.size() > 4) throw new IllegalArgumentException("Can only have 4 stop sequences max"); else this.stopSequences = stopSequences; return this; } /** @param echoPrompt Whether to echo back the prompt in addition to the completion. * @return This GPTRequestBuilder, for chaining */ public GPTRequestBuilder echoPrompt(boolean echoPrompt) { this.echoPrompt = echoPrompt; return this; } } /** * Logs the token usage every time request() is called. * @param numTokens The number of tokens used in this API request. */ private void logTokenUsage(int numTokens) { switch(model) { case davinci: case inDavinci: davinciTokenCounter += numTokens; break; case curie: case inCurie: curieTokenCounter += numTokens; break; case babbage: case inBabbage: babbageTokenCounter += numTokens; break; case ada: case inAda: adaTokenCounter += numTokens; break; } log.info(String.format("Total tokens used:%n%s%s%s%s-----------------------------------------%n", davinciTokenCounter > 0 ? "Davinci: " + davinciTokenCounter + " token" + (davinciTokenCounter > 1 ? "s\n" : "\n") : "", curieTokenCounter > 0 ? "Curie: " + curieTokenCounter + " token" + (curieTokenCounter > 1 ? "s\n" : "\n") : "", babbageTokenCounter > 0 ? "Babbage: " + babbageTokenCounter + " token" + (babbageTokenCounter > 1 ? "s\n" : "\n") : "", adaTokenCounter > 0 ? "Ada: " + adaTokenCounter + " token" + (adaTokenCounter > 1 ? "s\n" : "\n") : "")); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((3276, 3335), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package com.vission.chatGPT.config; import com.fasterxml.jackson.databind.ObjectMapper; import com.theokanning.openai.OpenAiApi; import com.theokanning.openai.service.OpenAiService; import com.vission.chatGPT.properties.ChatGPTProperties; import java.net.InetSocketAddress; import java.net.Proxy; import java.time.Duration; import lombok.RequiredArgsConstructor; import okhttp3.OkHttpClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import retrofit2.Retrofit; @Configuration @RequiredArgsConstructor @Import(ChatGPTProperties.class) public class ChatGPTConfig { private final ChatGPTProperties properties; private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(10); @Bean public OpenAiService openAiService() { ObjectMapper mapper = OpenAiService.defaultObjectMapper(); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(properties.getProxyHostName(), properties.getProxyPort())); OkHttpClient client = OpenAiService.defaultClient(properties.getApiKey(), Duration.ofSeconds(500)) .newBuilder() .proxy(proxy) .build(); Retrofit retrofit = OpenAiService.defaultRetrofit(client, mapper); OpenAiApi api = retrofit.create(OpenAiApi.class); return new OpenAiService(api); } // @Bean // public OpenAiClient openAiClient() { // //代理可以为null // Proxy proxy = new Proxy(Proxy.Type.HTTP, // new InetSocketAddress(properties.getProxyHostName(), properties.getProxyPort())); // //日志输出可以不添加 // List<Interceptor> interceptors = new ArrayList<>(); // if (properties.getLog()) { // HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger()); // httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); // interceptors.add(httpLoggingInterceptor); // } // return OpenAiClient.builder() // .apiKey(properties.getApiKey()) // .connectTimeout(properties.getConnectTimeout()) // .writeTimeout(properties.getWriteTimeout()) // .readTimeout(properties.getReadTimeout()) // .interceptor(interceptors) // .proxy(proxy) // .apiHost(properties.getApiHost()) // .build(); // } }
[ "com.theokanning.openai.service.OpenAiService.defaultClient" ]
[((1114, 1275), 'com.theokanning.openai.service.OpenAiService.defaultClient'), ((1114, 1250), 'com.theokanning.openai.service.OpenAiService.defaultClient'), ((1114, 1220), 'com.theokanning.openai.service.OpenAiService.defaultClient')]
package com.prometheus.vord.service; import com.theokanning.openai.image.CreateImageRequest; import com.theokanning.openai.image.Image; import com.theokanning.openai.service.OpenAiService; import jakarta.annotation.Resource; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service @RequiredArgsConstructor public class GPTService { @Resource(name = "getOpenAiService") private final OpenAiService openAiService; public List<String> generatePicture(String prompt) { CreateImageRequest createImageRequest = CreateImageRequest.builder() .prompt(prompt) .size("512x512") .n(3) .build(); List<String> list = new ArrayList<>(); for(Image i : openAiService.createImage(createImageRequest).getData()){ list.add(i.getUrl()); } return list; } }
[ "com.theokanning.openai.image.CreateImageRequest.builder" ]
[((620, 760), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((620, 735), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((620, 713), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((620, 680), 'com.theokanning.openai.image.CreateImageRequest.builder')]
package br.com.alura.screenMatch.service; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.service.OpenAiService; public class ConsultaChatGPT { public static String obterTraducao(String texto) { OpenAiService service = new OpenAiService("Sua chave API do chatGPT aqui"); CompletionRequest requisicao = CompletionRequest.builder() .model("text-davinci-003") .prompt("traduza para o português o texto: " + texto) .maxTokens(1000) .temperature(0.7) .build(); var resposta = service.createCompletion(requisicao); return resposta.getChoices().get(0).getText(); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((371, 564), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((371, 547), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((371, 521), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((371, 496), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((371, 433), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package data_access; import com.theokanning.openai.audio.CreateSpeechRequest; 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; import javazoom.jl.decoder.JavaLayerException; import javazoom.jl.player.advanced.AdvancedPlayer; import okhttp3.ResponseBody; import services.generate_password.GeneratePasswordDataAccessInterface; import services.suggest_reply.SuggestReplyDataAccessInterface; import services.text_to_speech.TextToSpeechDataAccessInterface; import services.translate_message.TranslateMessageDataAccessInterface; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * This class represents a data access object for generating secure passwords using the GPT model * from OpenAI. */ public class GPTDataAccessObject implements GeneratePasswordDataAccessInterface, TextToSpeechDataAccessInterface, SuggestReplyDataAccessInterface, TranslateMessageDataAccessInterface { private final OpenAiService service; /** * Initializes a new GPTDataAccessObject with the provided OpenAI API key. * * @param openaiApiKey The API key for accessing the OpenAI service. */ public GPTDataAccessObject(String openaiApiKey) { this.service = new OpenAiService(openaiApiKey); } private static void playMP3(InputStream inputStream) { try { AdvancedPlayer player = new AdvancedPlayer(inputStream); // Start playing the MP3 file player.play(); } catch (JavaLayerException e) { e.printStackTrace(); } } /** * Get the OpenAiService instance associated with this data access object. * * @return The OpenAiService instance used for interacting with the OpenAI API. */ public OpenAiService getService() { return service; } /** * Generates a secure password based on the provided prompt using the GPT model. * * @param prompt The prompt for generating the password. * @return The generated secure password as a string. */ @Override public String generateSecurePassword(String prompt) { List<ChatMessage> messages = new ArrayList<>(); ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), prompt); messages.add(userMessage); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder().model("gpt-4").messages( messages).maxTokens(10).build(); try { ChatMessage responseMessage = service.createChatCompletion(chatCompletionRequest).getChoices().get( 0).getMessage(); return responseMessage.getContent(); } catch (Exception e) { return "Error: Failed to generate a password."; } } /** * Generates audio from the provided text using a text-to-speech service and plays it. * * @param message The text to be converted into audio. * @return {@code true} if the audio generation and playback were successful; {@code false} * otherwise. * @throws IOException If an I/O error occurs during the processing of the audio content. */ @Override public boolean generateAudio(String message) { CreateSpeechRequest createSpeechRequest = CreateSpeechRequest.builder().model("tts-1").input(message).voice( "alloy").build(); try { ResponseBody speech = service.createSpeech(createSpeechRequest); // Play the MP3 directly without saving to a file playMP3(new ByteArrayInputStream(speech.bytes())); return true; } catch (IOException e) { e.printStackTrace(); return false; } } /** * Generates a suggested reply based on the provided prompt and last message of the other user * using the GPT model. * * @param prompt The prompt for generating the suggested reply. * @return The generated suggested reply as a string. */ @Override public String generateSuggestedReply(String prompt) { List<ChatMessage> messages = new ArrayList<>(); ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), prompt); messages.add(userMessage); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder().model("gpt-4").messages( messages).build(); try { ChatMessage responseMessage = service.createChatCompletion(chatCompletionRequest).getChoices().get( 0).getMessage(); return responseMessage.getContent(); } catch (Exception e) { return "Error: Failed to suggest a reply."; } } /** * Translates the given text into the specified target language using the GPT-4 language model. * * @param textToTranslate The text to be translated. * @param targetLanguage The target language into which the text should be translated. * @return The translated text or an error message if the translation fails. */ @Override public String translate(String textToTranslate, String targetLanguage) { List<ChatMessage> messages = new ArrayList<>(); String prompt = String.format("Translate the following into %s: %s", targetLanguage, textToTranslate); ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), prompt); messages.add(userMessage); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder().model("gpt-4").messages(messages) // No maxTokens set to let it depend on the input text .build(); try { ChatMessage responseMessage = service.createChatCompletion(chatCompletionRequest).getChoices().get( 0).getMessage(); return responseMessage.getContent(); } catch (Exception e) { return "Error: Failed to generate a translation."; } } }
[ "com.theokanning.openai.audio.CreateSpeechRequest.builder", "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((2481, 2509), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((2610, 2714), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((2610, 2706), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((2610, 2692), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((2610, 2656), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3534, 3633), 'com.theokanning.openai.audio.CreateSpeechRequest.builder'), ((3534, 3625), 'com.theokanning.openai.audio.CreateSpeechRequest.builder'), ((3534, 3593), 'com.theokanning.openai.audio.CreateSpeechRequest.builder'), ((3534, 3578), 'com.theokanning.openai.audio.CreateSpeechRequest.builder'), ((4442, 4470), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((4571, 4661), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4571, 4653), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4571, 4617), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((5642, 5670), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((5771, 5932), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((5771, 5836), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((5771, 5817), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package io.github.lynbean.lynbot.cogs.openai.chat; import java.util.List; import com.google.common.base.Optional; import com.theokanning.openai.completion.chat.ChatCompletionChunk; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatCompletionResult; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessageRole; import io.github.lynbean.lynbot.Bot; import io.github.lynbean.lynbot.cogs.openai.core.OpenAiCore; import io.reactivex.functions.Action; import io.reactivex.functions.Consumer; import lombok.Getter; import lombok.Setter; import lombok.experimental.Accessors; @Accessors(chain = true) public class Chat extends OpenAiCore { private @Getter @Setter Double frequencyPenalty; private @Getter @Setter Double presencePenalty; private @Getter @Setter Double temperature; private @Getter @Setter Double topP; private @Getter @Setter Integer maxTokens; private @Getter List<ChatMessage> messages; private @Getter @Setter String model; private @Getter @Setter String user; public static Chat fromString(String content) { ChatMessage message = new ChatMessage(); message.setContent(content); message.setRole(ChatMessageRole.USER.value()); return new Chat(message); } public Chat(ChatMessage message) { super(); this.messages = List.of(message); } public Chat(List<ChatMessage> messages) { super(); this.messages = messages; } private ChatCompletionRequest createCompletionRequest() { return ChatCompletionRequest.builder() .frequencyPenalty( Optional.fromNullable(frequencyPenalty) .or(Bot.getConfig().getDouble("openai.chat.frequency_penalty")) ) .maxTokens( Optional.fromNullable(maxTokens) .or(Bot.getConfig().getInt("openai.chat.max_tokens")) ) .messages(messages) .model( Optional.fromNullable(model) .or(Bot.getConfig().getString("openai.chat.default_model")) ) .presencePenalty( Optional.fromNullable(presencePenalty) .or(Bot.getConfig().getDouble("openai.chat.presence_penalty")) ) .topP( Optional.fromNullable(topP) .or(Bot.getConfig().getDouble("openai.chat.top_p")) ) .user(user) .build(); } public ChatCompletionResult complete() { return OPEN_AI_SERVICE.createChatCompletion(createCompletionRequest()); } public void complete(Consumer<ChatCompletionChunk> onNext, Consumer<Throwable> onError, Action onComplete) { OPEN_AI_SERVICE.streamChatCompletion(createCompletionRequest()) .subscribe( chunk -> onNext.accept(chunk), error -> onError.accept(error), onComplete ); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1301, 1329), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((1659, 2603), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1659, 2582), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1659, 2558), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1659, 2409), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1659, 2227), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1659, 2068), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1659, 2036), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1659, 1875), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1738, 1861), 'com.google.common.base.Optional.fromNullable'), ((1802, 1860), 'io.github.lynbean.lynbot.Bot.getConfig'), ((1916, 2022), 'com.google.common.base.Optional.fromNullable'), ((1973, 2021), 'io.github.lynbean.lynbot.Bot.getConfig'), ((2105, 2213), 'com.google.common.base.Optional.fromNullable'), ((2158, 2212), 'io.github.lynbean.lynbot.Bot.getConfig'), ((2274, 2395), 'com.google.common.base.Optional.fromNullable'), ((2337, 2394), 'io.github.lynbean.lynbot.Bot.getConfig'), ((2445, 2544), 'com.google.common.base.Optional.fromNullable'), ((2497, 2543), 'io.github.lynbean.lynbot.Bot.getConfig')]
package org.ncgr.chatbot; import java.util.Collections; import java.util.List; import com.theokanning.openai.embedding.Embedding; import com.theokanning.openai.embedding.EmbeddingRequest; import com.theokanning.openai.embedding.EmbeddingResult; import com.theokanning.openai.service.OpenAiService; /** * Class to test retrieval of embeddings from OpenAI and upserting them to Pinecone. */ public class EmbeddingUpsertTest { // the OpenAI embedding model to use static String EMBED_MODEL = "text-embedding-ada-002"; public static void main(String[] args) { if (args.length<2) { System.err.println("Usage: EbeddingUpsertTest <index> <text>"); System.exit(1); } String index = args[0]; String text = args[1]; String openaiApiKey = System.getenv().get("OPENAI_API_KEY"); String pineconeProjectName = System.getenv().get("PINECONE_PROJECT_NAME"); String pineconeApiKey = System.getenv().get("PINECONE_API_KEY"); String pineconeEnvironment = System.getenv().get("PINECONE_ENVIRONMENT"); String pineconeIndexName = System.getenv().get("PINECONE_INDEX_NAME"); OpenAiService service = new OpenAiService(openaiApiKey); EmbeddingRequest embeddingRequest = EmbeddingRequest.builder() .model(EMBED_MODEL) .input(Collections.singletonList(text)) .build(); List<Embedding> embeddings = service.createEmbeddings(embeddingRequest).getData(); for (Embedding embedding : embeddings) { List<Double> vector = embedding.getEmbedding(); System.out.println("object: " + embedding.getObject()); System.out.println("index: " + embedding.getIndex()); System.out.println("vector: " + vector); } } }
[ "com.theokanning.openai.embedding.EmbeddingRequest.builder" ]
[((1295, 1426), 'com.theokanning.openai.embedding.EmbeddingRequest.builder'), ((1295, 1405), 'com.theokanning.openai.embedding.EmbeddingRequest.builder'), ((1295, 1353), 'com.theokanning.openai.embedding.EmbeddingRequest.builder')]
package com.topopixel.library.langchain.java.llms.openai; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.completion.CompletionResult; import com.topopixel.library.langchain.java.callbacks.manager.CallbackManagerForLLMRun; import com.topopixel.library.langchain.java.llms.base.BaseLLM; import com.topopixel.library.langchain.java.llms.openai.sdk.OpenAiService; import com.topopixel.library.langchain.java.schema.Generation; import com.topopixel.library.langchain.java.schema.LLMResult; import com.topopixel.library.langchain.java.utils.LangChainUtils; import java.lang.reflect.Field; import java.util.*; import java.util.stream.Collectors; import lombok.*; @Getter @Setter public abstract class BaseOpenAI extends BaseLLM { public OpenAiService service; public String modelName = "text-davinci-003"; public String openaiApiKey = null; public String openaiApiBase = null; public String openaiOrganization = null; // configs private Float temperature = 0.7f; private Integer maxTokens = 256; private Float topP = 1f; private Float frequencyPenalty = 0f; private Float presencePenalty = 0f; private Integer n = 1; private Integer bestOf = 1; protected BaseOpenAI() {} protected BaseOpenAI(OpenAIConfig config) { // TODO: make exception when use gpt3.5 or gpt4 validataEnvironment(config); } private void validataEnvironment(OpenAIConfig config) { Map<String, Object> configMap = config.toMap(); if (openaiApiKey == null) { openaiApiKey = LangChainUtils.getFromDictOrEnv(configMap, "openai_api_key", "OPENAI_API_KEY", Optional.empty()); } if (openaiApiBase == null) { openaiApiBase = LangChainUtils.getFromDictOrEnv(configMap, "openai_api_base", "OPENAI_API_BASE", Optional.empty()); } if (openaiApiBase != null) { service = new OpenAiService(openaiApiKey, openaiApiBase); } else { service = new OpenAiService(openaiApiKey); } injectConfig(config); } private void injectConfig(OpenAIConfig config) { try { Field[] fields = OpenAIConfig.class.getDeclaredFields(); Field[] selfFields = BaseOpenAI.class.getDeclaredFields(); List<String> configNames = Arrays.stream(fields).map(Field::getName).collect(Collectors.toList()); List<String> selfNames = Arrays.stream(selfFields).map(Field::getName).collect(Collectors.toList()); for (int i = 0; i < configNames.size(); i++) { String name = configNames.get(i); if (!selfNames.contains(name)) { continue; } Field configField = fields[i]; Field selfField = BaseOpenAI.class.getDeclaredField(name); selfField.setAccessible(true); configField.setAccessible(true); if (configField.get(config) == null) { continue; } selfField.set(this, configField.get(config)); } } catch (Exception e) { return; } } // TODO: callback manager @Override protected LLMResult internalGenerate(List<String> prompts, List<String> stop, CallbackManagerForLLMRun runManager) { // for now only support 1 string elem // TODO: support streaming // TODO: support sub prompts CompletionRequest request = CompletionRequest.builder() .model(modelName) .temperature(temperature.doubleValue()) .maxTokens(maxTokens) .topP(topP.doubleValue()) .frequencyPenalty(frequencyPenalty.doubleValue()) .presencePenalty(presencePenalty.doubleValue()) .n(n) .bestOf(bestOf) .prompt(prompts.get(0)) .build(); if (stop != null) { request.setStop(stop); } CompletionResult response = service.createCompletion(request); return createLLMResult(prompts, response); } private LLMResult createLLMResult(List<String> prompts, CompletionResult response) { // for now only support 1 string elem List<Generation> generation = response.getChoices().stream() .map(choice -> Generation.builder() .text(choice.getText()) .generationInfo(new HashMap<String, Object>() {{ put("finish_reason", choice.getFinish_reason()); put("logprobs", choice.getLogprobs()); }}).build() ).collect(Collectors.toList()); Map<String, Object> llmOuput = new HashMap<String, Object>() {{ put("token_usage", response.getUsage()); put("model_name", modelName); }}; return LLMResult.builder() .generations(Arrays.asList(generation)) .llmOutput(llmOuput) .build(); } @Override protected String llmType() { return "openai"; } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((3541, 3947), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3541, 3926), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3541, 3890), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3541, 3862), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3541, 3844), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3541, 3784), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3541, 3722), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3541, 3684), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3541, 3650), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3541, 3598), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4384, 4685), 'com.topopixel.library.langchain.java.schema.Generation.builder'), ((4384, 4677), 'com.topopixel.library.langchain.java.schema.Generation.builder'), ((4384, 4448), 'com.topopixel.library.langchain.java.schema.Generation.builder'), ((4924, 5049), 'com.topopixel.library.langchain.java.schema.LLMResult.builder'), ((4924, 5028), 'com.topopixel.library.langchain.java.schema.LLMResult.builder'), ((4924, 4995), 'com.topopixel.library.langchain.java.schema.LLMResult.builder')]
package com.yksc.dummy.controller; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Optional; import org.apache.commons.lang3.StringUtils; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.theokanning.openai.completion.chat.ChatCompletionChoice; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessageRole; import com.theokanning.openai.service.OpenAiService; import com.yksc.dummy.data.Data; import com.yksc.model.db.ChatMessage; import com.yksc.model.db.ChatRoom; import com.yksc.model.db.User; import com.yksc.model.rest.ChatRequest; import com.yksc.model.util.IdGeneraterUtil; @RestController @RequestMapping("/ChatCompletions") public class ChatCompletionsController { @PostMapping public ResponseEntity<Object> generateText(@RequestHeader("user-mail-address") String userMailAddress, @RequestBody ChatRequest chatRequest) { List<ChatMessage> chatMessageList = chatRequest.getChatMessageList(); String selectedModel = chatRequest.getSelectedAiModel(); ChatMessage userChatMessage = chatMessageList.get(chatMessageList.size()-1); userChatMessage.setMessageId(IdGeneraterUtil.nextGuid()); boolean first = false; //new Chat ChatRoom chatRoom = null; if( StringUtils.isBlank(chatRequest.getRoomId()) ) { Optional<User> optionalUser = Data.usersMap.values().stream() .filter( user -> user.getEmail().equals( userMailAddress ) ) .findFirst(); if( optionalUser.isPresent() ) { first = true; chatRoom = new ChatRoom(); String guid = IdGeneraterUtil.nextGuid(); chatRoom.setRoomId( guid ); chatRoom.setOwnerUserId( optionalUser.get().getUserId() ); chatRoom.setRoomTitle( "include server" ); chatRoom.setCreateDate( new Date() ); chatRoom.setUpdateDate( new Date() ); chatRoom.setAiModel( selectedModel ); chatRoom.setAiModelSource( "Open Ai" ); chatRoom.setSumTotal( 0 ); Data.chatRoomList.add( chatRoom ); } }else { String roomId = chatRequest.getRoomId(); Optional<ChatRoom> optional = Data.chatRoomList.stream().filter(temp -> StringUtils.equals(roomId, temp.getRoomId())).findFirst(); chatRoom = optional.get(); } System.out.println("selectedModel:" + selectedModel); System.out.println("chatMessageList.size:" + chatMessageList.size()); boolean isRealChatGpt = true; if( isRealChatGpt ) { if( first ) { chatRoom.setRoomTitle( thumbnailText( userChatMessage.getMessage() ) ); } long start = Calendar.getInstance().getTimeInMillis(); String result = realChatGpt(chatMessageList, selectedModel); ChatMessage chatMessage = new ChatMessage( IdGeneraterUtil.nextGuid(), "ai", result, new Date() ); long end = Calendar.getInstance().getTimeInMillis(); chatMessage.setResponseTime(start - end); System.out.println("new user message id:" + userChatMessage.getMessageId()); Data.chatMessgaeMap.put(userChatMessage.getMessageId(), userChatMessage); chatRoom.getChatMessageIds().add(userChatMessage.getMessageId()); Data.chatMessgaeMap.put(chatMessage.getMessageId(), chatMessage); chatRoom.getChatMessageIds().add(chatMessage.getMessageId()); chatMessage.setRoomTitle(chatRoom.getRoomTitle()); chatMessage.setRoomId(chatRoom.getRoomId()); return ResponseEntity.ok( chatMessage ); }else { long start = Calendar.getInstance().getTimeInMillis(); SimpleDateFormat simple = new SimpleDateFormat( "yyyy/MM/dd hh:mm:ss.SSS" ); String date = simple.format( Calendar.getInstance().getTime() ); String result = "This is a pretend generated message. " + date + " (><;)"; String guid = IdGeneraterUtil.nextGuid(); Date sendDate = Calendar.getInstance().getTime(); ChatMessage chatMessage = new ChatMessage( guid, "ai", result, sendDate ); try { //Pretending to work Thread.sleep( 3000L ); } catch (InterruptedException e) { e.printStackTrace(); } long end = Calendar.getInstance().getTimeInMillis(); chatMessage.setResponseTime(start - end); Data.chatMessgaeMap.put(userChatMessage.getMessageId(), userChatMessage); chatRoom.getChatMessageIds().add(userChatMessage.getMessageId()); Data.chatMessgaeMap.put(chatMessage.getMessageId(), chatMessage); chatRoom.getChatMessageIds().add(chatMessage.getMessageId()); chatMessage.setRoomTitle(chatRoom.getRoomTitle()); chatMessage.setRoomId(chatRoom.getRoomId()); return ResponseEntity.ok( chatMessage ); } } private String thumbnailText( String aiMessage ) { aiMessage = "Please summarize in 20 characters or less. No explanation needed. Base your summary on the language of the message." + "\r\n" + aiMessage; String apiKey = System.getenv( "OPEN_AI_API_KEY" ); List<com.theokanning.openai.completion.chat.ChatMessage> messages = new ArrayList<>(); messages.add( new com.theokanning.openai.completion.chat.ChatMessage(ChatMessageRole.USER.value(), aiMessage)); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder().model("gpt-3.5-turbo-0125").maxTokens(10) .messages(messages).build(); StringBuilder contentBuilder = new StringBuilder(); OpenAiService service = new OpenAiService(apiKey); service.streamChatCompletion(chatCompletionRequest).doOnError(Throwable::printStackTrace) .blockingForEach(chatCompletion -> { for (ChatCompletionChoice choice : chatCompletion.getChoices()) { com.theokanning.openai.completion.chat.ChatMessage message = choice.getMessage(); if (message != null && message.getContent() != null) { contentBuilder.append(message.getContent()); } } }); return contentBuilder.toString(); } private String realChatGpt(List<ChatMessage> chatMessageList, String selectedModel ) { String apiKey = System.getenv( "OPEN_AI_API_KEY" ); OpenAiService service = new OpenAiService(apiKey); List<com.theokanning.openai.completion.chat.ChatMessage> messages = new ArrayList<>(); for (ChatMessage chatMessage : chatMessageList) { String message = chatMessage.getMessage(); String sender = chatMessage.getSender(); if( StringUtils.equals(sender, ChatMessage.SENDER_AI) ) { messages.add( new com.theokanning.openai.completion.chat.ChatMessage(ChatMessageRole.ASSISTANT.value(), message)); }else if( StringUtils.equals(sender, ChatMessage.SENDER_USER) ) { messages.add( new com.theokanning.openai.completion.chat.ChatMessage(ChatMessageRole.USER.value(), message)); } } System.out.println("selectedModel:" + selectedModel); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder().model(selectedModel) .messages(messages).build(); // .maxTokens(16 * 1000) StringBuilder contentBuilder = new StringBuilder(); service.streamChatCompletion(chatCompletionRequest).doOnError(Throwable::printStackTrace) .blockingForEach(chatCompletion -> { for (ChatCompletionChoice choice : chatCompletion.getChoices()) { com.theokanning.openai.completion.chat.ChatMessage message = choice.getMessage(); if (message != null && message.getContent() != null) { contentBuilder.append(message.getContent()); } } }); return contentBuilder.toString(); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1872, 1995), 'com.yksc.dummy.data.Data.usersMap.values'), ((1872, 1973), 'com.yksc.dummy.data.Data.usersMap.values'), ((1872, 1903), 'com.yksc.dummy.data.Data.usersMap.values'), ((1872, 1894), 'com.yksc.dummy.data.Data.usersMap.values'), ((2574, 2607), 'com.yksc.dummy.data.Data.chatRoomList.add'), ((2726, 2825), 'com.yksc.dummy.data.Data.chatRoomList.stream'), ((2726, 2813), 'com.yksc.dummy.data.Data.chatRoomList.stream'), ((2726, 2752), 'com.yksc.dummy.data.Data.chatRoomList.stream'), ((3260, 3300), 'java.util.Calendar.getInstance'), ((3503, 3543), 'java.util.Calendar.getInstance'), ((3716, 3788), 'com.yksc.dummy.data.Data.chatMessgaeMap.put'), ((3878, 3942), 'com.yksc.dummy.data.Data.chatMessgaeMap.put'), ((4240, 4280), 'java.util.Calendar.getInstance'), ((4404, 4436), 'java.util.Calendar.getInstance'), ((4602, 4634), 'java.util.Calendar.getInstance'), ((4894, 4934), 'java.util.Calendar.getInstance'), ((5009, 5081), 'com.yksc.dummy.data.Data.chatMessgaeMap.put'), ((5180, 5244), 'com.yksc.dummy.data.Data.chatMessgaeMap.put'), ((5972, 6000), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((6064, 6170), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6064, 6162), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6064, 6137), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6064, 6123), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((7354, 7387), 'com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value'), ((7551, 7579), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((7715, 7800), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((7715, 7792), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((7715, 7767), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package com.staringpig.framework.openai.model; import com.staringpig.framework.openai.openai.OpenAI; import com.staringpig.framework.openai.session.SessionManager; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.completion.CompletionResult; import com.theokanning.openai.moderation.Moderation; import net.dreamlu.mica.core.utils.$; import net.dreamlu.mica.core.utils.StringUtil; import java.math.BigDecimal; import java.util.List; import java.util.function.Consumer; public abstract class CompletionModel extends OpenAIModel { protected final String suffix; /** * Echo back the prompt in addition to the completion */ protected final Boolean echo; /** * Generates best_of completions server-side and returns the "best" * (the one with the lowest log probability per token). * Results cannot be streamed. * <p> * When used with {@link CompletionRequest#n}, best_of controls the number of candidate completions and n specifies how many to return, * best_of must be greater than n. */ protected final Integer bestOf; /** * Include the log probabilities on the logprobs most likely tokens, as well the chosen tokens. * For example, if logprobs is 10, the API will return a list of the 10 most likely tokens. * The API will always return the logprob of the sampled token, * so there may be up to logprobs+1 elements in the response. */ protected final Integer logprobs; protected CompletionModel(String id, OpenAI openAI, OpenAI.Metadata metadata, Integer maxTokens, BigDecimal pricePerThousandTokensOfPrompt, BigDecimal pricePerThousandTokensOfCompletion, String suffix, Boolean echo, Integer bestOf, Integer logprobs, SessionManager sessionManager) { super(id, openAI, metadata, maxTokens, pricePerThousandTokensOfPrompt, pricePerThousandTokensOfCompletion, sessionManager); this.suffix = suffix; this.echo = echo; this.bestOf = bestOf; this.logprobs = logprobs; } @Override public Answer ask(String user, String question, Integer limitTokens) { List<Moderation> moderation = super.moderation(question); if ($.isNotEmpty(moderation)) { return new Answer(moderation); } return buildAnswer(super.openAI.createCompletion(buildRequest(user, question, limitTokens))); } @Override public void ask(String user, String question, Integer limitTokens, Consumer<Answer> onAnswer) { List<Moderation> moderation = super.moderation(question); if ($.isNotEmpty(moderation)) { onAnswer.accept(new Answer(moderation)); } super.openAI.createCompletion(buildRequest(user, question, limitTokens), completionResult -> onAnswer.accept(buildAnswer(completionResult))); } private Answer buildAnswer(CompletionResult completion) { StringBuilder answer = new StringBuilder(); for (int i = 0; i < super.metadata.getN(); i++) { answer.append(StringUtil.trimWhitespace(completion.getChoices().get(i).getText())); } return new Answer(completion.getUsage().getTotalTokens(), super.cost(completion.getUsage().getPromptTokens(), completion.getUsage().getCompletionTokens()), answer.toString()); } private CompletionRequest buildRequest(String user, String question, Integer limitTokens) { return CompletionRequest.builder() .model(this.getId()) .prompt(question) .temperature(super.metadata.getTemperature()) .maxTokens(Math.min(this.maxTokens, limitTokens) - OpenAIModel.tokens(question) - 10) .user(user) .n(super.metadata.getN()) .stop(this.metadata.getStop()) .bestOf(this.bestOf) .frequencyPenalty(this.metadata.getFrequencyPenalty()) .echo(this.echo) .logitBias(this.metadata.getLogitBias()) .logprobs(this.logprobs) .presencePenalty(this.metadata.getPresencePenalty()) .stream(this.metadata.getStream()) .suffix(this.suffix) .build(); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((3573, 4373), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 4348), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 4311), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 4260), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 4191), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 4150), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 4093), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 4060), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 3989), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 3952), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 3905), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 3863), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 3835), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 3733), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 3671), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3573, 3637), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package estudo.java.springboot.screenmatch.service; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.service.OpenAiService; public class ConsultaChatGPT { public static String obterTraducao(String texto) { OpenAiService service = new OpenAiService(System.getenv("OPENAI_API_KEY")); CompletionRequest requisicao = CompletionRequest.builder() .model("text-davinci-003") .prompt("traduza para o português o texto: " + texto) .maxTokens(1000) .temperature(0.7) .build(); var resposta = service.createCompletion(requisicao); return resposta.getChoices().get(0).getText(); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((377, 610), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((377, 585), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((377, 551), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((377, 518), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((377, 447), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package com.bjoggis.openai.service; import com.bjoggis.openai.entity.AiConfiguration; import com.bjoggis.openai.entity.Message; import com.bjoggis.openai.entity.ThreadChannel; import com.bjoggis.openai.function.ActiveAiConfigurationFunction; import com.bjoggis.openai.function.SaveMessageFunction; import com.bjoggis.openai.function.SaveMessageFunction.SaveMessageOptions; import com.bjoggis.openai.properties.OpenaiProperties; import com.bjoggis.openai.repository.AiConfigurationRepository; import com.bjoggis.openai.repository.MessageRepository; import com.bjoggis.openai.repository.ThreadChannelRepository; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatCompletionResult; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.time.Duration; import java.util.ArrayList; import java.util.Base64; import java.util.List; import java.util.Set; import java.util.UUID; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @Service public class ChatServiceImpl implements ChatService { private final Logger logger = LoggerFactory.getLogger(ChatServiceImpl.class); private final OpenaiProperties properties; private final ActiveAiConfigurationFunction configurationFunction; private final AiConfigurationRepository aiConfigurationRepository; private final MessageRepository messageRepository; private final SaveMessageFunction saveMessageFunction; private final ThreadChannelRepository threadChannelRepository; public ChatServiceImpl(OpenaiProperties properties, ActiveAiConfigurationFunction configurationFunction, AiConfigurationRepository aiConfigurationRepository, MessageRepository messageRepository, SaveMessageFunction saveMessageFunction, ThreadChannelRepository threadChannelRepository) { this.properties = properties; this.configurationFunction = configurationFunction; this.aiConfigurationRepository = aiConfigurationRepository; this.messageRepository = messageRepository; this.saveMessageFunction = saveMessageFunction; this.threadChannelRepository = threadChannelRepository; } @Override public String chat(String messageId, String message, String threadId, String userId) { final ThreadChannel threadChannel = threadChannelRepository.findByThreadId(threadId); AiConfiguration configuration = configurationFunction.get(); OpenAiService service = new OpenAiService(properties.token(), Duration.ofMinutes(1)); // Count the number of tokens in the message long messageTokens = StringUtils.countOccurrencesOf(message, " ") + 1; if (messageTokens > configuration.getRequestMaxTokens()) { logger.warn("Message too long, aborting"); return "Message too long, please try again with less than 100 words."; } saveMessageFunction.accept(new SaveMessageOptions(messageId, message, false, threadChannel)); Set<Message> messages = messageRepository.findByThreadChannel_ThreadIdOrderByCreatedAsc( threadChannel.getThreadId()); if (messages.size() > configuration.getMaxMessages()) { logger.warn("Too many messages, aborting"); return "Too many messages, please start a new thread."; } logger.info("Using system message: " + configuration.getSystemMessage()); List<ChatMessage> chatMessages = new ArrayList<>(); chatMessages.add(new ChatMessage("system", configuration.getSystemMessage())); List<ChatMessage> oldMessages = new java.util.ArrayList<>(messages.stream().map(message2 -> { switch (message2.getSender()) { case USER -> { return new ChatMessage("user", message2.getMessageAsString()); } case SYSTEM -> { return new ChatMessage("system", message2.getMessageAsString()); } case ASSISTANT -> { return new ChatMessage("assistant", message2.getMessageAsString()); } } return null; }).toList()); chatMessages.addAll(oldMessages); chatMessages.forEach(chatMessage -> logger.info(chatMessage.getRole() + ": " + chatMessage .getContent())); ChatCompletionRequest request = ChatCompletionRequest.builder() .model(configuration.getModel()) .messages(chatMessages) .n(configuration.getNumberOfMessages()) .maxTokens(configuration.getResponseMaxTokens()) .temperature(configuration.getTemperature()) .user(hashUserId(userId)) .build(); ChatCompletionResult response = service.createChatCompletion(request); logger.info("Message tokens: " + response.getUsage().getPromptTokens()); long completionTokens = response.getUsage().getCompletionTokens(); logger.info("Completion tokens used: " + completionTokens); String content = response.getChoices().get(0).getMessage().getContent(); saveMessageFunction.accept( new SaveMessageOptions(UUID.randomUUID().toString(), content, true, threadChannel)); logger.info("Response: " + content); return content; } private String hashUserId(String userId) { String hashedUserID = "unknown"; try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hashedId = digest.digest((userId).getBytes(StandardCharsets.UTF_8)); hashedUserID = Base64.getEncoder().encodeToString(hashedId); } catch (Exception ex) { ex.printStackTrace(); } return hashedUserID; } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((4381, 4694), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4381, 4677), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4381, 4643), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4381, 4590), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4381, 4533), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4381, 4485), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4381, 4453), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((5128, 5156), 'java.util.UUID.randomUUID'), ((5518, 5562), 'java.util.Base64.getEncoder')]
package com.cyster.sherpa.impl.advisor; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import com.cyster.sherpa.service.conversation.Conversation; import com.cyster.sherpa.service.conversation.ConversationException; import com.cyster.sherpa.service.conversation.Message; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; public class ChatAdvisorConversation implements Conversation { private final String model = "gpt-3.5-turbo"; private OpenAiService openAiService; private List<Message> messages; ChatAdvisorConversation(OpenAiService openAiService, List<Message> messages) { this.openAiService = openAiService; this.messages = messages; } @Override public Conversation addMessage(String message) { this.messages.add(new Message(message)); return this; } @Override public Message respond() throws ConversationException { var chatMessages = new ArrayList<ChatMessage>(); for (var message : this.messages) { if (message.getType() == Message.Type.SYSTEM) { chatMessages.add(new ChatMessage("system", message.getContent())); } else if (message.getType() == Message.Type.AI) { chatMessages.add(new ChatMessage("assistant", message.getContent())); } else if (message.getType() == Message.Type.USER) { chatMessages.add(new ChatMessage("user", message.getContent())); } } var chatCompletionRequest = ChatCompletionRequest.builder() .model(model) .messages(chatMessages) .build(); var result = this.openAiService.createChatCompletion(chatCompletionRequest); var choices = result.getChoices(); if (choices.size() == 0) { messages.add(new Message(Message.Type.INFO, "No responses")); throw new ConversationException("No Reponses"); } if (choices.size() > 1) { messages.add(new Message(Message.Type.INFO, "Multiple responses (ignored)")); throw new ConversationException("Multiple Reponses"); } var message = new Message(Message.Type.AI, choices.get(0).getMessage().getContent()); messages.add(message); return message; } @Override public List<Message> getMessages() { return messages.stream() .filter(message -> message.getType() == Message.Type.AI || message.getType() == Message.Type.USER) .collect(Collectors.toList()); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1685, 1799), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1685, 1778), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1685, 1742), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package com.ramesh.openai; import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; 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; /*** * This project demonstrates the Self Consistency prompting technique which is useful when there is need * to get consistent responses from chat gpt, for e.g. solving a puzzle correctly ***/ class SelfConsistencyPrompting { public static void main(String... args) { // Set the Open AI Token & Model String token = "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"; String model = "gpt-3.5-turbo"; // service handle for calling OpenAI APIs OpenAiService service = new OpenAiService(token, Duration.ofSeconds(30)); // self consistent messages are part of the prompt which tell chat gpt how to think and respond thus // giving consistent messages System.out.println("\n-----------------------------------------------------------"); String[] prompts = new String[15]; prompts[0]="Q: There are 15 trees in the grove. Grove workers will plant trees in the grove today. After they are done,"; prompts[1]="there will be 21 trees. How many trees did the grove workers plant today?"; prompts[2]="A: We start with 15 trees. Later we have 21 trees. The difference must be the number of trees they planted."; prompts[3]="So, they must have planted 21 - 15 = 6 trees. The answer is 6."; prompts[4]="Q: If there are 3 cars in the parking lot and 2 more cars arrive, how many cars are in the parking lot?"; prompts[5]="A: There are 3 cars in the parking lot already. 2 more arrive. Now there are 3 + 2 = 5 cars. The answer is 5."; prompts[6]="Q: Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total?"; prompts[7]="A: Leah had 32 chocolates and Leah’s sister had 42. That means there were originally 32 + 42 = 74"; prompts[8]="chocolates. 35 have been eaten. So in total they still have 74 - 35 = 39 chocolates. The answer is 39."; prompts[9]="Q: Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops"; prompts[10]="did Jason give to Denny?"; prompts[11]="A: Jason had 20 lollipops. Since he only has 12 now, he must have given the rest to Denny. The number of"; prompts[12]="lollipops he has given to Denny must have been 20 - 12 = 8 lollipops. The answer is 8."; prompts[13]="Q: When I was 6 my sister was half my age. Now I’m 70 how old is my sister?"; prompts[14]="A:"; final List<ChatMessage> messages = new ArrayList<>(); for (int i = 0; i < 15; i++) { System.out.println(prompts[i]); final ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), prompts[i]); messages.add(userMessage); } // create the chat gpt chat completion request ChatCompletionRequest chatCompletionRequest2 = ChatCompletionRequest.builder() .model(model) .messages(messages) .n(1) .temperature(.1) .maxTokens(100) .logitBias(new HashMap<>()) .build(); System.out.println("------------"); System.out.print("ChatGPT response="); // send the request to chat gpt and print the response service.createChatCompletion(chatCompletionRequest2).getChoices().forEach((c) -> { System.out.println(c.getMessage().getContent()); }); service.shutdownExecutor(); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((2904, 2932), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((3082, 3335), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3082, 3310), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3082, 3266), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3082, 3234), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3082, 3201), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3082, 3179), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3082, 3143), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package br.com.webapp.screenmatch.services; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.service.OpenAiService; public class ConsultaChatGPT { public static String obterTraducao(String texto) { OpenAiService service = new OpenAiService(System.getenv("OPENAI_APIKEY")); CompletionRequest requisicao = CompletionRequest.builder() .model("gpt-3.5-turbo-instruct") .prompt("traduza para o português o texto: " + texto) .maxTokens(1000) .temperature(0.7) .build(); var resposta = service.createCompletion(requisicao); return resposta.getChoices().get(0).getText(); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((368, 607), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((368, 582), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((368, 548), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((368, 515), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((368, 444), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package example; import com.theokanning.openai.service.OpenAiService; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.image.CreateImageRequest; import com.theokanning.openai.image.CreateImageVariationRequest; import com.theokanning.openai.image.CreateImageEditRequest; import java.io.File; class OpenAiApiExample { public static void main(String... args) { String token = System.getenv("OPENAI_TOKEN"); OpenAiService service = new OpenAiService(token); /* System.out.println("\nCreating completion..."); CompletionRequest completionRequest = CompletionRequest.builder() // .model("ada") // .model("gpt-3.5-turbo") // .model("code-davinci-002") .model("gpt-3.5") .prompt("code processing.org java sketch to compute number pi") .echo(true) .user("testing") .n(1) .temperature(0.0) .maxTokens(1024) .topP(1.0) .build(); service.createCompletion(completionRequest).getChoices().forEach(System.out::println); */ System.out.println("\nCreating Image..."); // CreateImageVariationRequest request = CreateImageVariationRequest.builder() // //.prompt("Two tabby cats breakdancing") // .n(2) // .build(); int num = 1; CreateImageEditRequest editRequest = CreateImageEditRequest.builder() //.prompt("change cat and dog color to tabby red and do swing dance with holding paws") .prompt("add frame") .n(num) .size("1024x1024") .responseFormat("url") .build(); CreateImageVariationRequest varRequest = CreateImageVariationRequest.builder() //.prompt("add bookmarker to photo") // no prompt used with variation requests .n(num) .size("1024x1024") .responseFormat("url") .build(); String imagePath; imagePath = File.separator+"images"+File.separator+"readers_RGBA_l.png"; imagePath = File.separator+"images"+File.separator+"VAL_0340_1024_L_cr.png"; imagePath = File.separator+"images"+File.separator+"mask_VAL_0340_1024_L_cr.png"; String maskPath = null; maskPath = File.separator+"images"+File.separator+"readers_mask_RGBA_r.png"; maskPath = File.separator+"images"+File.separator+"almost_empty_mask_RGBA.png"; //maskPath = File.separator+"images"+File.separator+"readers_mask_RGBA_l.png"; maskPath = File.separator+"images"+File.separator+"empty_mask_RGBA.png"; maskPath = File.separator+"images"+File.separator+"almost_empty_mask_RGBA.png"; maskPath = File.separator+"images"+File.separator+"mask_VAL_0340_1024_L_cr.png"; maskPath = null; //imagePath = File.separator+"images"+File.separator+"dog_breakdancing_with_cat_RGBA.png"; //String imagePath = File.separator+"images"+File.separator+"readers_l.png"; File imageFile = new File(imagePath); System.out.println(imageFile); System.out.println(File.separator); System.out.println(imageFile.getAbsolutePath()); try { System.out.println(imageFile.getCanonicalPath()); } catch(Exception ioe) { System.out.println(ioe); } System.out.println("\nImage is located at:"); for (int i=0; i<num; i++) { System.out.println(service.createImageEdit(editRequest, imagePath, maskPath).getData().get(0).getUrl()); //System.out.println("num "+ (i+1) + " of " + num +" " + service.createImageVariation(varRequest, imagePath).getData().get(i).getUrl()); System.out.println(); } // https://stackoverflow.com/questions/57100451/okhttp3-requestbody-createcontenttype-content-deprecated } }
[ "com.theokanning.openai.image.CreateImageVariationRequest.builder", "com.theokanning.openai.image.CreateImageEditRequest.builder" ]
[((1431, 1691), 'com.theokanning.openai.image.CreateImageEditRequest.builder'), ((1431, 1666), 'com.theokanning.openai.image.CreateImageEditRequest.builder'), ((1431, 1639), 'com.theokanning.openai.image.CreateImageEditRequest.builder'), ((1431, 1616), 'com.theokanning.openai.image.CreateImageEditRequest.builder'), ((1431, 1604), 'com.theokanning.openai.image.CreateImageEditRequest.builder'), ((1743, 1963), 'com.theokanning.openai.image.CreateImageVariationRequest.builder'), ((1743, 1938), 'com.theokanning.openai.image.CreateImageVariationRequest.builder'), ((1743, 1911), 'com.theokanning.openai.image.CreateImageVariationRequest.builder'), ((1743, 1888), 'com.theokanning.openai.image.CreateImageVariationRequest.builder')]
package com.example.autoreplybot; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsManager; import android.telephony.SmsMessage; import android.util.Log; import android.widget.Toast; import com.theokanning.openai.OpenAiService; import com.theokanning.openai.completion.CompletionChoice; import com.theokanning.openai.completion.CompletionRequest; import java.util.List; public class SmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); if (extras != null) { Object[] pdus = (Object[]) extras.get("pdus"); String smsrec = ""; String smssender = ""; for (Object pdu : pdus) { SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu); String messageBody = smsMessage.getMessageBody(); String messageSender = smsMessage.getOriginatingAddress(); Toast.makeText(context, "SMS:" + messageBody, Toast.LENGTH_LONG).show(); smsrec = messageBody; smssender = messageSender; } Log.d("NETWORK_LOAD","Initializing ChatGPT..."); OpenAiService service = new OpenAiService(Globals.API_KEY); CompletionRequest completionRequest = CompletionRequest.builder() .prompt("reply kindly and sweet to this:" + smsrec) .model("text-davinci-003") .maxTokens(160) .echo(false) .build(); final String phonenum = smssender; /** * TODO change this into a more efficient way of managing background threads * * This is just for sample PoC. Hehe */ new Thread(new Runnable() { @Override public void run() { List<CompletionChoice> choices = service .createCompletion(completionRequest).getChoices(); String chatgptresponse = choices.get(0).getText().trim(); Log.d("REPLY",chatgptresponse.trim()); SmsManager sm = SmsManager.getDefault(); if(chatgptresponse.length() > 160){ //Send a reply sm.sendTextMessage(phonenum,null,chatgptresponse.substring(0,160),null,null); } else{ sm.sendTextMessage(phonenum,null,chatgptresponse.trim(),null,null); } } }).start(); } } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((1089, 1184), 'android.widget.Toast.makeText'), ((1465, 1709), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1465, 1680), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1465, 1647), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1465, 1611), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1465, 1564), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package com.raidrin.sakanu.services; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.theokanning.openai.completion.chat.ChatCompletionChoice; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatCompletionResult; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; import java.util.Locale; @Service @RequiredArgsConstructor public class OpenAiTermQuery { private final OpenAiService openAiService; public TermResponse query(String domain, String term) { String systemContent = TaskMessageGenerator.generateTaskMessage(domain); if(term == null || term.trim().isEmpty()) { throw new IllegalArgumentException("Term cannot be null or empty"); } String sanitizedTerm = term.trim().toLowerCase(Locale.ENGLISH); if(sanitizedTerm.length() >= 255) { throw new IllegalArgumentException("Term cannot be longer than 255 characters"); } return getResponseFromOpenAI(systemContent, sanitizedTerm); } private TermResponse getResponseFromOpenAI(String systemContent, String term) { ChatMessage systemMessage = new ChatMessage(); systemMessage.setRole("system"); systemMessage.setContent(systemContent); ChatMessage userMessage = new ChatMessage(); userMessage.setRole("user"); userMessage.setContent(term.trim().toLowerCase(Locale.ENGLISH)); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() .model("gpt-4") .messages( List.of( systemMessage, userMessage ) ) .temperature(1.2) .build(); ChatCompletionResult chatCompletionResult = openAiService.createChatCompletion(chatCompletionRequest); List<ChatCompletionChoice> chatCompletionChoices = chatCompletionResult.getChoices(); if (chatCompletionChoices.size() != 1) { throw new NoChoiceFoundException("Expected 1 choice, got " + chatCompletionChoices.size()); } ChatCompletionChoice chatCompletionChoice = chatCompletionChoices.get(0); String gptResponse = chatCompletionChoice.getMessage().getContent(); // Testing purposes // try { // Files.writeString( // Path.of("gpt_responses", domain + "-" + term + ".json"), // gptResponse // ); // } catch (IOException e) { // throw new RuntimeException(e); // } try { return getTermResponse(gptResponse); } catch (JsonProcessingException e) { throw new RuntimeException("Failed to process gpt response", e); } } public TermResponse getTermResponse(String gptResponse) throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.readValue( gptResponse, TermResponse.class ); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1756, 2145), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1756, 2112), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1756, 2070), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1756, 1827), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package com.chat.base.handler; import com.chat.base.bean.common.BaseCodeEnum; import com.chat.base.bean.constants.CommonConstant; import com.chat.base.bean.entity.GptModelConfig; import com.chat.base.bean.gpt.ChatReq; import com.chat.base.bean.vo.CacheGptApiTokenVo; import com.chat.base.bean.vo.CacheUserInfoVo; import com.chat.base.bean.vo.ChatMessageResultVo; import com.chat.base.handler.billing.ModelBillingFactory; import com.chat.base.handler.billing.ModelBillingService; import com.chat.base.handler.billing.impl.ModelBillingByBalanceImpl; import com.chat.base.handler.gpt.OpenAiProxyServiceFactory; import com.chat.base.handler.model.ChatModelProcessor; import com.chat.base.handler.model.ChatModelProcessorFactory; import com.chat.base.service.ChatBaseOpenAiProxyService; import com.chat.base.utils.ChatMessageCacheUtil; import com.chat.base.utils.ResultVO; import com.chat.base.utils.TokenUtil; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import io.github.asleepyfish.enums.RoleEnum; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.nio.charset.Charset; import java.util.LinkedList; import java.util.Optional; import java.util.UUID; /** * @author huyd * @date 2023/5/23 8:06 PM */ @Slf4j @Component public class AIChatManger { @Autowired private WeightAlgorithmManager weightAlgorithmManager; @Autowired private UserManager userManager; @Autowired private ModelBillingByBalanceImpl modelBillingByBalance; @Autowired private PromptRecordManager promptRecordManager; /** * 用户没有登录 只记录 不计费 * @param chatReq * @param response * @throws Exception */ public void streamChatWithWebV3NoStatus(ChatReq chatReq, HttpServletResponse response) throws Exception { response.setContentType("text/event-stream"); response.setCharacterEncoding("UTF-8"); response.setHeader("Cache-Control", "no-cache"); String model = chatReq.getModel(); if (StringUtils.isNoneEmpty(model) && !"gpt-3.5-turbo".equals(model)){ response.getOutputStream().write(BaseCodeEnum.NO_MODEL.getMsg().getBytes()); response.getOutputStream().close(); return; } Integer contentNumber = chatReq.getContentNumber(); String user = chatReq.getConversationId(); LinkedList<ChatMessage> userChatMessages = ChatMessageCacheUtil.getUserChatMessages(user, contentNumber); userChatMessages.add(new ChatMessage(RoleEnum.USER.getRoleName(), chatReq.getPrompt())); ChatMessageCacheUtil.getOkUserChatMessages(userChatMessages, model); if(userChatMessages.size()<=0){ response.getOutputStream().write(BaseCodeEnum.TOKEN_OVER.getMsg().getBytes()); response.getOutputStream().close(); return; } userChatMessages.addFirst(new ChatMessage(RoleEnum.SYSTEM.getRoleName(), chatReq.getSystemMessage())); ChatBaseOpenAiProxyService proxyService = OpenAiProxyServiceFactory.createProxyService("1"); if (proxyService == null) { response.getOutputStream().write(BaseCodeEnum.NO_MODEL.getMsg().getBytes()); response.getOutputStream().close(); return; } ChatMessageResultVo streamChatCompletion = proxyService.createStreamChatCompletion(ChatCompletionRequest.builder() .model(model) .messages(userChatMessages) .user(user) .temperature(chatReq.getTemperature()) .topP(chatReq.getTop_p()) .stream(true) .build(), response.getOutputStream(), CommonConstant.SYS_TOKEN); if(streamChatCompletion!=null){ ChatMessageCacheUtil.saveChatMessage(user,streamChatCompletion.getChatMessage()); promptRecordManager.asyncAddPromptRecord(streamChatCompletion); // 用户的回话id 也是 gpt的api的用户身份 } } /** * 应用系统 聊天 * @param chatReq * @param cacheUserInfoVo * @param response * @throws IOException */ public void chatStream(ChatReq chatReq, CacheUserInfoVo cacheUserInfoVo, HttpServletResponse response) throws IOException { String model = chatReq.getModel(); ModelBillingService modelBillingService = ModelBillingFactory.getModelBillingService(model); if(modelBillingService==null){ response.getOutputStream().write(BaseCodeEnum.MODEL_NO_OPEN.getMsg().getBytes()); return; } //交易id String tradeId = UUID.randomUUID().toString(); Integer contentNumber = chatReq.getContentNumber(); String user = chatReq.getConversationId(); LinkedList<ChatMessage> userChatMessages = ChatMessageCacheUtil.getUserChatMessages(user, contentNumber); userChatMessages.add(new ChatMessage(RoleEnum.USER.getRoleName(), chatReq.getPrompt())); ChatMessageCacheUtil.getOkUserChatMessages(userChatMessages, model); if(userChatMessages.size()<=0){ response.getOutputStream().write(BaseCodeEnum.TOKEN_OVER.getMsg().getBytes()); response.getOutputStream().close(); return; } userChatMessages.addFirst(new ChatMessage(RoleEnum.SYSTEM.getRoleName(), chatReq.getSystemMessage())); String token = cacheUserInfoVo.getGptApiTokenVo().getToken(); response.setContentType("text/event-stream"); response.setCharacterEncoding("UTF-8"); response.setHeader("Cache-Control", "no-cache"); Optional<GptModelConfig> modelConfig = weightAlgorithmManager.round(cacheUserInfoVo, model); if (!modelConfig.isPresent()) { response.getOutputStream().write(BaseCodeEnum.NO_MODEL_ROLE.getMsg().getBytes()); return; } GptModelConfig gptModelConfig = modelConfig.get(); ChatBaseOpenAiProxyService proxyService = OpenAiProxyServiceFactory.createProxyService(gptModelConfig.getId().toString()); if (proxyService == null) { response.getOutputStream().write(BaseCodeEnum.NO_MODEL.getMsg().getBytes()); response.getOutputStream().close(); return; } try { int tokenMessages = TokenUtil.countTokenMessages(userChatMessages, model); ResultVO<String> beforeBillingResult = modelBillingService.beforeBilling(cacheUserInfoVo.getGptApiTokenVo(), tokenMessages,tradeId); if(!beforeBillingResult.isOk()){ response.getOutputStream().write(beforeBillingResult.getMsg().getBytes()); return; } ChatMessageResultVo streamChatCompletion = proxyService.createStreamChatCompletion(ChatCompletionRequest.builder() .model(model) .messages(userChatMessages) .user(user) .temperature(chatReq.getTemperature()) .topP(chatReq.getTop_p()) .stream(true) .build(), response.getOutputStream(), token); //更新用户余额 boolean billingResult = userManager.costUserBalanceByChat(cacheUserInfoVo, streamChatCompletion, tradeId); if(!billingResult){ Long advanceChargeAmount = modelBillingByBalance.getUserAdvanceChargeMap(cacheUserInfoVo.getId()).getOrDefault(tradeId, 0L); log.info("扣款失败 返回预扣款给用户 cacheUserInfoVo={}", cacheUserInfoVo); synchronized (cacheUserInfoVo.getClass()){ CacheGptApiTokenVo gptApiTokenVo = cacheUserInfoVo.getGptApiTokenVo(); gptApiTokenVo.setBalance(gptApiTokenVo.getBalance()+advanceChargeAmount); } } if(streamChatCompletion!=null){ ChatMessageCacheUtil.saveChatMessage(user,streamChatCompletion.getChatMessage()); promptRecordManager.asyncAddPromptRecord(streamChatCompletion); // 用户的回话id 也是 gpt的api的用户身份 } }catch (Exception e){ Long advanceChargeAmount = modelBillingByBalance.getUserAdvanceChargeMap(cacheUserInfoVo.getId()).getOrDefault(tradeId, 0L); log.error("回话实现错误,现在返回预扣款给用户 cacheUserInfoVo={}", cacheUserInfoVo,e); synchronized (cacheUserInfoVo.getClass()){ CacheGptApiTokenVo gptApiTokenVo = cacheUserInfoVo.getGptApiTokenVo(); gptApiTokenVo.setBalance(gptApiTokenVo.getBalance()+advanceChargeAmount); } } finally { modelBillingByBalance.removeUserTradeId(cacheUserInfoVo.getId(),tradeId); } } /** * 官方的接口 * * @param chatReq * @param cacheUserInfoVo * @throws IOException */ public void chatStream(ChatCompletionRequest chatReq, CacheUserInfoVo cacheUserInfoVo, HttpServletResponse response) throws IOException { String model = chatReq.getModel(); ModelBillingService modelBillingService = ModelBillingFactory.getModelBillingService(model); if(modelBillingService==null){ return; } //交易id String tradeId = UUID.randomUUID().toString(); String token = cacheUserInfoVo.getGptApiTokenVo().getToken(); Optional<GptModelConfig> modelConfig = weightAlgorithmManager.round(cacheUserInfoVo, model); if (!modelConfig.isPresent()) { response.getOutputStream().write((String.format("data: %s\n\n", BaseCodeEnum.NO_MODEL_ROLE.getMsg())).getBytes(Charset.defaultCharset())); return; } GptModelConfig gptModelConfig = modelConfig.get(); try { ResultVO<String> beforeBillingResult = modelBillingService.beforeBilling(cacheUserInfoVo.getGptApiTokenVo(), TokenUtil.countTokenMessages(chatReq.getMessages(), model),tradeId); if(!beforeBillingResult.isOk()){ response.getOutputStream().write((String.format("data: %s\n\n", beforeBillingResult.getMsg())).getBytes(Charset.defaultCharset())); return; } ChatModelProcessor chatModelProcessor = ChatModelProcessorFactory.getChatModelProcessor(model); assert chatModelProcessor != null; ChatMessageResultVo streamChatCompletion = chatModelProcessor.chatStream(chatReq, response.getOutputStream(), token,gptModelConfig.getId().toString()); //更新用户余额 boolean billingResult = userManager.costUserBalanceByChat(cacheUserInfoVo, streamChatCompletion, tradeId); if(!billingResult){ Long advanceChargeAmount = modelBillingByBalance.getUserAdvanceChargeMap(cacheUserInfoVo.getId()).getOrDefault(tradeId, 0L); log.info("扣款失败 返回预扣款给用户 cacheUserInfoVo={}", cacheUserInfoVo); synchronized (cacheUserInfoVo.getClass()){ CacheGptApiTokenVo gptApiTokenVo = cacheUserInfoVo.getGptApiTokenVo(); gptApiTokenVo.setBalance(gptApiTokenVo.getBalance()+advanceChargeAmount); } } promptRecordManager.asyncAddPromptRecord(streamChatCompletion); // 用户的回话id 也是 gpt的api的用户身份 }catch (Exception e){ Long advanceChargeAmount = modelBillingByBalance.getUserAdvanceChargeMap(cacheUserInfoVo.getId()).getOrDefault(tradeId, 0L); log.error("回话实现错误,现在返回预扣款给用户 cacheUserInfoVo={}", cacheUserInfoVo,e); synchronized (cacheUserInfoVo.getClass()){ CacheGptApiTokenVo gptApiTokenVo = cacheUserInfoVo.getGptApiTokenVo(); gptApiTokenVo.setBalance(gptApiTokenVo.getBalance()+advanceChargeAmount); } } finally { modelBillingByBalance.removeUserTradeId(cacheUserInfoVo.getId(),tradeId); } } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((2403, 2444), 'com.chat.base.bean.common.BaseCodeEnum.NO_MODEL.getMsg'), ((2403, 2433), 'com.chat.base.bean.common.BaseCodeEnum.NO_MODEL.getMsg'), ((2797, 2824), 'io.github.asleepyfish.enums.RoleEnum.USER.getRoleName'), ((3012, 3055), 'com.chat.base.bean.common.BaseCodeEnum.TOKEN_OVER.getMsg'), ((3012, 3044), 'com.chat.base.bean.common.BaseCodeEnum.TOKEN_OVER.getMsg'), ((3186, 3215), 'io.github.asleepyfish.enums.RoleEnum.SYSTEM.getRoleName'), ((3430, 3471), 'com.chat.base.bean.common.BaseCodeEnum.NO_MODEL.getMsg'), ((3430, 3460), 'com.chat.base.bean.common.BaseCodeEnum.NO_MODEL.getMsg'), ((3644, 3929), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3644, 3904), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3644, 3874), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3644, 3832), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3644, 3777), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3644, 3749), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3644, 3705), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4770, 4816), 'com.chat.base.bean.common.BaseCodeEnum.MODEL_NO_OPEN.getMsg'), ((4770, 4805), 'com.chat.base.bean.common.BaseCodeEnum.MODEL_NO_OPEN.getMsg'), ((4893, 4921), 'java.util.UUID.randomUUID'), ((5194, 5221), 'io.github.asleepyfish.enums.RoleEnum.USER.getRoleName'), ((5408, 5451), 'com.chat.base.bean.common.BaseCodeEnum.TOKEN_OVER.getMsg'), ((5408, 5440), 'com.chat.base.bean.common.BaseCodeEnum.TOKEN_OVER.getMsg'), ((5582, 5611), 'io.github.asleepyfish.enums.RoleEnum.SYSTEM.getRoleName'), ((6060, 6106), 'com.chat.base.bean.common.BaseCodeEnum.NO_MODEL_ROLE.getMsg'), ((6060, 6095), 'com.chat.base.bean.common.BaseCodeEnum.NO_MODEL_ROLE.getMsg'), ((6410, 6451), 'com.chat.base.bean.common.BaseCodeEnum.NO_MODEL.getMsg'), ((6410, 6440), 'com.chat.base.bean.common.BaseCodeEnum.NO_MODEL.getMsg'), ((7048, 7361), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((7048, 7332), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((7048, 7298), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((7048, 7252), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((7048, 7193), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((7048, 7161), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((7048, 7113), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((9571, 9599), 'java.util.UUID.randomUUID'), ((9890, 9925), 'com.chat.base.bean.common.BaseCodeEnum.NO_MODEL_ROLE.getMsg')]
package com.happy.chat.service.impl; import static com.happy.chat.constants.Constant.CHAT_FROM_USER; import static com.happy.chat.constants.Constant.PERF_CHAT_MODULE; import static com.happy.chat.constants.Constant.PERF_ERROR_MODULE; import static com.happy.chat.uitls.CacheKeyProvider.chatSystemTipsKey; import static com.happy.chat.uitls.CacheKeyProvider.chatUnPayTipsKey; import static com.happy.chat.uitls.CacheKeyProvider.chatWarnWordKey; import static com.happy.chat.uitls.CacheKeyProvider.gptApiTokenKey; import static com.happy.chat.uitls.CacheKeyProvider.happyModelHttpUrl; import static com.happy.chat.uitls.CacheKeyProvider.userChatgptWarnKey; import static com.happy.chat.uitls.CacheKeyProvider.userChatgptWarnMaxCountKey; import static com.happy.chat.uitls.CacheKeyProvider.userEnterChatgptAdvanceModelThresholdKey; import static com.happy.chat.uitls.CacheKeyProvider.userEnterHappyModelLatestTimeKey; import static com.happy.chat.uitls.CacheKeyProvider.userExitHappyModelExpireMillsKey; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import com.happy.chat.dao.FlirtopiaChatDao; import com.happy.chat.domain.FlirtopiaChat; import com.happy.chat.domain.IceBreakWord; import com.happy.chat.model.ChatRequest; import com.happy.chat.model.ChatResponse; import com.happy.chat.model.HappyModelRequest; import com.happy.chat.service.ChatService; import com.happy.chat.service.OpenAIService; import com.happy.chat.service.PaymentService; import com.happy.chat.uitls.FileUtils; import com.happy.chat.uitls.ObjectMapperUtils; import com.happy.chat.uitls.OkHttpUtils; import com.happy.chat.uitls.PrometheusUtils; import com.happy.chat.uitls.RedisUtil; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessageRole; import io.prometheus.client.Counter; import lombok.extern.slf4j.Slf4j; import okhttp3.Response; @Lazy @Service @Slf4j public class ChatServiceImpl implements ChatService { private final String defaultHappyModelExitExpireTime = "300000"; private final String defaultEnterAdvanceModelHistoryChatSize = "10"; private final String defaultToPayTips = "Don’t be shy to undress me, only $9.9"; private final String defaultSystemTips = "Don't scare a girl. Do it gently and softly."; private final String defaultUserChatWarnMaxCount = "3"; private final String normalVersionGpt = "normal"; private final String advancedVersionGpt = "advanced"; @Autowired private FlirtopiaChatDao flirtopiaChatDao; @Autowired private RedisUtil redisUtil; @Autowired private PaymentService paymentService; @Autowired private OpenAIService openAIService; @Autowired private OkHttpUtils okHttpUtils; @Autowired private PrometheusUtils prometheusUtil; @Autowired private Counter chatPrometheusCounter; @Override public List<IceBreakWord> getIceBreakWordsByRobot(String robotId) { return flirtopiaChatDao.getRobotIceBreakWords(robotId); } @Override public List<FlirtopiaChat> getUserHistoryChats(String userId) { return flirtopiaChatDao.getUserChatList(userId); } @Override public List<FlirtopiaChat> getUserRobotHistoryChats(String userId, String robotId) { return flirtopiaChatDao.getUserRobotChats(userId, robotId); } @Override public int insert(FlirtopiaChat flirtopiaChat) { return flirtopiaChatDao.insertChat(flirtopiaChat); } @Override public int batchInsert(List<FlirtopiaChat> flirtopiaChats) { return flirtopiaChatDao.batchInsertChat(flirtopiaChats); } /** * 1、用户输入内容有敏感词,直接返回默认回复(取缓存数据,若没有取到则返回I have no idea) * <p> * 2、用户输入内容无敏感词 * 拿出用户和AI的历史聊天记录,按时间前后顺序排序,用作请求gpt和快乐模型的参数输入。 * <p> * 前置说明,快乐模型退场机制检测:用户进入快乐模型,一段时间未聊天后,再次正常聊天,不再使用快乐模型,避免回复崩塌。 * 逻辑上就是 check 进没进过快乐模型 或者 "取缓存里记录的:调用的快乐模型的上一次时间" 和 "当前时间"做比较,未超过 "缓存配置的阈值,比如5分钟" * <p> * 2-1)已付费用户,首先进入快乐模型退场机制检测。若退场,则降级调用gpt热情版,若没有退场,则调用快乐模型继续回复。 * 对回复内容做检测:空或者包含敏感词返回默认回复,见上 * <p> * 同时,当不是默认回复且内容来自快乐模型,要更新下这次调用的快乐模型的当前时间,用于退场机制检测。 * <p> * 2-2)未付费用户,也要做下快乐模型的退场检测(未进入认为是退场,进入过且超过时间认为是退场)。 * 2-2-1)若已处在快乐模型状态下即未退场(虽然内容是高斯模糊),那么就直接调用快乐模型回复,对回复内容做检测,空或者包含敏感词返回默认回复,见上 * 否则返回内容的同时要加一条付费提示 * <p> * 2-2-2)若不在快乐模型状态下即退场,那么去缓存里拿对应配置的历史聊天轮次数阈值(没取到使用硬编码写死5轮) * 比较历史聊天轮次是否超过配置的阈值,若超过则需要调用热情版gpt,否则需要调用普通版gpt * 调用gpt前需要组装一些参数,包括但不限于:robot的prompt信息(人设)、警报关键词、组装当前聊天和历史聊天等 * 同样也要对回复内容做检测,空或者包含敏感词返回默认回复,见上。否则去看下警报关键词: * <p> * 出现了警报关键词的情况,逻辑如下: * 2-2-2-1)普通版:去缓存里拿出配置的警报次数阈值(没取到使用硬编码写死3次),比较用户和该ai的触发警报次数是否超过配置的阈值 * 小于等于阈值,则返回客户端回复内容外还要加一条系统提示(也是从缓存里取) * 大于阈值,调用快乐模型。对快乐模型的回复内容同样做检测,空或者敏感词,返回默认回复,否则返回客户端回复内容外还要告诉客户端该内容付费要加上高斯模糊 * 2-2-2-2)热情版:调用快乐模型,对快乐模型的回复内容同样做检测,空或者敏感词,返回默认回复,否则返回客户端回复内容外还要告诉客户端该内容付费要加上高斯模糊 * * @return */ @Override public ChatResponse requestChat(ChatRequest chatRequest) { String userId = chatRequest.getUserId(); String robotId = chatRequest.getRobotId(); String content = chatRequest.getUserWord(); String robotDefaultResp = chatRequest.getRobotDefaultResp(); List<String> sensitiveWords = chatRequest.getSensitiveWords(); boolean hasSensitiveWord = hasSensitiveWord(content, sensitiveWords); // 敏感词 直接返回 if (hasSensitiveWord) { log.warn("user request content contains sensitive {} {} {}", userId, robotId, content); prometheusUtil.perf(PERF_CHAT_MODULE, "用户聊天请求内容有敏感词,使用AI默认文案回复"); return ChatResponse.builder() .content(robotDefaultResp) .useDefault(true) .reasonAndModel("userContentHasSensitiveWord-Default") .build(); } // 按时间升序排序 List<FlirtopiaChat> userHistoryMessages = getUserRobotHistoryChats(userId, robotId).stream() .sorted(Comparator.comparing(FlirtopiaChat::getCreateTime)).collect(Collectors.toList()); // 是否付费 若付费,直接调用快乐模型,调用前检查是否超过快乐模型过期时间,若已经过期,则降级调用热情版gpt boolean hasPay = paymentService.userHasPayedRobot(userId, robotId); if (hasPay) { return getRobotAlreadyPaidResp(userId, robotId, content, userHistoryMessages, robotDefaultResp, sensitiveWords); } // 未付费请求 return getRobotUnPaidResp(userId, robotId, content, userHistoryMessages, robotDefaultResp, sensitiveWords); } /** * 已付费 热情版gpt回复 * * @param userId * @param robotId * @param content * @param userHistoryMessages * @return */ private ChatResponse getRobotAlreadyPaidRespFromAdvancedGpt(String userId, String robotId, String content, List<FlirtopiaChat> userHistoryMessages, String robotDefaultResp, List<String> sensitiveWords) { String respContent = requestChatgpt(robotId, advancedVersionGpt, content, userHistoryMessages); // 没拿到,直接return if (StringUtils.isEmpty(respContent)) { return buildChatResponse(userId, robotId, respContent, "alreadyPaidAdvancedGptEmpty:Default", robotDefaultResp, sensitiveWords); } //内容是否警报 boolean contentHasWarn = chatgptRespHasWarn(respContent); //出现警报,直接请求快乐模型 if (contentHasWarn) { String happyResp = requestHappyModel(robotId, content, userHistoryMessages); ChatResponse chatResponse = buildChatResponse(userId, robotId, happyResp, "alreadyPaidAdvancedGptWarn:HappyModel", robotDefaultResp, sensitiveWords); if (!chatResponse.isUseDefault()) { updateHappyModelLatestTime(userId, robotId); } return chatResponse; } return buildChatResponse(userId, robotId, respContent, "alreadyPaidAdvancedGptNoWarn:AdvancedGpt", robotDefaultResp, sensitiveWords); } /** * 未付费的ai回复 * * @param userId * @param robotId * @param content * @param userHistoryMessages * @return */ private ChatResponse getRobotUnPaidResp(String userId, String robotId, String content, List<FlirtopiaChat> userHistoryMessages, String robotDefaultResp, List<String> sensitiveWords) { // 做下快乐模型退场检测 boolean timeForExit = checkTimeForExitHappyModel(userId, robotId); // 没退场 使用快乐模型回复 if (!timeForExit) { return getRobotUnPaidRespFromHappyModel(userId, robotId, content, userHistoryMessages, robotDefaultResp, sensitiveWords); } // 即从没进去过快乐模型,退场,轮次够,使用热情版gpt if (isEnterChatgptAdvancedModel(userHistoryMessages)) { return getRobotUnPaidRespFromAdvancedGpt(userId, robotId, content, userHistoryMessages, robotDefaultResp, sensitiveWords); } // 退场,轮次不够,使用热情版gpt return getRobotUnPaidRespFromNormalGpt(userId, robotId, content, userHistoryMessages, robotDefaultResp, sensitiveWords); } /** * 未付费 快乐模型回复 * * @param userId * @param robotId * @param content * @param userHistoryMessages * @return */ private ChatResponse getRobotUnPaidRespFromHappyModel(String userId, String robotId, String content, List<FlirtopiaChat> userHistoryMessages, String robotDefaultResp, List<String> sensitiveWords) { String aiRespContent = requestHappyModel(robotId, content, userHistoryMessages); ChatResponse chatResponse = buildChatResponse(userId, robotId, aiRespContent, "unPaidTimeNotExit:HappyModel", robotDefaultResp, sensitiveWords); // 来自快乐模型的回复 要更新下时间 if (!chatResponse.isUseDefault()) { updateHappyModelLatestTime(userId, robotId); chatResponse.setPayTips(getUnPayTips()); } return chatResponse; } /** * 未付费 热情版gpt回复 * * @param userId * @param robotId * @param content * @param userHistoryMessages * @return */ private ChatResponse getRobotUnPaidRespFromAdvancedGpt(String userId, String robotId, String content, List<FlirtopiaChat> userHistoryMessages, String robotDefaultResp, List<String> sensitiveWords) { String respContent = requestChatgpt(robotId, advancedVersionGpt, content, userHistoryMessages); // 没拿到,直接return if (StringUtils.isEmpty(respContent)) { return buildChatResponse(userId, robotId, respContent, "unPaidAdvancedGptEmpty:Default", robotDefaultResp, sensitiveWords); } //内容是否警报 boolean contentHasWarn = chatgptRespHasWarn(respContent); //出现警报,直接请求快乐模型 if (contentHasWarn) { String happyResp = requestHappyModel(robotId, content, userHistoryMessages); ChatResponse chatResponse = buildChatResponse(userId, robotId, happyResp, "unPaidAdvancedGptWarn:HappyModel", robotDefaultResp, sensitiveWords); if (!chatResponse.isUseDefault()) { updateHappyModelLatestTime(userId, robotId); // 快乐模型返回的话 要有付费卡 chatResponse.setPayTips(getUnPayTips()); } return chatResponse; } return buildChatResponse(userId, robotId, respContent, "unPaidAdvancedGptNoWarn:AdvancedGpt", robotDefaultResp, sensitiveWords); } /** * 未付费 普通版gpt回复 * * @param userId * @param robotId * @param content * @param userHistoryMessages * @return */ private ChatResponse getRobotUnPaidRespFromNormalGpt(String userId, String robotId, String content, List<FlirtopiaChat> userHistoryMessages, String robotDefaultResp, List<String> sensitiveWords) { // 退场,使用普通版gpt String respContent = requestChatgpt(robotId, normalVersionGpt, content, userHistoryMessages); // 没拿到,直接return if (StringUtils.isEmpty(respContent)) { return buildChatResponse(userId, robotId, respContent, "unPaidNormalGptEmpty:Default", robotDefaultResp, sensitiveWords); } //内容是否警报 boolean contentHasWarn = chatgptRespHasWarn(respContent); // 超过3次,请求快乐模型 if (overGptWarnCount(userId, robotId)) { String happyResp = requestHappyModel(robotId, content, userHistoryMessages); ChatResponse chatResponse = buildChatResponse(userId, robotId, happyResp, "unPaidNormalGptWarn:HappyModel", robotDefaultResp, sensitiveWords); if (!chatResponse.isUseDefault()) { updateHappyModelLatestTime(userId, robotId); // 快乐模型返回的话 要有付费卡 chatResponse.setPayTips(getUnPayTips()); } return chatResponse; } else if (contentHasWarn) { // 没超过三次,但也有警报 // warn次数+1 addGptWarnCount(userId, robotId); ChatResponse chatResponse = buildChatResponse(userId, robotId, respContent, "unPaidNormalGptWarnNotEnough-NormalGpt", robotDefaultResp, sensitiveWords); if (!chatResponse.isUseDefault()) { // 返回加上规劝文案 chatResponse.setSystemTips(redisUtil.getOrDefault(chatSystemTipsKey(), defaultSystemTips)); } return chatResponse; } return buildChatResponse(userId, robotId, respContent, "unPaidNormalGptNoWarn:NormalGpt", robotDefaultResp, sensitiveWords); } /** * 付费后的ai回复 * * @param userId * @param robotId * @param userReqContent * @param userHistoryMessages * @return */ private ChatResponse getRobotAlreadyPaidResp(String userId, String robotId, String userReqContent, List<FlirtopiaChat> userHistoryMessages, String robotDefaultResp, List<String> sensitiveWords) { // 快乐模型是否退场机制 boolean timeForExit = checkTimeForExitHappyModel(userId, robotId); if (timeForExit) { //退场,回退到请求热情版,同样需要看下警报词 return getRobotAlreadyPaidRespFromAdvancedGpt(userId, robotId, userReqContent, userHistoryMessages, robotDefaultResp, sensitiveWords); } return getRobotAlreadyPaidRespFromHappyModel(userId, robotId, userReqContent, userHistoryMessages, robotDefaultResp, sensitiveWords); } /** * 已付费 快乐模型回复 * * @param userId * @param robotId * @param userReqContent * @param userHistoryMessages * @param robotDefaultResp * @param sensitiveWords * @return */ private ChatResponse getRobotAlreadyPaidRespFromHappyModel(String userId, String robotId, String userReqContent, List<FlirtopiaChat> userHistoryMessages, String robotDefaultResp, List<String> sensitiveWords) { // 未退场 String responseContent = requestHappyModel(robotId, userReqContent, userHistoryMessages); ChatResponse chatResponse = buildChatResponse(userId, robotId, responseContent, "alreadyPaid:HappyModel", robotDefaultResp, sensitiveWords); // 来自快乐模型的回复 要更新下时间 if (!chatResponse.isUseDefault()) { updateHappyModelLatestTime(userId, robotId); } return chatResponse; } private ChatResponse buildChatResponse(String userId, String robotId, String content, String reasonAndModel, String robotDefaultResp, List<String> sensitiveWords) { // 为空 if (StringUtils.isEmpty(content)) { log.error("buildChatResponse empty {} {} {}", userId, robotId, content); prometheusUtil.perf(PERF_ERROR_MODULE, "ai聊天回复内容为空,见: " + reasonAndModel); return ChatResponse.builder() .content(robotDefaultResp) .useDefault(true) .reasonAndModel(reasonAndModel + ":empty") .build(); } // 敏感词 if (hasSensitiveWord(content, sensitiveWords)) { log.error("buildChatResponse hasSensitiveWord {} {} {}", userId, robotId, content); prometheusUtil.perf(PERF_CHAT_MODULE, "ai聊天回复内容包含敏感词,见:" + reasonAndModel); return ChatResponse.builder() .content(robotDefaultResp) .useDefault(true) .reasonAndModel(reasonAndModel + ":sensitive") .build(); } prometheusUtil.perf(PERF_CHAT_MODULE, "ai聊天回复内容成功,见:" + reasonAndModel); return ChatResponse.builder() .content(content) .reasonAndModel(reasonAndModel) .build(); } // 付费提示 private String getUnPayTips() { List<String> results = redisUtil.range(chatUnPayTipsKey(), 0, -1); if (CollectionUtils.isEmpty(results)) { log.error("robot unpay tips empty"); prometheusUtil.perf(PERF_CHAT_MODULE, "未能获取到待付费提示文案,使用默认文案"); return defaultToPayTips; } return results.get(RandomUtils.nextInt(0, results.size())); } /** * 是否进入chatgpt 热情版 * * @param userHistoryMessages * @return */ private boolean isEnterChatgptAdvancedModel(List<FlirtopiaChat> userHistoryMessages) { String val = redisUtil.getOrDefault(userEnterChatgptAdvanceModelThresholdKey(), defaultEnterAdvanceModelHistoryChatSize); return userHistoryMessages.size() >= Integer.parseInt(val); } private boolean chatgptRespHasWarn(String content) { if (StringUtils.isEmpty(content)) { return false; } List<String> warnList = redisUtil.range(chatWarnWordKey(), 0, -1); if (CollectionUtils.isEmpty(warnList)) { prometheusUtil.perf(PERF_CHAT_MODULE, "未能获取到AI回复警报词列表"); return false; } return warnList.stream() .anyMatch(warn -> content.toLowerCase().contains(warn.toLowerCase())); } private void addGptWarnCount(String userId, String robotId) { redisUtil.increment(userChatgptWarnKey(userId, robotId), 1L); } private boolean overGptWarnCount(String userId, String robotId) { String userChatWarnCount = redisUtil.get(userChatgptWarnKey(userId, robotId)); if (StringUtils.isEmpty(userChatWarnCount)) { return false; } String userChatWarnThreshold = redisUtil.getOrDefault(userChatgptWarnMaxCountKey(), defaultUserChatWarnMaxCount); return Integer.parseInt(userChatWarnCount) >= Integer.parseInt(userChatWarnThreshold); } private void updateHappyModelLatestTime(String userId, String robotId) { redisUtil.set(userEnterHappyModelLatestTimeKey(userId, robotId), System.currentTimeMillis() + ""); } // 快乐模型退场检查 private boolean checkTimeForExitHappyModel(String userId, String robotId) { String latestTimeStr = redisUtil.get(userEnterHappyModelLatestTimeKey(userId, robotId)); // 为空 认为是退场 说明历史上根本没有进入过快乐模型 if (StringUtils.isEmpty(latestTimeStr)) { return true; } String exitExpireTimeStr = redisUtil.getOrDefault(userExitHappyModelExpireMillsKey(), defaultHappyModelExitExpireTime); // 超过五分钟没聊 就退出 if (System.currentTimeMillis() - Long.parseLong(latestTimeStr) >= Long.parseLong(exitExpireTimeStr)) { return true; } return false; } private String requestHappyModel(String robotId, String currentUserInput, List<FlirtopiaChat> historyChats) { HappyModelRequest happyModelRequest = HappyModelRequest.builder() .temperature(0.1) .maxNewToken(100) .historyMaxLen(1000) .topP(0.85) .userId(robotId) .presencePenalty(0.5) .frequencyPenalty(0.5) .current(HappyModelRequest.Current.builder() .u(currentUserInput) .build()) .build(); List<HappyModelRequest.History> histories = new ArrayList<>(); // 转换成格式 historyChats.forEach(historyChat -> { if (CHAT_FROM_USER.equals(historyChat.getMessageFrom())) { histories.add(HappyModelRequest.History.builder() .u(historyChat.getContent()) .build()); } else { histories.add(HappyModelRequest.History.builder() .b(historyChat.getContent()) .build()); } }); happyModelRequest.setHistory(histories); try { String url = redisUtil.get(happyModelHttpUrl()); if (StringUtils.isEmpty(url)) { prometheusUtil.perf(PERF_ERROR_MODULE, "快乐模型URL未配置"); return null; } Response response = okHttpUtils.postJson(url, ObjectMapperUtils.toJSON(happyModelRequest)); String json; if (response != null && response.body() != null) { json = response.body().string(); log.info("json {}", json); Map<String, String> jsonMap = ObjectMapperUtils.fromJSON(json, Map.class, String.class, String.class); prometheusUtil.perf(PERF_CHAT_MODULE, "请求快乐模型返回结果成功"); return jsonMap.get("response"); } } catch (Exception e) { log.error("requestHappyModel exception", e); prometheusUtil.perf(PERF_ERROR_MODULE, "请求快乐模型返回异常"); } return null; } /** * 请求openAI * * @param robotId * @param version * @param currentUserInput * @param historyChats * @return */ private String requestChatgpt(String robotId, String version, String currentUserInput, List<FlirtopiaChat> historyChats) { // 从缓存里取出robot对应的prompt,分成热情版/普通版。即role=system String fileName = String.format("prompt/%s_%s.prompt", robotId, version); String prompt = FileUtils.getFileContent(fileName); if (StringUtils.isEmpty(prompt)) { log.error("robot {} has no prompt {} ", robotId, version); prometheusUtil.perf(PERF_ERROR_MODULE, "未能获取到AI Prompt配置"); return null; } List<String> apiKeys = redisUtil.range(gptApiTokenKey(), 0, -1); if (CollectionUtils.isEmpty(apiKeys)) { log.error("chat gpt apikey empty {}", robotId); prometheusUtil.perf(PERF_ERROR_MODULE, "未能获取到gptApiToken配置"); return null; } List<ChatMessage> messages = new ArrayList<>(); // role=system,系统设定,即prompt; role=user,用户输入;role=assistant,gpt输入 ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), prompt); messages.add(systemMessage); // 转换成格式 historyChats.forEach(historyChat -> { if (CHAT_FROM_USER.equals(historyChat.getMessageFrom())) { messages.add(new ChatMessage(ChatMessageRole.USER.value(), historyChat.getContent())); } else { messages.add(new ChatMessage(ChatMessageRole.ASSISTANT.value(), historyChat.getContent())); } }); messages.add(new ChatMessage(ChatMessageRole.USER.value(), currentUserInput)); log.info("request openai, robot {}, request {} ", robotId, ObjectMapperUtils.toJSON(messages)); ChatMessage response = openAIService.requestChatCompletion(apiKeys, messages); if (response == null) { log.error("chat open ai return empty {}", robotId); prometheusUtil.perf(PERF_ERROR_MODULE, "请求gpt返回结果为空"); return null; } prometheusUtil.perf(PERF_CHAT_MODULE, "请求gpt返回结果成功"); return response.getContent(); } private boolean hasSensitiveWord(String content, List<String> sensitiveWords) { if (StringUtils.isEmpty(content)) { return false; } return sensitiveWords.stream() .anyMatch(content::contains); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value" ]
[((7809, 8020), 'com.happy.chat.model.ChatResponse.builder'), ((7809, 7991), 'com.happy.chat.model.ChatResponse.builder'), ((7809, 7916), 'com.happy.chat.model.ChatResponse.builder'), ((7809, 7878), 'com.happy.chat.model.ChatResponse.builder'), ((19278, 19477), 'com.happy.chat.model.ChatResponse.builder'), ((19278, 19448), 'com.happy.chat.model.ChatResponse.builder'), ((19278, 19385), 'com.happy.chat.model.ChatResponse.builder'), ((19278, 19347), 'com.happy.chat.model.ChatResponse.builder'), ((19796, 19999), 'com.happy.chat.model.ChatResponse.builder'), ((19796, 19970), 'com.happy.chat.model.ChatResponse.builder'), ((19796, 19903), 'com.happy.chat.model.ChatResponse.builder'), ((19796, 19865), 'com.happy.chat.model.ChatResponse.builder'), ((20127, 20256), 'com.happy.chat.model.ChatResponse.builder'), ((20127, 20231), 'com.happy.chat.model.ChatResponse.builder'), ((20127, 20183), 'com.happy.chat.model.ChatResponse.builder'), ((23338, 23773), 'com.happy.chat.model.HappyModelRequest.builder'), ((23338, 23748), 'com.happy.chat.model.HappyModelRequest.builder'), ((23338, 23608), 'com.happy.chat.model.HappyModelRequest.builder'), ((23338, 23569), 'com.happy.chat.model.HappyModelRequest.builder'), ((23338, 23531), 'com.happy.chat.model.HappyModelRequest.builder'), ((23338, 23498), 'com.happy.chat.model.HappyModelRequest.builder'), ((23338, 23470), 'com.happy.chat.model.HappyModelRequest.builder'), ((23338, 23433), 'com.happy.chat.model.HappyModelRequest.builder'), ((23338, 23399), 'com.happy.chat.model.HappyModelRequest.builder'), ((23634, 23747), 'com.happy.chat.model.HappyModelRequest.Current.builder'), ((23634, 23714), 'com.happy.chat.model.HappyModelRequest.Current.builder'), ((23634, 23669), 'com.happy.chat.model.HappyModelRequest.Current.builder'), ((24022, 24143), 'com.happy.chat.model.HappyModelRequest.History.builder'), ((24022, 24110), 'com.happy.chat.model.HappyModelRequest.History.builder'), ((24022, 24057), 'com.happy.chat.model.HappyModelRequest.History.builder'), ((24197, 24318), 'com.happy.chat.model.HappyModelRequest.History.builder'), ((24197, 24285), 'com.happy.chat.model.HappyModelRequest.History.builder'), ((24197, 24232), 'com.happy.chat.model.HappyModelRequest.History.builder'), ((26687, 26717), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((26954, 26982), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((27078, 27111), 'com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value'), ((27204, 27232), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value')]
package com.vission.chatGPT.service; import com.theokanning.openai.image.CreateImageRequest; import com.theokanning.openai.image.Image; import com.theokanning.openai.image.ImageResult; import com.theokanning.openai.service.OpenAiService; import java.util.List; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @Service @Slf4j @RequiredArgsConstructor public class ChatGPTImageService { private final OpenAiService openAiService; public List<String> generations(String description) { CreateImageRequest request = CreateImageRequest.builder() .prompt(description) .size("1024x1024") .responseFormat("url") .user("vission") .n(1) .build(); ImageResult image = openAiService.createImage(request); return image.getData().stream().map(Image::getUrl).collect(Collectors.toList()); } }
[ "com.theokanning.openai.image.CreateImageRequest.builder" ]
[((640, 859), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((640, 834), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((640, 812), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((640, 779), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((640, 740), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((640, 705), 'com.theokanning.openai.image.CreateImageRequest.builder')]
package com.example.fyp.controller; import java.util.Vector; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.ResponseBody; // additional import org.springframework.web.bind.annotation.RestController; import com.example.fyp.model.DetailEmotion; import com.example.fyp.model.TextSentimentAnswer; import com.example.fyp.model.promptModel; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.service.OpenAiService; //Controller to handle text analyzer's GPT sentiment requests @RestController public class SentimentController { @Value("${apiKey}") private String apiKeyContent; private TextSentimentAnswer textAnalyzer(String sentence) throws RuntimeException{ String apiKey = apiKeyContent; // String apiKey = "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"; String currentModel = "text-davinci-003"; TextSentimentAnswer t = new TextSentimentAnswer(); // Set up OpenAI API OpenAiService service = new OpenAiService(apiKey); //OVERALL SENTIMENT String prompt = "Decide if this statement's sentiment is positive, neutral, or negative: " + sentence; CompletionRequest sentimentRequest = CompletionRequest.builder() .model(currentModel) .prompt(prompt) .echo(true) .maxTokens(60) .build(); String response = service.createCompletion(sentimentRequest).getChoices().get(0).getText(); String overallSentiment_raw = response.substring(prompt.length()).trim(); String overallSentiment; // Clean Output (.Negative --> Negative) if ( overallSentiment_raw.toLowerCase().indexOf("negative") != -1 ) { overallSentiment = "Negative"; } else if (overallSentiment_raw.toLowerCase().indexOf("positive") != -1){ overallSentiment = "Positive"; } else if (overallSentiment_raw.toLowerCase().indexOf("neutral") != -1){ overallSentiment = "Neutral"; } else{ overallSentiment = "Not found"; } System.out.println("overall sentiment: " + overallSentiment); t.setOverallSentiment(overallSentiment); prompt = "Analyze the given sentence: \"" + sentence + "\" and explain why it has a " + overallSentiment + " sentiment. Consider the specific aspects, keywords, or phrases that contribute to this sentiment and provide a comprehensive explanation."; CompletionRequest altExplanationRequest = CompletionRequest.builder() .model(currentModel) .prompt(prompt) .echo(true) .maxTokens(1000) .build(); response = service.createCompletion(altExplanationRequest).getChoices().get(0).getText(); String overallContent = response.substring(prompt.length()).trim(); System.out.println("overall content: " + overallContent); t.setOverallContent(overallContent); prompt = "Analyze the given input sentence " + sentence + " and detect the emotions associated with highlighted keywords or sentences, then give explanation and associate them with a unique color in hexcode that are not too dark." + " Please format the answer as [Emotion]|[highlighted keyword or sentence]|[explanation]|[hexcode]\n[IF MORE EMOTIONS]."; String[] output_array = {""}; CompletionRequest explanationRequest = CompletionRequest.builder() .model(currentModel) .prompt(prompt) .echo(true) .maxTokens(1000) .build(); response = service.createCompletion(explanationRequest).getChoices().get(0).getText(); String output = response.substring(prompt.length()).trim(); output_array = output.split("\n"); Vector<DetailEmotion> emotions = new Vector<DetailEmotion>(); System.out.println(output_array.length); for (int i = 0; i < output_array.length; i++) { DetailEmotion d = new DetailEmotion(); System.out.println("sentence: " + sentence.toLowerCase()); System.out.println("highlighted: " + output_array[i].split("\\|")[1].toLowerCase()); if (!sentence.toLowerCase().contains(output_array[i].split("\\|")[1].toLowerCase().replaceAll("\"", ""))) { d.setTitle(output_array[i].split("\\|")[0]); d.setHighlighted(output_array[i].split("\\|")[1]); d.setExplanation(output_array[i].split("\\|")[2]); d.setColor("#E6E9ED"); } else { d.setTitle(output_array[i].split("\\|")[0]); d.setHighlighted(output_array[i].split("\\|")[1]); d.setExplanation(output_array[i].split("\\|")[2]); d.setColor(output_array[i].split("\\|")[3]); } emotions.add(d); } t.setEmotions(emotions); return t; } @PostMapping(value = "/analyze", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public TextSentimentAnswer inputSentence(ModelMap model, @ModelAttribute promptModel prompt){ while(true) { try { TextSentimentAnswer response = textAnalyzer(prompt.getPrompt()); return response; } catch(RuntimeException e) { System.out.println("Runtime Exception occurred : "+ e.getMessage()); } } } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((1513, 1913), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1513, 1844), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1513, 1769), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1513, 1697), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1513, 1621), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3056, 3478), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3056, 3405), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3056, 3324), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3056, 3248), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3056, 3168), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4265, 4687), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4265, 4614), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4265, 4533), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4265, 4457), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4265, 4377), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package acute.ai; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Strings; import com.google.gson.*; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.theokanning.openai.OpenAiHttpException; import com.theokanning.openai.client.OpenAiApi; 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; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.TextComponent; import okhttp3.*; import okhttp3.Authenticator; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.NamespacedKey; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; import retrofit2.Retrofit; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.IOException; import java.lang.reflect.Type; import java.net.*; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.nio.file.Path; import java.nio.file.Paths; import java.text.DecimalFormat; import java.time.Duration; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import static com.theokanning.openai.service.OpenAiService.*; public final class CraftGPT extends JavaPlugin { public NamespacedKey magicWandKey = new NamespacedKey(this, "secret"); public NamespacedKey autoSpawnChunkFlagKey = new NamespacedKey(this, "chunk-flag"); public boolean debug = false; public boolean apiKeySet = false; public boolean apiConnected = false; public OpenAiService openAIService; public static final Random random = new Random(); public List<String> waitingOnAPIList = new ArrayList<>(); ConcurrentHashMap<UUID, Entity> chattingPlayers = new ConcurrentHashMap<>(); ArrayList<UUID> debuggingPlayers = new ArrayList<>(); ConcurrentHashMap<UUID, AIMob> selectingPlayers = new ConcurrentHashMap<>(); private final Gson gson = new GsonBuilder().registerTypeAdapter(Class.class, new ClassTypeAdapter()).setPrettyPrinting().create(); ConcurrentHashMap<String, AIMob> craftGPTData = new ConcurrentHashMap<>(); private File usageFile; private FileConfiguration usageFileConfig; public static final String CHAT_PREFIX = ChatColor.GOLD + "[" + ChatColor.GRAY + "Craft" + ChatColor.GREEN + "GPT" + ChatColor.GOLD + "] " + ChatColor.GRAY; public static final String DISCORD_URL = "https://discord.gg/BXhUUQEymg"; public static final String SPIGOT_URL = "https://www.spigotmc.org/resources/craftgpt.110635/"; public static final String UPDATE_AVAILABLE = "Update available! Download v%s "; public static final String UP_TO_DATE = "CraftGPT is up to date: (%s)"; public static final String UNRELEASED_VERSION = "Version (%s) is more recent than the one publicly available. Dev build?"; public static final String UPDATE_CHECK_FAILED = "Could not check for updates. Reason: "; public static final int spigotID = 110635; @Override public void onEnable() { getLogger().info("+----------------------------------------------------------------+"); getLogger().info("| CraftGPT Community |"); getLogger().info("+================================================================+"); //getLogger().info("| * Please report bugs at: https://git.io/JkJLD |"); getLogger().info("| * Join the Discord at: https://discord.gg/BXhUUQEymg |"); getLogger().info("| * Enjoying the plugin? Leave a review and share with a friend! |"); getLogger().info("+----------------------------------------------------------------+"); // Register events getServer().getPluginManager().registerEvents(new CraftGPTListener(this), this); // Register commands getCommand("craftgpt").setExecutor(new Commands(this)); getCommand("craftgpt").setTabCompleter(new Commands(this)); // Save/read config.yml Path path = Paths.get("plugins/CraftGPT/config.yml"); if (Files.exists(path)) { // Only save a backup if one already exists to prevent overwriting backup with defaults try { Files.copy(path, Paths.get("plugins/CraftGPT/config.bak"), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { getLogger().warning("Failed to create backup config!"); } } saveDefaultConfig(); getConfig().options().copyDefaults(true); debug = getConfig().getBoolean("debug"); if (debug) { getLogger().info(CHAT_PREFIX + "Debug mode enabled!"); } // Save/read usage.yml createUsageFile(false); // Load data.json craftGPTData = readData(this); if (craftGPTData != null) getLogger().info(String.format("Loaded %s AI-enabled mobs.", craftGPTData.size())); getLogger().info(String.format("Loaded %s events.", getConfig().getConfigurationSection("events").getKeys(false).size())); // Connect to bStats int bStatsId = 18710; Metrics metrics = new Metrics(this, bStatsId); enableOpenAI(); if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { new PlaceholderAPIExpansion(this).register(); getLogger().info("Registered with PlaceholderAPI"); } // Check for updates UpdateChecker.init(this, spigotID).requestUpdateCheck().whenComplete((result, exception) -> { if (result.requiresUpdate()) { this.getLogger().warning((String.format( UPDATE_AVAILABLE, result.getNewestVersion()) + "at " + SPIGOT_URL)); return; } UpdateChecker.UpdateReason reason = result.getReason(); if (reason == UpdateChecker.UpdateReason.UP_TO_DATE) { this.getLogger().info(String.format(UP_TO_DATE, result.getNewestVersion())); } else if (reason == UpdateChecker.UpdateReason.UNRELEASED_VERSION) { this.getLogger().info(String.format(UNRELEASED_VERSION, result.getNewestVersion())); } else { this.getLogger().warning(UPDATE_CHECK_FAILED + reason); } }); getLogger().info("Enabled"); } @Override public void onDisable() { getLogger().info("Disabling..."); if (!chattingPlayers.isEmpty()) { getLogger().info("Ending chats..."); Set<UUID> chattingUUIDs = new HashSet<>(chattingPlayers.keySet()); for (UUID uuid: chattingUUIDs) { CraftGPTListener craftGPTListener = new CraftGPTListener(this); craftGPTListener.exitChat(getServer().getPlayer(uuid)); } } getLogger().info("Writing save data..."); writeData(this); saveUsageFile(); getLogger().warning("Disabled"); } public FileConfiguration getUsageFile() { return this.usageFileConfig; } public void saveUsageFile() { try { getUsageFile().save(usageFile); } catch (IOException e) { throw new RuntimeException(e); } } public void saveUsageFileAsync() { // Can't run/schedule async tasks when disabled! if (!this.isEnabled()) { saveUsageFile(); return; } Bukkit.getScheduler().runTaskAsynchronously(this, new Runnable() { @Override public void run() { try { getUsageFile().save(usageFile); getUsageFile().load(usageFile); } catch (IOException e) { throw new RuntimeException(e); } catch (InvalidConfigurationException e) { throw new RuntimeException(e); } } }); } public void createUsageFile(boolean overwrite) { usageFile = new File(getDataFolder(), "usage.yml"); if (!usageFile.exists() || overwrite || usageFile.length() == 0L) { usageFile.getParentFile().mkdirs(); saveResource("usage.yml", true); } this.usageFileConfig = YamlConfiguration.loadConfiguration(usageFile); } public void enableOpenAI() { String key = getConfig().getString("api_key"); if (key == null || key.length() < 15) { getLogger().severe("No API key specified in config! Must set an API key for CraftGPT to work!"); return; } else { apiKeySet = true; } // Create HTTP client and OpenAI connection with configurable proxy and timeout ObjectMapper mapper = defaultObjectMapper(); OkHttpClient client; Duration timeout = Duration.ofSeconds(getConfig().getInt("timeout")); if (getConfig().getBoolean("proxy.enabled")) { Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(getConfig().getString("proxy.host"), getConfig().getInt("proxy.port"))); if (getConfig().getBoolean("proxy.authentication.enabled")) { getLogger().info("Authenticating to HTTP proxy..."); Authenticator proxyAuthenticator = new Authenticator() { @Override public Request authenticate(Route route, Response response) throws IOException { String credential = Credentials.basic(getConfig().getString("proxy.authentication.username"), getConfig().getString("proxy.authentication.password")); return response.request().newBuilder() .header("Proxy-Authorization", credential) .build(); } }; client = defaultClient(key, timeout) .newBuilder() .proxyAuthenticator(proxyAuthenticator) .proxy(proxy) .build(); } else { client = defaultClient(key, timeout) .newBuilder() .proxy(proxy) .build(); } getLogger().info("Connecting to OpenAI via proxy (" + getConfig().getString("proxy.host") + ":" + getConfig().getInt("proxy.port") + ")..."); } else { client = defaultClient(key, timeout) .newBuilder() .build(); } Retrofit retrofit = defaultRetrofit(client, mapper); OpenAiApi api = retrofit.create(OpenAiApi.class); openAIService = new OpenAiService(api); new BukkitRunnable() { @Override public void run() { long start = System.currentTimeMillis(); getLogger().info("Connecting to OpenAI..."); String response = tryNonChatRequest("Say hi", "Hi!", .1f, 2); if (response == null) { getLogger().severe("Tried 3 times and couldn't connect to OpenAI for the error(s) printed above!"); getLogger().severe("Read the error message carefully before asking for help in the Discord. Almost all errors are resolved by ensuring you have a valid and billable API key."); } else { long end = System.currentTimeMillis(); getLogger().info("Connected to OpenAI!" + " (" + ((end-start) / 1000f) + "s)"); apiConnected = true; } } }.runTaskAsynchronously(this); } public void writeData(CraftGPT craftGPT) { long start = System.currentTimeMillis(); Path path = Paths.get(craftGPT.getDataFolder() + "/data.json"); try { if(!Files.exists(path)) { Files.createDirectories(path.getParent()); Files.createFile(path); getLogger().severe("No data.json exists! Creating empty one."); // Initialize with empty JSON Files.write(path, "{}".getBytes(StandardCharsets.UTF_8)); } } catch (IOException e) { e.printStackTrace(); } try (BufferedWriter bufferedWriter = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) { gson.toJson(craftGPTData, bufferedWriter); long end = System.currentTimeMillis(); getLogger().info("Wrote data.json! (" + (end-start) + "ms)"); } catch (IOException e) { e.printStackTrace(); } } //fixme Probably much better way of handling this. The ChatMessage type couldn't be automatically parsed by gson public class ClassTypeAdapter implements JsonSerializer<Class<ChatMessage>>, JsonDeserializer<Class<?>> { @Override public JsonElement serialize(Class<ChatMessage> src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src.getName()); } @Override public Class<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { return Class.forName(json.getAsString()); } catch (ClassNotFoundException e) { throw new JsonParseException(e); } } } public ConcurrentHashMap<String, AIMob> readData(CraftGPT craftGPT) { Path path = Paths.get(craftGPT.getDataFolder() + "/data.json"); try { if (!Files.exists(path)) { getLogger().info("Creating data.json"); Files.createDirectories(path.getParent()); Files.createFile(path); // Initialize with empty JSON Files.write(path, "{}".getBytes(StandardCharsets.UTF_8)); } } catch (IOException e) { e.printStackTrace(); } try { BufferedReader bufferedReader = Files.newBufferedReader(path, StandardCharsets.UTF_8); JsonReader jsonReader = new JsonReader(bufferedReader); ConcurrentHashMap<String, AIMob> map = gson.fromJson(jsonReader, new TypeToken<ConcurrentHashMap<String, AIMob>>() {}.getType()); getLogger().info("Read data.json!"); return map; } catch (IOException e) { return null; } } public String rawProgressBar(int current, int max, int totalBars, char symbol, ChatColor completedColor, ChatColor notCompletedColor) { if (current > max) current = max; float percent = (float) current / max; int progressBars = (int) (totalBars * percent); return Strings.repeat("" + completedColor + symbol, progressBars) + Strings.repeat("" + notCompletedColor + symbol, totalBars - progressBars); } public String colorProgressBar(int current, int max, int totalBars) { ChatColor completedColor = ChatColor.GREEN; double percentage = (double) current / max; if (percentage > .5) { completedColor = ChatColor.YELLOW; } if (percentage > .75) { completedColor = ChatColor.RED; } return rawProgressBar(current, max, totalBars, '|', completedColor, ChatColor.GRAY); } public String getPlayerUsageProgressBar(Player player) { return colorProgressBar((int) getPlayerUsagePercentage(player), 100, 40); } public double getPlayerUsagePercentage(Player player) { long limit = CraftGPTListener.getTokenLimit(player); long usage = getUsageFile().getLong("players." + player.getUniqueId() + ".total-usage"); DecimalFormat dfZero = new DecimalFormat("0.00"); return Double.valueOf(dfZero.format(100.0 * usage / limit)); } public double getGlobalUsagePercentage() { long limit = getConfig().getLong("global-usage-limit"); long usage = getUsageFile().getLong("global-total-usage"); DecimalFormat dfZero = new DecimalFormat("0.00"); return Double.valueOf(dfZero.format(100.0 * usage / limit)); } public String getGlobalUsageProgressBar() { return colorProgressBar((int) getGlobalUsagePercentage(), 100, 40); } public boolean isAIMob(Entity entity) { if (craftGPTData.containsKey(entity.getUniqueId().toString())) return true; else return false; } public AIMob getAIMob(Entity entity) { if (isAIMob(entity)) { return craftGPTData.get(entity.getUniqueId().toString()); } else { return null; } } public boolean isChatting(Player player) { if (chattingPlayers.containsKey(player.getUniqueId())) return true; else return false; } public String tryNonChatRequest(String systemMessage, String userMessage, float temp, int maxTokens) { String errorSignature = null; String response; for (int i = 0; i < 3; i++) { try { response = nonChatRequest(systemMessage, userMessage, temp, maxTokens); return response; } catch (OpenAiHttpException e) { if (errorSignature != null && errorSignature.equals(e.statusCode + e.type)) { getLogger().warning("Failed again with identical error on try number " + (i+1) + "."); } else { printAPIErrorConsole(e); errorSignature = e.statusCode + e.type; } } catch (Exception e) { getLogger().warning(String.format("[Try %s] Non-OpenAI error: " + e.getMessage(), i)); if (!e.getMessage().contains("timeout")) { e.printStackTrace(); } } } return null; } public String nonChatRequest(String systemMessage, String userMessage, float temp, int maxTokens) { List<ChatMessage> chatMessages = new ArrayList<>(); chatMessages.add(new ChatMessage(ChatMessageRole.SYSTEM.value(), systemMessage)); chatMessages.add(new ChatMessage(ChatMessageRole.USER.value(), userMessage)); ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() .messages(chatMessages) .temperature((double) temp) .maxTokens(maxTokens) .model("gpt-3.5-turbo") .build(); return openAIService.createChatCompletion(completionRequest).getChoices().get(0).getMessage().getContent(); } public void printAPIErrorConsole(OpenAiHttpException e) { getLogger().warning("OpenAI API error!"); getLogger().warning("Error type: " + e.type); getLogger().warning("OpenAI error code: " + e.statusCode); getLogger().warning("OpenAI error message: " + e.getMessage()); if (e.getMessage().contains("quota")) { getLogger().warning("This is most often caused by an invalid API key or because your OpenAI account is not a paid account/does not have a payment method configured."); getLogger().warning("Using the API *REQUIRES* credits in your account which can either be purchased with a credit card or through a free trial."); getLogger().warning("More information on OpenAI errors available here: https://help.openai.com/en/collections/3808446-api-error-codes-explained"); } else if (e.getMessage().contains("Rate limit reached")) { getLogger().warning("This is most often occurs because the OpenAI free trial credits have a low rate limit of 3 messages/min. You must wait to send messages or add a billing method to your account."); } } public void toggleWaitingOnAPI(Entity entity) { if (isWaitingOnAPI(entity)) { waitingOnAPIList.remove(entity.getUniqueId().toString()); } else waitingOnAPIList.add(entity.getUniqueId().toString()); renameMob(entity); } public boolean isWaitingOnAPI(Entity entity) { if (waitingOnAPIList.contains(entity.getUniqueId().toString())) { return true; } else return false; } public void renameMob(Entity entity) { if (!(entity instanceof Player) && !entity.hasMetadata("NPC")) { entity.setCustomNameVisible(true); if (isWaitingOnAPI(entity)) { if (!craftGPTData.containsKey(entity.getUniqueId().toString())) { // Enabling mob (clock icon) entity.setCustomName("Enabling..." + ChatColor.YELLOW + " \u231A"); } else { // Waiting on API (clock icon) entity.setCustomName(craftGPTData.get(entity.getUniqueId().toString()).getName() + ChatColor.YELLOW + " \u231A"); } } else { if (chattingPlayers.containsValue(entity)) { // Currently chatting (green lightning bolt) entity.setCustomName(craftGPTData.get(entity.getUniqueId().toString()).getName() + ChatColor.GREEN + " \u26A1"); // star "\u2B50" } else { if (isAIMob(entity)) { // AI-enabled (blue lightning bolt) entity.setCustomName(craftGPTData.get(entity.getUniqueId().toString()).getName() + ChatColor.BLUE + " \u26A1"); } else { entity.setCustomName(null); entity.setCustomNameVisible(false); } } } } } public ChatMessage generateDefaultPrompt(AIMob aiMob) { String newPrompt = getConfig().getString("prompt.default-system-prompt"); newPrompt = newPrompt.replace("%ENTITY_TYPE%", aiMob.getEntityType()); newPrompt = newPrompt.replace("%BACKSTORY%", aiMob.getBackstory()); if (debug) getLogger().info("PROMPT: " + newPrompt); return new ChatMessage(ChatMessageRole.SYSTEM.value(), newPrompt); } public void createAIMobData(AIMob aiMob, String uuid) { if (debug) getLogger().info("************************************\n" + aiMob.getName() + "\n" + aiMob.getTemperature() + "\n" + aiMob.getMessages() + "\n" + aiMob.getBackstory()); craftGPTData.put(uuid, aiMob); writeData(this); } public void printFailureToCreateMob(Player player, Entity entity) { getLogger().severe("Mob at: " + entity.getLocation() + " failed to enable due to error printed above!"); player.sendMessage(CraftGPT.CHAT_PREFIX + ChatColor.RED + "ERROR: OpenAI API failure!"); player.sendMessage(ChatColor.RED + "======================================="); player.sendMessage(ChatColor.RED + "- This is most often caused by an invalid API key or because your OpenAI account is not a paid account/does not have a payment method configured."); player.sendMessage(ChatColor.RED + "- Using the API" + ChatColor.UNDERLINE + ChatColor.ITALIC + ChatColor.WHITE + " requires " + ChatColor.RESET + ChatColor.RED + "credits in your account from a credit card or free trial."); player.sendMessage(ChatColor.RED + "- For more information on the exact error, see the server logs."); player.sendMessage(ChatColor.RED + "======================================="); } public TextComponent getClickableCommandHoverText(String message, String command, String hoverText) { TextComponent textComponent = new TextComponent(message); textComponent.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command)); textComponent.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(hoverText).create())); return textComponent; } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((5847, 5900), 'org.bukkit.Bukkit.getPluginManager'), ((8083, 8576), 'org.bukkit.Bukkit.getScheduler'), ((18793, 18823), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((18883, 18911), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((18978, 19196), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((18978, 19171), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((18978, 19131), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((18978, 19093), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((18978, 19049), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((22808, 22838), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value')]
package com.georgster.control.manager; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import com.georgster.collectable.Collected; import com.georgster.control.util.ClientContext; import com.georgster.database.ProfileType; import com.georgster.economy.CoinBank; import com.georgster.gpt.MemberChatCompletions; import com.georgster.profile.UserProfile; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatCompletionResult; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; import discord4j.core.event.domain.guild.GuildCreateEvent; import discord4j.core.object.entity.Member; /** * Manages all {@link UserProfile UserProfiles} for a {@link com.georgster.control.SoapClient SoapClient}. */ public class UserProfileManager extends SoapManager<UserProfile> { private static OpenAiService aiService; //The singleton AI Service to communicate with OpenAI's API /** * Creates a new UserProfileManager for the given SoapClient's {@link ClientContext}. * * @param context The context of the SoapClient for this manager. */ public UserProfileManager(ClientContext context) { super(context, ProfileType.PROFILES, UserProfile.class, "memberId"); createAiService(); } /** * Creates the singleton OpenAiService. */ private static void createAiService() { try { if (aiService == null) { aiService = new OpenAiService(Files.readString(Path.of(System.getProperty("user.dir"),"src", "main", "java", "com", "georgster", "gpt", "openaikey.txt"))); } } catch (IOException e) { e.printStackTrace(); } } /** * Updates all user profiles information with the data in the {@link GuildCreateEvent}. * * @param event The event with all member information. */ public void updateFromEvent(GuildCreateEvent event) { event.getGuild().getMembers().subscribe(member -> { String id = member.getId().asString(); if (exists(id)) { // All manageables must maintained. UserProfile profile = get(id); MemberChatCompletions completions = profile.getCompletions(); if (completions == null) completions = new MemberChatCompletions(id); CoinBank bank = profile.getBank(); if (bank == null) bank = new CoinBank(id); List<Collected> collecteds = profile.getCollecteds(); if (collecteds == null) collecteds = new ArrayList<>(); update(new UserProfile(event.getGuild().getId().asString(), id, member.getTag(), completions, bank, collecteds)); } else { add(new UserProfile(event.getGuild().getId().asString(), id, member.getTag())); } }); } /** * Creates a new ChatCompletion for the provided {@link Member} based on the given * prompt and returns all responses. * <p> * The response will be based on the previous ten chat completions for that member * in this manager. * <p> * Uses OpenAI's gpt-3.5-turbo model. * <p> * <b>Note:</b> Only the first response will be saved in the member's chat completion log. * * @param prompt The prompt from the user. * @param member The member who prompted the completion request. * @return All responses from the AI. */ public List<String> createCompletionGetAll(String prompt, Member member) { UserProfile profile = get(member.getId().asString()); List<String> responses = new ArrayList<>(); createCompletionRequest(prompt, member).getChoices().forEach(choice -> responses.add(choice.getMessage().getContent())); profile.getCompletions().addCompletion(prompt, responses.get(0)); update(profile); return responses; } /** * Creates a new ChatCompletion for the provided {@link Member} based on the given * prompt and returns the first response. * The response will be based on the previous ten chat completions for that member * in this manager. Uses OpenAI's gpt-3.5-turbo model. * * @param prompt The prompt from the user. * @param member The member who prompted the completion request. * @return The first responses from the AI. */ public String createCompletion(String prompt, Member member) { UserProfile profile = get(member.getId().asString()); String response = createCompletionRequest(prompt, member).getChoices().get(0).getMessage().getContent(); profile.getCompletions().addCompletion(prompt, response); update(profile); return response; } /** * Creates a ChatCompletionRequest to OpenAI using the {@code gpt-3.5-turbo} model. * * @param prompt The prompt from the user. * @param member The member who prompted the completion request. * @return The result of the request. */ private ChatCompletionResult createCompletionRequest(String prompt, Member member) { String id = member.getId().asString(); List<ChatMessage> messages = new ArrayList<>(); messages.add(new ChatMessage("system", "You are a Discord bot called SOAP Bot.")); get(id).getCompletions().getTokens().forEach(token -> token.forEach((k,v) -> { messages.add(new ChatMessage("user", k)); messages.add(new ChatMessage("assistant", v)); }) ); messages.add(new ChatMessage("user", prompt)); ChatCompletionRequest request = ChatCompletionRequest.builder().messages(messages).model("gpt-3.5-turbo").build(); return aiService.createChatCompletion(request); } /** * Returns the total amount of coins for all users in this manager, * that is, the total amount of coins for users in a Guild. * * @return The total amount of coins for all users in this manager. */ public long getTotalCoins() { return getAll().stream().mapToLong(profile -> profile.getBank().getBalance()).sum(); } public void updateFromCollectables(CollectableManager manager) { manager.getAll().forEach(collectable -> collectable.getCollecteds().forEach(collected -> { UserProfile profile = get(collected.getMemberId()); List<Collected> collecteds = profile.getCollecteds(); collecteds.removeIf(c -> c.getIdentifier().equals(collected.getIdentifier())); collecteds.add(collected); update(profile); }) ); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((5814, 5895), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((5814, 5887), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((5814, 5864), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package de.ja.view.explanation.audio; import com.theokanning.openai.OpenAiHttpException; import com.theokanning.openai.audio.CreateSpeechRequest; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatCompletionResult; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessageRole; import com.theokanning.openai.service.OpenAiService; import de.ja.model.audio.SpeechResult; import de.ja.view.ExplainerFrame; import de.ja.view.explanation.audio.player.AudioPlayerPanel; import de.swa.gc.GraphCode; import net.miginfocom.swing.MigLayout; import okhttp3.ResponseBody; import org.jdesktop.swingx.JXTaskPane; import javax.swing.*; import javax.swing.border.TitledBorder; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.Collectors; /** * Diese Klasse stellt die Benutzerschnittstelle * zum Erstellen einer auditiven Erklärung von * Graph Codes dar. */ public class AudioPanel extends JPanel implements ActionListener { // API-Key. private static String key; private final JComboBox<String> modelComboBox; private final JComboBox<String> voiceComboBox; private final JComboBox<String> formatComboBox; private final JSpinner speedSpinner; // Textfeld für die generierte Prompt. private final JTextArea promptArea; private final JButton generateAudioButton; // Nachrichten, die die Prompt darstellen. private List<ChatMessage> messages = new ArrayList<>(); private final AudioPlayerPanel audioPlayerPanel; // Referenz. private final ExplainerFrame reference; public AudioPanel(ExplainerFrame reference) { this.reference = reference; key = System.getenv("OpenAI-Key"); // Layout definieren. MigLayout imagePanelMigLayout = new MigLayout("" , "[fill, grow]", "10[10%][][][fill,30%][60%]"); //1. 10% setLayout(imagePanelMigLayout); // Textfeld für die Prompt initialisieren und konfigurieren. promptArea = new JTextArea(); promptArea.setLineWrap(true); promptArea.setWrapStyleWord(true); promptArea.setEditable(false); JScrollPane promptSP = new JScrollPane(promptArea); promptSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); promptSP.setBorder(new TitledBorder("Generated Prompt")); add(promptSP, "cell 0 3, growx, height ::30%, aligny top"); // Ästhetische Eigenschaften für erweiterte Optionen einstellen... UIManager.put("TaskPane.animate", Boolean.FALSE); UIManager.put("TaskPane.titleOver", new Color(200, 200, 200)); UIManager.put("TaskPane.titleForeground", new Color(187, 187, 187)); UIManager.put("TaskPane.titleBackgroundGradientStart", new Color(85, 88, 89)); UIManager.put("TaskPane.titleBackgroundGradientEnd", new Color(85, 88, 89)); UIManager.put("TaskPane.background", new Color(76, 80, 82)); UIManager.put("TaskPane.borderColor", new Color(94, 96, 96)); // Erweiterte Optionen initialisieren und konfigurieren. JXTaskPane advancedOptions = new JXTaskPane(); advancedOptions.setCollapsed(true); advancedOptions.setTitle("Advanced Options"); add(advancedOptions, "cell 0 0, growx, aligny top"); // Layout für die Optionen in den erweiterten Optionen definieren. MigLayout advancedOptionsMigLayout = new MigLayout("", "0[]5[]10[]5[]0", "0[]0"); advancedOptions.setLayout(advancedOptionsMigLayout); // Erweiterte Optionen definieren. JLabel modelLabel = new JLabel("Model:"); modelLabel.setHorizontalTextPosition(SwingConstants.CENTER); modelLabel.setHorizontalAlignment(SwingConstants.CENTER); modelComboBox = new JComboBox<>(); modelComboBox.addItem("tts-1"); modelComboBox.addItem("tts-1-hd"); JLabel voiceLabel = new JLabel("Voice:"); voiceLabel.setHorizontalTextPosition(SwingConstants.CENTER); voiceLabel.setHorizontalAlignment(SwingConstants.CENTER); voiceComboBox = new JComboBox<>(); voiceComboBox.addItem("alloy"); voiceComboBox.addItem("echo"); voiceComboBox.addItem("fable"); voiceComboBox.addItem("onyx"); voiceComboBox.addItem("nova"); voiceComboBox.addItem("shimmer"); JLabel formatLabel = new JLabel("Format:"); formatLabel.setHorizontalTextPosition(SwingConstants.CENTER); formatLabel.setHorizontalAlignment(SwingConstants.CENTER); formatComboBox = new JComboBox<>(); formatComboBox.addItem("mp3"); formatComboBox.addItem("opus"); formatComboBox.addItem("aac"); formatComboBox.addItem("flac"); JLabel speedLabel = new JLabel("Speed:"); speedLabel.setHorizontalTextPosition(SwingConstants.CENTER); speedLabel.setHorizontalAlignment(SwingConstants.CENTER); SpinnerNumberModel nSpinnerNumberModel = new SpinnerNumberModel(1, 0.25, 4.0, 0.01); speedSpinner = new JSpinner(); speedSpinner.setModel(nSpinnerNumberModel); advancedOptions.add(modelLabel); advancedOptions.add(modelComboBox); advancedOptions.add(voiceLabel); advancedOptions.add(voiceComboBox); advancedOptions.add(formatLabel); advancedOptions.add(formatComboBox); advancedOptions.add(speedLabel); advancedOptions.add(speedSpinner); // Knopf zum Generieren von Bildern. generateAudioButton = new JButton("Generate Audio"); generateAudioButton.addActionListener(this); add(generateAudioButton, "cell 0 1, width ::150px, aligny top"); audioPlayerPanel = new AudioPlayerPanel(); add(audioPlayerPanel,"cell 0 2, growx, aligny top"); } /** * Graph Code verarbeiten * @param graphCode Ausgewählter Graph Code. */ public void setGraphCode(GraphCode graphCode) { if(graphCode != null) { // Prompt vorbereiten. String prompt = setUpPrompt(graphCode); promptArea.setText(prompt); } else { promptArea.setText(null); } } /** * Prompt vorbereiten und aus Graph Code * generieren. * @param graphCode Ausgewählter Graph Code. * @return Generierte Prompt. */ private String setUpPrompt(GraphCode graphCode) { // Alle Paare die über eine 1-Beziehung verfügen. String s = graphCode.getFormattedTerms(); // Textnachrichten für die Prompt. messages = new ArrayList<>(); messages.add(new ChatMessage(ChatMessageRole.SYSTEM.value(), "You are an assistant, who is able to generate cohesive textual explanations based on a collection of words.")); messages.add(new ChatMessage( ChatMessageRole.ASSISTANT.value(), "The collection of words represents a dictionary. The dictionary contains so-called feature " + "vocabulary terms. Additionally some of these terms are connected through a relationship. " + "These relationships will be noted as <i_t> - <i_t1,...,i_tn>, where i_t denotes the index of a feature " + "vocabulary term in the given collection.")); messages.add(new ChatMessage( "assistant", "Using these terms, we can create a coherent explanation that accurately " + "describes the terms and its relations.\n" + "\n" + "An example could be: The image shows water, the sky, and clouds. " + "We can imagine a scene with clouds floating in the sky above.")); messages.add(new ChatMessage( "user", "The collections of words is as follows: " + graphCode.listTerms() + ". Only respect these terms and its relations: " + s + ", and ignore all others. " + "Do not create an explanation regarding the dictionary. Only generate a text containing " + "the terms of the dictionary like in the example above.")); messages.add(new ChatMessage( "assistant", "Based on the dictionary, here is a cohesive text " + "containing the terms from the dictionary:")); // Nachrichten zusammenfügen. return messages.stream().map(ChatMessage::getContent).collect(Collectors.joining("\n")); } @Override public void actionPerformed(ActionEvent e) { audioPlayerPanel.resetSpeechResult(); // Anbindung zur Schnittstelle. OpenAiService service = new OpenAiService(key, Duration.ofSeconds(60)); if(key.isEmpty()) { reference.getExplainerConsoleModel().insertText("OpenAI-Key is missing, abort process. Must be set in launch-config: OpenAI-Key=..."); return; } // Prozess erstellen. ExecutorService executorService = Executors.newSingleThreadExecutor(); Thread t = new Thread(() -> { // Textanfrage initialisieren und parametrisieren. ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() .messages(messages) .model("gpt-4") .maxTokens(256) .build(); try { // Cursor auf Warten setzen. setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); // Knopf deaktivieren. generateAudioButton.setEnabled(false); // Info in der Konsole ausgeben. reference.getExplainerConsoleModel() .insertText("Generating an auditive explanation!"); // Textanfrage an Endpunkt senden. ChatCompletionResult chatCompletionResult = service.createChatCompletion(chatCompletionRequest); // Ergebnis der Anfrage. String chatResponse = chatCompletionResult.getChoices().get(0).getMessage().getContent(); // Bildanfrage initialisieren und parametrisieren. CreateSpeechRequest speechRequest = CreateSpeechRequest.builder() .input(chatResponse) .model((String) Objects.requireNonNull(modelComboBox.getSelectedItem())) .voice((String) Objects.requireNonNull(voiceComboBox.getSelectedItem())) .responseFormat((String) formatComboBox.getSelectedItem()) .speed((Double) speedSpinner.getValue()) .build(); ResponseBody speechResponseBody = service.createSpeech(speechRequest); SpeechResult speechResult = SpeechResult.builder() .audioType(speechResponseBody.contentType()) .bytes(speechResponseBody.bytes()) .build(); audioPlayerPanel.setSpeechResult(speechResult); } catch(OpenAiHttpException openAiHttpException) { if(openAiHttpException.statusCode == 401) { JOptionPane.showMessageDialog(null, "You provided an invalid API-Key!", "Authentication Error", JOptionPane.ERROR_MESSAGE); reference.getExplainerConsoleModel().insertText("You provided an invalid API-Key!"); } } catch (Exception ex) { ex.printStackTrace(); // Fehler in Konsole ausgeben. reference.getExplainerConsoleModel().insertText(ex.getMessage()); } finally { // Cursor auf Standard zurücksetzen. setCursor(Cursor.getDefaultCursor()); // Knopf reaktivieren. generateAudioButton.setEnabled(true); } }); // Prozess ausführen und beenden. executorService.execute(t); executorService.shutdown(); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.audio.CreateSpeechRequest.builder", "com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((6946, 6976), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((7161, 7194), 'com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value'), ((9558, 9730), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((9558, 9701), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((9558, 9665), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((9558, 9629), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((10573, 11022), 'com.theokanning.openai.audio.CreateSpeechRequest.builder'), ((10573, 10989), 'com.theokanning.openai.audio.CreateSpeechRequest.builder'), ((10573, 10924), 'com.theokanning.openai.audio.CreateSpeechRequest.builder'), ((10573, 10841), 'com.theokanning.openai.audio.CreateSpeechRequest.builder'), ((10573, 10744), 'com.theokanning.openai.audio.CreateSpeechRequest.builder'), ((10573, 10647), 'com.theokanning.openai.audio.CreateSpeechRequest.builder'), ((11155, 11338), 'de.ja.model.audio.SpeechResult.builder'), ((11155, 11305), 'de.ja.model.audio.SpeechResult.builder'), ((11155, 11246), 'de.ja.model.audio.SpeechResult.builder')]
package com.yoazmenda.llm4j.provider; import com.theokanning.openai.OpenAiService; import com.theokanning.openai.completion.CompletionRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class OpenAILlm implements LlmProvider { private static final Logger logger = LoggerFactory.getLogger(OpenAILlm.class); private String modelName; private Double temperature; private Integer maxTokens; private OpenAiService service; public OpenAILlm(String apiKey, String modelName, Double temperature, Integer maxTokens) { this.modelName = modelName; this.temperature = temperature; this.maxTokens = maxTokens; service = new OpenAiService(apiKey); } @Override public String getCompletions(String prompt) { CompletionRequest completionRequest = CompletionRequest.builder() .prompt(prompt) .model(modelName) .maxTokens(maxTokens) .temperature(temperature) .n(1) .build(); logger.debug("completion request: {}", completionRequest.toString()); String response = service.createCompletion(completionRequest).getChoices().get(0).getText(); logger.debug("completion response: {}", response); return response; } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((833, 1053), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((833, 1028), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((833, 1006), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((833, 964), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((833, 926), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((833, 892), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package com.example.demo.provider; import com.theokanning.openai.service.OpenAiService; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import java.util.Collections; @Service public class OpenAIProvider { @Value("${openai.apiKey}") private String apiKey; public String askOpenAI(String prompt) { if (apiKey == null || apiKey.isEmpty()) { throw new IllegalArgumentException("OPENAI_API_KEY environment variable not set"); } OpenAiService service = new OpenAiService(apiKey); ChatMessage message = new ChatMessage("user", prompt); ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() .model("gpt-3.5-turbo") .messages(Collections.singletonList(message)) .maxTokens(1000) .n(1) .build(); String response = service.createChatCompletion(completionRequest).getChoices().get(0).getMessage().getContent(); return response; } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((830, 1044), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((830, 1019), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((830, 997), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((830, 964), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((830, 901), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package com.copybot.bonappcopybot.openai; import com.copybot.bonappcopybot.model.entity.drinks.wine.Wine; import com.copybot.bonappcopybot.model.entity.drinks.wine.WineCopy; import com.theokanning.openai.OpenAiService; import com.theokanning.openai.completion.CompletionRequest; public class WineCopyAI { static WineCopyAI obj = new WineCopyAI(); private WineCopyAI(){} public static WineCopyAI getInstance(){ return obj; } public String getWineCopy(Wine wine, WineCopy copy){ String APIKey = "APIKEY"; OpenAiService service = new OpenAiService(APIKey); String prompt = createPrompt(wine, copy); System.out.println(prompt); CompletionRequest completionRequest = CompletionRequest.builder() .prompt(prompt) .temperature(0.9) .maxTokens(150) .topP(1.0) .frequencyPenalty(1.0) .presencePenalty(0.3) .echo(false) .build(); String copyResult = service.createCompletion("text-davinci-001", completionRequest).getChoices().get(0).getText(); System.out.println(copyResult); copyResult = copyResult.replaceFirst("\\n\\n", ""); String lang = copy.getLang(); if (lang.equals("English") || lang.equals("english") || lang.equals("en") || lang.equals("EN")){ return copyResult; }else { String translated = translateCopy(copyResult, lang); return translated; } } private String createPrompt(Wine wine, WineCopy copy){ String prompt = "Write an engaging and joyfull text add for " + copy.getOccasion() + ", with 30 words for the following product: "; prompt += " Type: Wine,"; prompt += " Name : " + wine.getName() + ", "; prompt += " Product: " + wine.getProduct() + ", "; prompt += " Brand: " + wine.getBrand() + ", "; prompt += " Date: " + wine.getDate() + ", "; prompt += " Color: " + wine.getColor() + ", "; prompt += " Ingredients: " + wine.getIngredients() + ", "; prompt += " Description: " + wine.getDescription() + "->"; return prompt; } private String translateCopy(String originalCopy, String lang){ String APIKey = "APIKEY"; OpenAiService service = new OpenAiService(APIKey); String prompt = "Translate the following text to " + lang + ": " + originalCopy; System.out.println(prompt); CompletionRequest completionRequest = CompletionRequest.builder() .prompt(prompt) .temperature(0.9) .maxTokens(150) .topP(1.0) .frequencyPenalty(1.0) .presencePenalty(0.3) .echo(false) .build(); String copyResult = service.createCompletion("text-davinci-001", completionRequest).getChoices().get(0).getText(); copyResult = copyResult.replaceFirst("\\n\\n", ""); return copyResult; } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((737, 1020), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((737, 995), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((737, 966), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((737, 928), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((737, 889), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((737, 862), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((737, 830), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((737, 796), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2563, 2846), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2563, 2821), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2563, 2792), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2563, 2754), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2563, 2715), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2563, 2688), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2563, 2656), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2563, 2622), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package com.minivv.pilot.utils; import com.minivv.pilot.model.AppSettings; import com.theokanning.openai.OpenAiApi; import com.theokanning.openai.completion.CompletionChoice; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.service.OpenAiService; import okhttp3.OkHttpClient; import org.apache.commons.collections.CollectionUtils; import retrofit2.Retrofit; import java.net.InetSocketAddress; import java.net.Proxy; import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.Locale; import static com.theokanning.openai.service.OpenAiService.*; public class GPTClient { public static List<CompletionChoice> callChatGPT(String code, AppSettings settings) { try { Locale.setDefault(Locale.getDefault()); if (settings.enableProxy) { Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(settings.proxyHost, settings.proxyPort)); OkHttpClient client = defaultClient(settings.gptKey, Duration.ofSeconds(settings.maxWaitSeconds)) .newBuilder() .proxy(proxy) .build(); Retrofit retrofit = defaultRetrofit(client, defaultObjectMapper()); OpenAiApi api = retrofit.create(OpenAiApi.class); OpenAiService service = new OpenAiService(api); CompletionRequest completionRequest = CompletionRequest.builder() .prompt(code) .model(settings.gptModel) .maxTokens(settings.gptMaxTokens) .temperature(0.3) .presencePenalty(0.0) .frequencyPenalty(0.0) .bestOf(1) .stream(false) .echo(false) .build(); return service.createCompletion(completionRequest).getChoices(); } else { OpenAiService service = new OpenAiService(settings.gptKey); CompletionRequest completionRequest = CompletionRequest.builder() .prompt(code) .model(settings.gptModel) .echo(true) .build(); return service.createCompletion(completionRequest).getChoices(); } } catch (Exception e) { return new ArrayList<>(); } } public static boolean isSuccessful(List<CompletionChoice> choices) { return CollectionUtils.isNotEmpty(choices) && !choices.get(0).getText().isBlank(); } public static String toString(List<CompletionChoice> choices) { if (CollectionUtils.isEmpty(choices)) { return "ChatGPT response is empty,please check your network or config!"; } return choices.get(0).getText(); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((1462, 1914), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1462, 1881), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1462, 1844), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1462, 1805), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1462, 1770), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1462, 1723), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1462, 1677), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1462, 1635), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1462, 1577), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1462, 1527), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2148, 2332), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2148, 2299), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2148, 2263), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2148, 2213), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package dev.lueem.ai; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.io.FileInputStream; import java.io.IOException; 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; public class OpenAiClient { private OpenAiService service; private Properties properties; public OpenAiClient() { properties = new Properties(); try { properties.load(new FileInputStream("config.properties")); String apiKey = properties.getProperty("openai.api.key"); service = new OpenAiService(apiKey); } catch (IOException e) { e.printStackTrace(); } } public String askQuestion(String question) { List<ChatMessage> messages = new ArrayList<>(); messages.add(new ChatMessage(ChatMessageRole.USER.value(), question)); ChatCompletionRequest request = ChatCompletionRequest.builder() .model("gpt-3.5-turbo-16k-0613") .messages(messages) .n(1) .maxTokens(10000) .build(); ChatMessage responseMessage = service.createChatCompletion(request).getChoices().get(0).getMessage(); return responseMessage.getContent(); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1008, 1036), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((1091, 1288), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1091, 1263), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1091, 1229), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1091, 1207), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1091, 1171), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package ssodamproject.server.GPT.dto; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.ToString; import java.util.List; @Getter @NoArgsConstructor @AllArgsConstructor @ToString public class GPTCompletionChatRequest { private String model; private String role1; private String message1; private String role2; private String message2; private Integer max_tokens; private Double top_n; private Double temperature; private Double frequency_penalty; public static ChatCompletionRequest of(GPTCompletionChatRequest request) { return ChatCompletionRequest.builder() .model(request.getModel()) .frequencyPenalty(1.0) .messages(convertChatMessages(request)) .maxTokens(300) .temperature(0.2) .topP(0.92) .build(); } private static List<ChatMessage> convertChatMessages(GPTCompletionChatRequest request) { ChatMessage message1 = new ChatMessage(request.getRole1(), request.getMessage1()); ChatMessage message2 = new ChatMessage(request.getRole2(), request.getMessage2()); return List.of(message1, message2); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((770, 1058), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((770, 1033), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((770, 1005), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((770, 971), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((770, 939), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((770, 883), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((770, 844), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package br.com.alura.screenmatch.service; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.service.OpenAiService; public class ConsultaChatGPT { public static String obterTraducao(String texto) { OpenAiService service = new OpenAiService("my key"); CompletionRequest requisicao = CompletionRequest.builder() .model("gpt-3.5-turbo-instruct") .prompt("traduza para o português o texto: " + texto) .maxTokens(1000) .temperature(0.7) .build(); var resposta = service.createCompletion(requisicao); return resposta.getChoices().get(0).getText().trim(); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((353, 597), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((353, 571), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((353, 536), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((353, 502), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((353, 430), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package com.theagilemonkeys.ellmental.textgeneration.openai; import com.theagilemonkeys.ellmental.textgeneration.TextGenerationService; import com.theagilemonkeys.ellmental.textgeneration.openai.errors.NoContentFoundException; import com.theokanning.openai.completion.chat.ChatCompletionChoice; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.time.Duration; import java.util.List; public class OpenAiChatGenerationModel extends TextGenerationService<ChatMessage> { private static final Double DEFAULT_TEMPERATURE = 0.7; private static final int DEFAULT_MAX_TOKENS = 3000; private static final Logger log = LoggerFactory.getLogger(OpenAiChatGenerationModel.class); private final Double temperature; private final int maxTokens; private final OpenAiModels model; // The OpenAI client is package-private to allow injecting a mock in tests OpenAiService openAiService; /** * Constructor for OpenAiTextGenerationModel that uses default values for temperature (0.7), * maxTokens (3000) and model (GPT-3.5). * @param openAiKey OpenAI API key */ public OpenAiChatGenerationModel(String openAiKey) { this(openAiKey, OpenAiModels.GPT_3_5, DEFAULT_TEMPERATURE, DEFAULT_MAX_TOKENS); } /** * Constructor for OpenAiTextGenerationModel that explicitly sets the model, but uses the default values for * temperature (0.7) and maxTokens (3000). * @param openAiKey OpenAI API key * @param model Model to use for the chat generation */ public OpenAiChatGenerationModel(String openAiKey, OpenAiModels model) { this(openAiKey, model, DEFAULT_TEMPERATURE, DEFAULT_MAX_TOKENS); } /** * Constructor for OpenAiTextGenerationModel that explicitly sets the temperature, maxTokens and model. * @param openAiKey OpenAI API key * @param temperature Temperature to use for the chat generation * @param maxTokens Maximum number of tokens to use for the chat generation * @param model Model to use for the chat generation */ public OpenAiChatGenerationModel(String openAiKey, OpenAiModels model, Double temperature, int maxTokens) { this.openAiService = new OpenAiService(openAiKey, Duration.ofSeconds(240)); this.temperature = temperature; this.maxTokens = maxTokens; this.model = model; } /** * Generates a chat response using the OpenAI API. * @param chatMessages List of chat messages to use as context for the response * @return Generated chat response */ @Override public String generate(List<ChatMessage> chatMessages) { log.debug("Generating chat response for chat messages {}", chatMessages); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() .messages(chatMessages) .temperature(temperature) .maxTokens(maxTokens) .model(model.getCodename()) .build(); ChatCompletionChoice chatCompletionChoice = openAiService.createChatCompletion(chatCompletionRequest).getChoices().get(0); String chatCompletionContent = chatCompletionChoice.getMessage().getContent(); log.debug("Chat completion response is {}", chatCompletionContent); if (chatCompletionContent.isEmpty()) { throw new NoContentFoundException(chatMessages); } return chatCompletionContent; } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((2962, 3182), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((2962, 3157), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((2962, 3113), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((2962, 3075), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((2962, 3033), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package org.example; 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; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; public class Chat { public static void main(String[] args) { // .env 파일에서 open api key 가져오기 String token = ""; try (InputStream inputStream = Chat.class.getClassLoader().getResourceAsStream(".env"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));) { token = bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); } OpenAiService service = new OpenAiService(token); //보낼 메시지 final List<ChatMessage> messages = new ArrayList<>(); final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), "You are a helpful assistant."); final ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), "effective java item 7 자세히 설명해줘"); messages.add(systemMessage); messages.add(userMessage); // 요청 내용과 설정 만들기 ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder() .model("gpt-3.5-turbo") .messages(messages) .n(1) .maxTokens(50) .logitBias(new HashMap<>()) .build(); // 서비스에 요청을 보내고 결과 값을 받아옴. ArrayList<String> result = new ArrayList<>(); service.streamChatCompletion(chatCompletionRequest) .doOnError(Throwable::printStackTrace) .blockingForEach(chunk -> { String content = chunk.getChoices().get(0).getMessage().getContent(); result.add(content); }); // 답변 하나로 묶고 프린트 String answer = result.stream().filter(Objects::nonNull).collect(Collectors.joining("")); System.out.println(answer); service.shutdownExecutor(); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value" ]
[((1206, 1236), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((1327, 1355), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value')]
package com.example.telegramdailybot.service; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; @Service public class ChatGPT3Service { private final OpenAiService openAiService; private final int maxTokens; public ChatGPT3Service( @Value("${openai.token}") String openAiToken, @Value("${openai.timeout}") long timeoutSeconds, @Value("${openai.maxTokens}") int maxTokens) { this.openAiService = new OpenAiService(openAiToken, Duration.ofSeconds(timeoutSeconds)); this.maxTokens = maxTokens; } @Async public CompletableFuture<String> chat(String inputText) { List<ChatMessage> chatMessages = new ArrayList<>(); chatMessages.add(new ChatMessage("user", inputText)); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() .model("gpt-3.5-turbo") // You can choose another model if you want .messages(chatMessages) .maxTokens(maxTokens) .build(); return CompletableFuture.supplyAsync(() -> openAiService.createChatCompletion(chatCompletionRequest).getChoices().get(0).getMessage().getContent() ); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1235, 1453), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1235, 1428), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1235, 1390), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1235, 1306), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package com.blackn0va; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Random; import com.github.theholywaffle.teamspeak3.TS3Api; import com.github.theholywaffle.teamspeak3.TS3ApiAsync; import com.github.theholywaffle.teamspeak3.TS3Config; import com.github.theholywaffle.teamspeak3.TS3Query; import com.github.theholywaffle.teamspeak3.api.TextMessageTargetMode; import com.github.theholywaffle.teamspeak3.api.event.ClientMovedEvent; import com.github.theholywaffle.teamspeak3.api.event.TS3EventAdapter; import com.github.theholywaffle.teamspeak3.api.event.TS3EventType; import com.github.theholywaffle.teamspeak3.api.event.TextMessageEvent; import com.github.theholywaffle.teamspeak3.api.reconnect.ConnectionHandler; import com.github.theholywaffle.teamspeak3.api.reconnect.ReconnectStrategy; import com.theokanning.openai.completion.CompletionRequest; 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; public class App { public static int ClientIDBlack = 0; public static int ClientIDFly = 0; public static int ClientIDChukky = 0; public static String randomName = ""; public static String answer = ""; public static final TS3Config config = new TS3Config(); private static volatile int clientId; public static final List<ChatMessage> messages = new ArrayList<>(); public static void main(String[] args) { GenerateNickname(randomName); config.setHost("ts.laonda-clan.eu"); config.setEnableCommunicationsLogging(true); // Use default exponential backoff reconnect strategy config.setReconnectStrategy(ReconnectStrategy.exponentialBackoff()); // Make stuff run every time the query (re)connects config.setConnectionHandler(new ConnectionHandler() { @Override public void onConnect(TS3Api api) { stuffThatNeedsToRunEveryTimeTheQueryConnects(api); } @Override public void onDisconnect(TS3Query ts3Query) { // Nothing } }); final TS3Query query = new TS3Query(config); // Here "stuffThatNeedsToRunEveryTimeTheQueryConnects" will be run! // (And every time the query reconnects) query.connect(); // Then do other stuff that only needs to be done once stuffThatOnlyEverNeedsToBeRunOnce(query.getApi()); doSomethingThatTakesAReallyLongTime(query.getAsyncApi()); // Disconnect once we're done // query.exit(); /* * * * * * * //Get all channel ID´s * //api.getChannels().forEach(channel -> System.out.println(channel.getName() + * " " + channel.getId())); * * //get User ID´s * //api.getClients().forEach(client -> System.out.println(client.getNickname() * + " " + client.getId())); * * * //join channel * * * //api.moveClient(6353, 417); * * * * * api.getChannels().forEach(channel -> api.moveClient(ClientIDBlack, * channel.getId())); */ /* * * Zockerhallen 413 * Zockerhalle I 412 * Zockerhalle II 414 * Zockerhalle III 415 * Zockerhalle IV 416 * Zockerhalle V 417 */ } // funktion return String public static String GenerateNickname(String nickname) { // Create Random Names Random rand = new Random(); int randomNum = rand.nextInt((999 - 100) + 1) + 100; randomName = "Bot-" + randomNum; return randomName; } public static void BotStarten() { try { } catch (Exception e) { } } private static void stuffThatNeedsToRunEveryTimeTheQueryConnects(TS3Api api) { try { GenerateNickname(randomName); // Logging in, selecting the virtual server, selecting a channel // and setting a nickname needs to be done every time we reconnect api.login("test", "d4rz"); api.selectVirtualServerById(1); // api.moveQuery(x); api.setNickname(randomName); api.moveQuery(412); // What events we listen to also resets // api.registerEvent(TS3EventType.TEXT_CHANNEL, 415); // Out clientID changes every time we connect and we need it // for our event listener, so we need to store the ID in a field clientId = api.whoAmI().getId(); api.registerAllEvents(); api.registerEvent(TS3EventType.SERVER); api.registerEvent(TS3EventType.TEXT_CHANNEL); api.registerEvent(TS3EventType.TEXT_PRIVATE); api.registerEvent(TS3EventType.TEXT_SERVER); api.addTS3Listeners(new TS3EventAdapter() { @Override public void onTextMessage(TextMessageEvent e) { // if message is private then send message to user who send the message if (e.getTargetMode() == TextMessageTargetMode.CLIENT) { if (!e.getInvokerName().contains("Bot")) { String frage = e.getMessage(); final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.USER.value(), frage); messages.add(systemMessage); // call get answer and return answer answer = getAnswer(frage); // api.sendChannelMessage(answer); api.sendPrivateMessage(e.getInvokerId(), answer); System.out.println(answer); } } else { if (!e.getInvokerName().contains("Bot")) { String frage = e.getMessage(); final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.USER.value(), frage); messages.add(systemMessage); // call get answer and return answer answer = getAnswer(frage); // api.sendChannelMessage(answer); api.sendPrivateMessage(e.getInvokerId(), answer); System.out.println(answer); api.sendChannelMessage(answer); System.out.println(answer); } } } @Override public void onClientMoved(ClientMovedEvent e) { // System.out.println(e.getClientNickname() + " moved to channel " + // e.getTargetChannelId()); // send message to user // if channel is 415 then send message if (e.getTargetChannelId() == 415) { // call get answer and return answer // answer = getAnswer("Stell dich in 50 worten vor. Du bist eine KI und stellst // dich freundlich vor. Sag auch, dass sie bitte mit dir schreiben müssen, da du // nichts hörst. Und bitte sag Du und nicht Sie"); api.sendPrivateMessage(e.getClientId(), "Hallo, ich bin eine KI. Ich freue mich, dich kennenzulernen! Ich bin hier, um dir zu helfen und deine Fragen zu beantworten. Bitte schreib mir deine Fragen, da ich nichts hören kann. Ich werde mein Bestes geben, um dir so schnell wie möglich zu antworten. Wenn du noch etwas brauchst, lass es mich bitte wissen. Ich freue mich darauf, mit dir zu interagieren!"); } } }); } catch (Exception e) { System.out.println(e); } } // funktion return string antwort public static String getAnswer(String question) { try { OpenAiService service = new OpenAiService("sk-pJQ9H7UHKpAPI_Keyu"); System.out.println("\nCreating completion... "); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder() .model("gpt-3.5-turbo") .temperature(0.5) .presencePenalty(0.0) .frequencyPenalty(0.5) .messages(messages) .n(1) .maxTokens(200) .logitBias(new HashMap<>()) .build(); // String answer = // service.createCompletion(completionRequest).getChoices(completionResponse -> // completionResponse.getChoices().toString().replace("[CompletionChoice(text=", // "").replace(", index=)]", "").replace(", logprobs=", "").replace(", // finish_reason=", "").replace(", index=0nullstop)", "").replace("]", "")); // service.createCompletion(completionRequest).getChoices().forEach(System.out::print); answer = service.createChatCompletion(chatCompletionRequest).getChoices().toString() .replace("[CompletionChoice(text=", "").replace(", index=)]", "") .replace(", logprobs=", "").replace(", finish_reason=", "") .replace(", index=0nullstop)", "").replace("]", "") .replace("index=0nulllength)", "") .replace("[ChatCompletionChoice(index=0, message=ChatMessage(role=assistant, content=", "") .replace("), finishReason=stop)", ""); return answer; } catch (Exception e) { } return answer; } private static void stuffThatOnlyEverNeedsToBeRunOnce(final TS3Api api) { // We only want to greet people once // api.sendChannelMessage("PutPutBot is online!"); GenerateNickname(randomName); // On the API side of things, you only need to register your TS3Listeners once! // These are not affected when the query disconnects. api.registerAllEvents(); api.registerEvent(TS3EventType.SERVER); api.registerEvent(TS3EventType.TEXT_CHANNEL); api.registerEvent(TS3EventType.TEXT_PRIVATE); api.registerEvent(TS3EventType.TEXT_SERVER); api.addTS3Listeners(new TS3EventAdapter() { @Override public void onTextMessage(TextMessageEvent e) { // if message is private then send message to user who send the message if (e.getTargetMode() == TextMessageTargetMode.CLIENT) { if (!e.getInvokerName().contains("Bot")) { // call get answer and return answer answer = getAnswer(e.getMessage()); // api.sendChannelMessage(answer); api.sendPrivateMessage(e.getInvokerId(), answer); System.out.println(answer); } } else { if (!e.getInvokerName().contains("Bot")) { // call get answer and return answer answer = getAnswer(e.getMessage()); api.sendChannelMessage(answer); System.out.println(answer); } } } @Override public void onClientMoved(ClientMovedEvent e) { // System.out.println(e.getClientNickname() + " moved to channel " + // e.getTargetChannelId()); // send message to user // if channel is 415 then send message if (e.getTargetChannelId() == 415) { // answer = getAnswer("Stell dich in 50 worten vor. Du bist eine KI und stellst // dich freundlich vor. Sag auch, dass sie bitte mit dir schreiben müssen, da du // nichts hörst. Und bitte sag Du und nicht Sie"); api.sendPrivateMessage(e.getClientId(), "Hallo, ich bin eine KI. Ich freue mich, dich kennenzulernen! Ich bin hier, um dir zu helfen und deine Fragen zu beantworten. Bitte schreib mir deine Fragen, da ich nichts hören kann. Ich werde mein Bestes geben, um dir so schnell wie möglich zu antworten. Wenn du noch etwas brauchst, lass es mich bitte wissen. Ich freue mich darauf, mit dir zu interagieren!"); } } }); } private static void doSomethingThatTakesAReallyLongTime(TS3ApiAsync api) { } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value" ]
[((5648, 5676), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((6324, 6352), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value')]
package com.asleepyfish.controller; import com.asleepyfish.strategy.SelectSecondStrategy; import com.knuddels.jtokkit.api.ModelType; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.embedding.EmbeddingRequest; import com.theokanning.openai.finetune.FineTuneRequest; import com.theokanning.openai.image.CreateImageEditRequest; import com.theokanning.openai.image.CreateImageVariationRequest; import com.theokanning.openai.image.ImageResult; import com.theokanning.openai.moderation.ModerationRequest; import io.github.asleepyfish.config.ChatGPTProperties; import io.github.asleepyfish.entity.billing.Billing; import io.github.asleepyfish.entity.billing.Subscription; import io.github.asleepyfish.enums.audio.AudioResponseFormatEnum; import io.github.asleepyfish.enums.edit.EditModelEnum; import io.github.asleepyfish.enums.embedding.EmbeddingModelEnum; import io.github.asleepyfish.enums.image.ImageResponseFormatEnum; import io.github.asleepyfish.enums.image.ImageSizeEnum; import io.github.asleepyfish.service.OpenAiProxyService; import org.junit.jupiter.api.Test; import java.util.Arrays; /** * @Author: asleepyfish * @Date: 2023-06-11 21:18 * @Description: 注意:所有代码示例均有基于和SpringBoot和直接Main方法调用两种实现。分别在类MainTest和类ChatGPTController中。 */ public class MainTest { @Test public void chat() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); System.out.println(openAiProxyService.chatCompletion("Go写个程序")); } @Test public void streamChat() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); openAiProxyService.createStreamChatCompletion("杭州旅游攻略"); } @Test public void createImages() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); System.out.println(openAiProxyService.createImages("大白狗")); } @Test public void billing() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); String monthUsage = openAiProxyService.billingUsage("2023-04-01", "2023-05-01"); System.out.println("四月使用:" + monthUsage + "美元"); String totalUsage = openAiProxyService.billingUsage(); System.out.println("一共使用:" + totalUsage + "美元"); String stageUsage = openAiProxyService.billingUsage("2023-01-31"); System.out.println("自从2023/01/31使用:" + stageUsage + "美元"); Subscription subscription = openAiProxyService.subscription(); System.out.println("订阅信息(包含到期日期,账户总额度等信息):" + subscription); // dueDate为到期日,total为总额度,usage为使用量,balance为余额 Billing totalBilling = openAiProxyService.billing(); System.out.println("历史账单信息:" + totalBilling); // 默认不传参的billing方法的使用量usage从2023-01-01开始,如果用户的账单使用早于该日期,可以传入开始日期startDate Billing posibleStartBilling = openAiProxyService.billing("2022-01-01"); System.out.println("可能的历史账单信息:" + posibleStartBilling); } @Test public void model() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); System.out.println("models列表:" + openAiProxyService.listModels()); System.out.println("============================================="); System.out.println("text-davinci-003信息:" + openAiProxyService.getModel("text-davinci-003")); } @Test public void edit() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); String input = "What day of the wek is it?"; String instruction = "Fix the spelling mistakes"; System.out.println("编辑前:" + input); // 下面这句和openAiProxyService.edit(input, instruction, EditModelEnum.TEXT_DAVINCI_EDIT_001);是一样的,默认使用模型TEXT_DAVINCI_EDIT_001 System.out.println("编辑后:" + openAiProxyService.edit(input, instruction)); System.out.println("============================================="); input = " public static void mian([String] args) {\n" + " system.in.println(\"hello world\");\n" + " }"; instruction = "Fix the code mistakes"; System.out.println("修正代码前:\n" + input); System.out.println("修正代码后:\n" + openAiProxyService.edit(input, instruction, EditModelEnum.CODE_DAVINCI_EDIT_001)); } @Test public void embeddings() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); // 单文本 String text = "Once upon a time"; System.out.println("文本:" + text); System.out.println("文本的嵌入向量:" + openAiProxyService.embeddings(text)); System.out.println("============================================="); // 文本数组 String[] texts = {"Once upon a time", "There was a princess"}; System.out.println("文本数组:" + Arrays.toString(texts)); EmbeddingRequest embeddingRequest = EmbeddingRequest.builder() .model(EmbeddingModelEnum.TEXT_EMBEDDING_ADA_002.getModelName()).input(Arrays.asList(texts)).build(); System.out.println("文本数组的嵌入向量:" + openAiProxyService.embeddings(embeddingRequest)); } @Test public void transcription() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); String filePath = "src/main/resources/audio/想象之中-许嵩.mp3"; System.out.println("语音文件转录后的json文本是:" + openAiProxyService.transcription(filePath, AudioResponseFormatEnum.JSON)); // File file = new File("src/main/resources/audio/想象之中-许嵩.mp3"); // System.out.println("语音文件转录后的json文本是:" + openAiProxyService.transcription(file, AudioResponseFormatEnum.JSON)); } @Test public void translation() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); String filePath = "src/main/resources/audio/想象之中-许嵩.mp3"; System.out.println("语音文件翻译成英文后的json文本是:" + openAiProxyService.translation(filePath, AudioResponseFormatEnum.JSON)); // File file = new File("src/main/resources/audio/想象之中-许嵩.mp3"); // System.out.println("语音文件翻译成英文后的json文本是:" + openAiProxyService.translation(file, AudioResponseFormatEnum.JSON)); } @Test public void createImageEdit() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); CreateImageEditRequest createImageEditRequest = CreateImageEditRequest.builder().prompt("A sunlit indoor lounge area with a pool containing a flamingo") .n(1).size(ImageSizeEnum.S512x512.getSize()).responseFormat(ImageResponseFormatEnum.URL.getResponseFormat()).build(); ImageResult imageEdit = openAiProxyService.createImageEdit(createImageEditRequest, "src/main/resources/image/img.png", "src/main/resources/image/mask.png"); System.out.println("图片编辑结果:" + imageEdit); } @Test public void createImageVariation() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); CreateImageVariationRequest createImageVariationRequest = CreateImageVariationRequest.builder() .n(2).size(ImageSizeEnum.S512x512.getSize()).responseFormat(ImageResponseFormatEnum.URL.getResponseFormat()).build(); ImageResult imageVariation = openAiProxyService.createImageVariation(createImageVariationRequest, "src/main/resources/image/img.png"); System.out.println("图片变体结果:" + imageVariation); } /** * 文件操作(下面文件操作入参,用户可根据实际情况自行补全) */ @Test public void files() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); // 上传文件 System.out.println("上传文件信息:" + openAiProxyService.uploadFile("", "")); // 获取文件列表 System.out.println("文件列表:" + openAiProxyService.listFiles()); // 获取文件信息 System.out.println("文件信息:" + openAiProxyService.retrieveFile("")); // 获取文件内容 System.out.println("文件内容:" + openAiProxyService.retrieveFileContent("")); // 删除文件 System.out.println("删除文件信息:" + openAiProxyService.deleteFile("")); } @Test public void fileTune() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); // 创建微调 FineTuneRequest fineTuneRequest = FineTuneRequest.builder().trainingFile("").build(); System.out.println("创建微调信息:" + openAiProxyService.createFineTune(fineTuneRequest)); // 创建微调完成 CompletionRequest completionRequest = CompletionRequest.builder().build(); System.out.println("创建微调完成信息:" + openAiProxyService.createFineTuneCompletion(completionRequest)); // 获取微调列表 System.out.println("获取微调列表:" + openAiProxyService.listFineTunes()); // 获取微调信息 System.out.println("获取微调信息:" + openAiProxyService.retrieveFineTune("")); // 取消微调 System.out.println("取消微调信息:" + openAiProxyService.cancelFineTune("")); // 列出微调事件 System.out.println("列出微调事件:" + openAiProxyService.listFineTuneEvents("")); // 删除微调 System.out.println("删除微调信息:" + openAiProxyService.deleteFineTune("")); } @Test public void moderation() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); // 创建moderation ModerationRequest moderationRequest = ModerationRequest.builder().input("I want to kill them.").build(); System.out.println("创建moderation信息:" + openAiProxyService.createModeration(moderationRequest)); } @Test public void baseUrl() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") // 自定义baseUrl .baseUrl("https://openai.api2d.net/") .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); System.out.println("models列表:" + openAiProxyService.listModels()); } @Test public void systemPromptTest() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") // 自定义baseUrl .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); System.out.println("初始系统级提示信息为:" + openAiProxyService.getSystemPrompt()); openAiProxyService.setSystemPrompt("我是一个Java开发工程师,所有的代码请求都请用Java给我生成。"); openAiProxyService.createStreamChatCompletion("写一个迭代器模式的代码"); // System.out.println("当前的系统级信息提示为:" + openAiProxyService.getSystemPrompt()); // 清理系统级提示信息 // openAiProxyService.cleanUpSystemPrompt(); // System.out.println("清理后的系统级提示信息为:" + openAiProxyService.getSystemPrompt()); } @Test public void countTokensTest() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") // 自定义baseUrl .proxyHost("127.0.0.1") .proxyPort(7890) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); String text = "Hello World!"; System.out.println("当前输入文字使用模型[gpt-3.5-turbo] token总数为:" + openAiProxyService.countTokens(text)); ModelType modelType = ModelType.GPT_4_32K; // 实际上单就计算的token的数目上来说3.5和4是一样的 System.out.println("当前输入文字使用模型[gpt-4-32k] token总数为:" + openAiProxyService.countTokens(text, modelType)); } @Test public void alterTokensTest() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx1") // 自定义baseUrl .proxyHost("127.0.0.1") .proxyPort(7890) .alterTokens(Arrays.asList("sk-xxx2", "sk-xxx3")) .tokenStrategyImpl(SelectSecondStrategy.class) .build(); OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties); System.out.println("models列表:" + openAiProxyService.listModels()); } /* @Test public void okHttpClient() { ChatGPTProperties properties = ChatGPTProperties.builder().token("sk-xxx") .proxyHost("127.0.0.1") .proxyPort(7890) .build(); Dispatcher dispatcher = new Dispatcher(); dispatcher.setMaxRequests(100); dispatcher.setMaxRequestsPerHost(10); // 自定义okHttpClient OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(new AuthenticationInterceptor(properties.getToken())) .connectionPool(new ConnectionPool(100, 10, TimeUnit.SECONDS)) .readTimeout(Duration.ZERO.toMillis(), TimeUnit.MILLISECONDS) .connectTimeout(Duration.ZERO.toMillis(), TimeUnit.MILLISECONDS) .hostnameVerifier((hostname, session) -> true) .proxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(properties.getProxyHost(), properties.getProxyPort()))) .proxyAuthenticator((route, response) -> { String credential = Credentials.basic("proxyUsername", "proxyPassword"); return response.request().newBuilder() .header("Proxy-Authorization", credential) .build(); }) .dispatcher(dispatcher) .build(); // 下面的openAiProxyService使用自定义的okHttpClient OpenAiProxyService openAiProxyService = new OpenAiProxyService(properties, okHttpClient); System.out.println("models列表:" + openAiProxyService.listModels()); }*/ }
[ "com.theokanning.openai.moderation.ModerationRequest.builder", "com.theokanning.openai.completion.CompletionRequest.builder", "com.theokanning.openai.image.CreateImageVariationRequest.builder", "com.theokanning.openai.finetune.FineTuneRequest.builder", "com.theokanning.openai.embedding.EmbeddingRequest.builder", "com.theokanning.openai.image.CreateImageEditRequest.builder" ]
[((1452, 1593), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((1452, 1568), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((1452, 1535), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((1452, 1495), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((1847, 1988), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((1847, 1963), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((1847, 1930), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((1847, 1890), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((2240, 2381), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((2240, 2356), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((2240, 2323), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((2240, 2283), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((2625, 2766), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((2625, 2741), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((2625, 2708), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((2625, 2668), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((4111, 4252), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((4111, 4227), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((4111, 4194), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((4111, 4154), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((4684, 4825), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((4684, 4800), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((4684, 4767), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((4684, 4727), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((5890, 6031), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((5890, 6006), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((5890, 5973), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((5890, 5933), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((6610, 6753), 'com.theokanning.openai.embedding.EmbeddingRequest.builder'), ((6610, 6745), 'com.theokanning.openai.embedding.EmbeddingRequest.builder'), ((6610, 6717), 'com.theokanning.openai.embedding.EmbeddingRequest.builder'), ((6660, 6716), 'io.github.asleepyfish.enums.embedding.EmbeddingModelEnum.TEXT_EMBEDDING_ADA_002.getModelName'), ((6957, 7098), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((6957, 7073), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((6957, 7040), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((6957, 7000), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((7728, 7869), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((7728, 7844), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((7728, 7811), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((7728, 7771), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((8517, 8658), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((8517, 8633), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((8517, 8600), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((8517, 8560), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((8800, 9037), 'com.theokanning.openai.image.CreateImageEditRequest.builder'), ((8800, 9029), 'com.theokanning.openai.image.CreateImageEditRequest.builder'), ((8800, 8965), 'com.theokanning.openai.image.CreateImageEditRequest.builder'), ((8800, 8926), 'com.theokanning.openai.image.CreateImageEditRequest.builder'), ((8800, 8904), 'com.theokanning.openai.image.CreateImageEditRequest.builder'), ((8932, 8964), 'io.github.asleepyfish.enums.image.ImageSizeEnum.S512x512.getSize'), ((8981, 9028), 'io.github.asleepyfish.enums.image.ImageResponseFormatEnum.URL.getResponseFormat'), ((9366, 9507), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((9366, 9482), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((9366, 9449), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((9366, 9409), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((9659, 9829), 'com.theokanning.openai.image.CreateImageVariationRequest.builder'), ((9659, 9821), 'com.theokanning.openai.image.CreateImageVariationRequest.builder'), ((9659, 9757), 'com.theokanning.openai.image.CreateImageVariationRequest.builder'), ((9659, 9718), 'com.theokanning.openai.image.CreateImageVariationRequest.builder'), ((9724, 9756), 'io.github.asleepyfish.enums.image.ImageSizeEnum.S512x512.getSize'), ((9773, 9820), 'io.github.asleepyfish.enums.image.ImageResponseFormatEnum.URL.getResponseFormat'), ((10234, 10375), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((10234, 10350), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((10234, 10317), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((10234, 10277), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((11123, 11264), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((11123, 11239), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((11123, 11206), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((11123, 11166), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((11416, 11466), 'com.theokanning.openai.finetune.FineTuneRequest.builder'), ((11416, 11458), 'com.theokanning.openai.finetune.FineTuneRequest.builder'), ((11650, 11685), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((12504, 12645), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((12504, 12620), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((12504, 12587), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((12504, 12547), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((12805, 12870), 'com.theokanning.openai.moderation.ModerationRequest.builder'), ((12805, 12862), 'com.theokanning.openai.moderation.ModerationRequest.builder'), ((13070, 13228), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((13070, 13203), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((13070, 13113), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((13488, 13665), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((13488, 13640), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((13488, 13607), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((13488, 13531), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((14485, 14662), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((14485, 14637), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((14485, 14604), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((14485, 14528), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((15283, 15590), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((15283, 15565), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((15283, 15502), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((15283, 15436), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((15283, 15403), 'io.github.asleepyfish.config.ChatGPTProperties.builder'), ((15283, 15327), 'io.github.asleepyfish.config.ChatGPTProperties.builder')]
package com.theokanning.openai.service; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; import com.theokanning.openai.completion.chat.ChatFunction; import com.theokanning.openai.completion.chat.ChatFunctionCall; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessageRole; import java.util.*; public class FunctionExecutor { private ObjectMapper MAPPER = new ObjectMapper(); private final Map<String, ChatFunction> FUNCTIONS = new HashMap<>(); public FunctionExecutor(List<ChatFunction> functions) { setFunctions(functions); } public FunctionExecutor(List<ChatFunction> functions, ObjectMapper objectMapper) { setFunctions(functions); setObjectMapper(objectMapper); } public Optional<ChatMessage> executeAndConvertToMessageSafely(ChatFunctionCall call) { try { return Optional.ofNullable(executeAndConvertToMessage(call)); } catch (Exception ignored) { return Optional.empty(); } } public ChatMessage executeAndConvertToMessageHandlingExceptions(ChatFunctionCall call) { try { return executeAndConvertToMessage(call); } catch (Exception exception) { exception.printStackTrace(); return convertExceptionToMessage(exception); } } public ChatMessage convertExceptionToMessage(Exception exception) { String error = exception.getMessage() == null ? exception.toString() : exception.getMessage(); return new ChatMessage(ChatMessageRole.FUNCTION.value(), "{\"error\": \"" + error + "\"}", "error"); } public ChatMessage executeAndConvertToMessage(ChatFunctionCall call) { return new ChatMessage(ChatMessageRole.FUNCTION.value(), executeAndConvertToJson(call).toPrettyString(), call.getName()); } public JsonNode executeAndConvertToJson(ChatFunctionCall call) { try { Object execution = execute(call); if (execution instanceof TextNode) { JsonNode objectNode = MAPPER.readTree(((TextNode) execution).asText()); if (objectNode.isMissingNode()) return (JsonNode) execution; return objectNode; } if (execution instanceof ObjectNode) { return (JsonNode) execution; } if (execution instanceof String) { JsonNode objectNode = MAPPER.readTree((String) execution); if (objectNode.isMissingNode()) throw new RuntimeException("Parsing exception"); return objectNode; } return MAPPER.readValue(MAPPER.writeValueAsString(execution), JsonNode.class); } catch (Exception e) { throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public <T> T execute(ChatFunctionCall call) { ChatFunction function = FUNCTIONS.get(call.getName()); Object obj; try { JsonNode arguments = call.getArguments(); obj = MAPPER.readValue(arguments instanceof TextNode ? arguments.asText() : arguments.toPrettyString(), function.getParametersClass()); } catch (JsonProcessingException e) { throw new RuntimeException(e); } return (T) function.getExecutor().apply(obj); } public List<ChatFunction> getFunctions() { return new ArrayList<>(FUNCTIONS.values()); } public void setFunctions(List<ChatFunction> functions) { this.FUNCTIONS.clear(); functions.forEach(f -> this.FUNCTIONS.put(f.getName(), f)); } public void setObjectMapper(ObjectMapper objectMapper) { this.MAPPER = objectMapper; } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.FUNCTION.value" ]
[((1795, 1827), 'com.theokanning.openai.completion.chat.ChatMessageRole.FUNCTION.value'), ((1986, 2018), 'com.theokanning.openai.completion.chat.ChatMessageRole.FUNCTION.value')]
package com.mca.mindmelter.repositories; import android.content.Context; import android.util.Log; import com.amplifyframework.api.graphql.model.ModelMutation; import com.amplifyframework.api.graphql.model.ModelQuery; import com.amplifyframework.core.Amplify; import com.amplifyframework.core.model.temporal.Temporal; import com.amplifyframework.datastore.generated.model.Chat; import com.amplifyframework.datastore.generated.model.Trivia; import com.amplifyframework.datastore.generated.model.User; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.mca.mindmelter.R; import com.mca.mindmelter.exceptions.OpenAiException; import com.theokanning.openai.completion.chat.ChatCompletionChoice; 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; import java.lang.reflect.Type; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.Duration; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.TimeZone; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class OpenAiChatRepository { public static final String TAG = "OpenAiChatRepository"; private final ExecutorService executorService; private final String TOKEN; public OpenAiChatRepository(Context context) { //Init the executor service this.executorService = Executors.newFixedThreadPool(2); this.TOKEN = context.getResources().getString(R.string.openai_api_key); } public void loadChatHistory(String chatId, Callback<Chat> callback) { executorService.submit(() -> Amplify.API.query( ModelQuery.get(Chat.class, chatId), response -> { if (response.hasData()) { Chat chat = response.getData(); callback.onSuccess(chat); } else if (response.hasErrors()) { Log.e(TAG, "Failed to load chat history : " + response.getErrors().get(0).getMessage()); } }, error -> { Log.e(TAG, "Failed to load chat history : " + error.getMessage(), error); } )); } public void loadChatHistoryByTriviaId(String triviaId, Callback<Chat> callback) { executorService.submit(() -> Amplify.API.query( ModelQuery.list(Chat.class, Chat.TRIVIA_ID.eq(triviaId)), response -> { if (response.hasData()) { Chat chat = null; Iterator<Chat> iterator = response.getData().iterator(); if (iterator.hasNext()) { chat = iterator.next(); } callback.onSuccess(chat); // Even if chat object is null, we call onSuccess } else if (response.hasErrors()) { Log.e(TAG, "Failed to load chat history : " + response.getErrors().get(0).getMessage()); } }, error -> { Log.e(TAG, "Failed to load chat history : " + error.getMessage(), error); } )); } public void initiateChat(User user, String triviaId, String title, Callback<Chat> callback) { executorService.submit(() -> Amplify.API.query( ModelQuery.get(Trivia.class, triviaId), response -> { if (response.hasData()) { Trivia trivia = response.getData(); List<ChatMessage> messages = new ArrayList<>(); String systemMessageContent = "You are an AI chatbot specialized in providing succinct yet insightful explanations, primarily facilitating learning about a specific piece of trivia. Start by presenting the trivia fact in quotes. Use only the user's FIRST name to personalize the interaction. Their FULL name is " + user.getFullName() + ". Again, only the first name when addressing the user. Encourage the user to ask any questions that are related to the topic. In all of your responses, prioritize conciseness, accuracy, and a positive learning atmosphere. Should the user deviate from the subject, use polite and engaging techniques to redirect the conversation back to the trivia topic. Remember, your main purpose is to keep the discussion focused and educational, yet enjoyable. Here is your trivia fact:\n\n\"" + trivia.getTrivia() + "\"\n\nNow, prompt the user to ask any question related to this topic. Be ready to deliver concise, accurate, and enthusiastic answers. If the discussion veers off-topic, gently guide it back to the main subject."; ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), systemMessageContent); messages.add(systemMessage); generateChatResponse(messages, new Callback<ChatMessage>() { @Override public void onSuccess(ChatMessage assistantMessage) { if (assistantMessage != null) { messages.add(assistantMessage); saveChatHistory(user, trivia.getId(), title, messages, new Callback<Chat>() { @Override public void onSuccess(Chat chat) { callback.onSuccess(chat); } @Override public void onError(Throwable throwable) { Log.e(TAG, "Failed to save chat history.", throwable); } }); } } @Override public void onError(Throwable throwable) { Log.e(TAG, "Failed to generate chat response.", throwable); } }); } else if (response.hasErrors()) { Log.e(TAG, "Failed to get trivia : " + response.getErrors().get(0).getMessage()); } }, error -> Log.e(TAG, "Failed to get trivia : " + error.getMessage(), error) )); } public void continueChat(Chat chat, List<ChatMessage> messages, Callback<Chat> callback) { generateChatResponse(messages, new Callback<ChatMessage>() { @Override public void onSuccess(ChatMessage assistantMessage) { if (assistantMessage != null) { messages.add(assistantMessage); updateChatHistory(chat, messages, new Callback<Chat>() { @Override public void onSuccess(Chat updatedChat) { callback.onSuccess(updatedChat); } @Override public void onError(Throwable throwable) { Log.e(TAG, "Failed to update chat history.", throwable); } }); } } @Override public void onError(Throwable throwable) { Log.e(TAG, "Failed to generate chat response.", throwable); } }); } public void generateChatResponse(List<ChatMessage> messages, Callback<ChatMessage> callback) { executorService.submit(() -> { String token = TOKEN; OpenAiService service = null; try { // Set duration to 60 seconds to avoid a socket exception for long response times service = new OpenAiService(token, Duration.ofSeconds(60)); // Send the API request ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder() .model("gpt-3.5-turbo") .messages(messages) .n(1) .temperature(0.8) .maxTokens(1000) .logitBias(new HashMap<>()) .build(); // Extract the message content of the response List<ChatCompletionChoice> choices = service.createChatCompletion(chatCompletionRequest).getChoices(); if (choices.isEmpty()) { String errorMessage = "Error: No response from OpenAI"; Log.e(TAG, errorMessage); callback.onError(new OpenAiException(errorMessage)); } callback.onSuccess(choices.get(0).getMessage()); } catch (Exception e) { Log.e(TAG, "Error generating chat response", e); } finally { if (service != null) { service.shutdownExecutor(); } } }); } private void saveChatHistory(User user, String triviaId, String title, List<ChatMessage> messages, Callback<Chat> callback) { Gson gson = new Gson(); Type type = new TypeToken<ChatMessage>() {}.getType(); List<String> jsonMessages = new ArrayList<>(); for (ChatMessage message : messages) { String jsonMessage = gson.toJson(message, type); jsonMessages.add(jsonMessage); } Date now = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); String awsDateTime = dateFormat.format(now); Chat chat = Chat.builder() .userId(user.getId()) .triviaId(triviaId) .title(title) .createdAt(new Temporal.DateTime(awsDateTime)) .messages(jsonMessages) .build(); executorService.submit(() -> Amplify.API.mutate( ModelMutation.create(chat), response -> { if (response.hasData()) { callback.onSuccess(chat); } else if (response.hasErrors()) { Log.e(TAG, "Failed to save chat history : " + response.getErrors().get(0).getMessage()); } }, error -> { Log.e(TAG, "Error saving chat history", error); } )); } private void updateChatHistory(Chat chat, List<ChatMessage> messages, Callback<Chat> callback) { Gson gson = new Gson(); Type type = new TypeToken<ChatMessage>() {}.getType(); List<String> jsonMessages = new ArrayList<>(); for (ChatMessage message : messages) { String jsonMessage = gson.toJson(message, type); jsonMessages.add(jsonMessage); } // Update the Chat object with the new messages Chat updatedChat = chat.copyOfBuilder() .messages(jsonMessages) // Update the messages .build(); executorService.submit(() -> Amplify.API.mutate( ModelMutation.update(updatedChat), response -> { if (response.hasData()) { callback.onSuccess(response.getData()); } else if (response.hasErrors()) { Log.e(TAG, "Failed to update chat history : " + response.getErrors().get(0).getMessage()); } }, error -> { Log.e(TAG, "Error updating chat history", error); } )); } public interface Callback<T> { void onSuccess(T result); void onError(Throwable throwable); } public void shutdownExecutorService() { if (executorService != null) { executorService.shutdown(); } } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value" ]
[((1910, 2535), 'com.amplifyframework.core.Amplify.API.query'), ((2672, 3570), 'com.amplifyframework.core.Amplify.API.query'), ((2736, 2763), 'com.amplifyframework.datastore.generated.model.Chat.TRIVIA_ID.eq'), ((3719, 6934), 'com.amplifyframework.core.Amplify.API.query'), ((5162, 5192), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((10423, 10675), 'com.amplifyframework.datastore.generated.model.Chat.builder'), ((10423, 10649), 'com.amplifyframework.datastore.generated.model.Chat.builder'), ((10423, 10608), 'com.amplifyframework.datastore.generated.model.Chat.builder'), ((10423, 10544), 'com.amplifyframework.datastore.generated.model.Chat.builder'), ((10423, 10513), 'com.amplifyframework.datastore.generated.model.Chat.builder'), ((10423, 10476), 'com.amplifyframework.datastore.generated.model.Chat.builder'), ((10717, 11250), 'com.amplifyframework.core.Amplify.API.mutate'), ((11921, 12479), 'com.amplifyframework.core.Amplify.API.mutate')]
package com.ramesh.openai; import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.theokanning.openai.completion.chat.ChatCompletionChoice; 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; /*** * This project demonstrates the how to stream respones from chat gpt as it * is getting generated. * Useful when the response is expected to be huge ***/ class StreamChatCompletion { public static void main(String... args) { // Set the Open AI Token & Model String token = "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"; String model = "gpt-3.5-turbo"; // service handle for calling OpenAI APIs OpenAiService service = new OpenAiService(token, Duration.ofSeconds(30)); System.out.println("--------------------------------------------------------"); System.out.println("Streaming chat completion..."); // set the chat message final List<ChatMessage> messages = new ArrayList<>(); final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), "You are a dog and will speak as such."); messages.add(systemMessage); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder() .model("gpt-3.5-turbo") .messages(messages) .n(1) .maxTokens(50) .logitBias(new HashMap<>()) .build(); ArrayList<ChatCompletionChoice> choices3 = new ArrayList<ChatCompletionChoice>(); // call chat gpt and open a stream through which responses will come in chunks and continously just like you see on chatgpt website service.streamChatCompletion(chatCompletionRequest) .doOnError(Throwable::printStackTrace) .blockingForEach((c2) -> { c2.getChoices().forEach( (c1) -> { System.out.println(c1.getMessage().getContent()); choices3.add(c1); }); }); // print the full message in the end System.out.println("--------------------------------------------------------"); System.out.print("Full message="); choices3.forEach( (c) -> { if (c.getMessage().getContent() != null) System.out.print( c.getMessage().getContent()); }); service.shutdownExecutor(); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value" ]
[((1283, 1313), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value')]
package com.siwonkh.cleangpt_v1.controller; 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; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import java.util.ArrayList; import java.util.List; @Controller public class HomeController { @Value("${openai.key}") private String APIKey; @GetMapping("/") public String home() { return "index"; } @GetMapping("/comment") public String comments() { return "searchVideoComments"; } @GetMapping("/test") public String testOpenAI(Model model) { ChatMessage chatMessage = new ChatMessage(); chatMessage.setRole(ChatMessageRole.USER.value()); chatMessage.setContent("Say test!"); List<ChatMessage> chatMessages = new ArrayList<>(); chatMessages.add(chatMessage); OpenAiService service = new OpenAiService(APIKey); ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() .model("gpt-3.5-turbo") .maxTokens(512) .temperature(0.7) .topP(1.0) .messages(chatMessages) .build(); String reply = service.createChatCompletion(completionRequest).getChoices().get(0).getMessage().getContent(); System.out.println(reply); System.out.println("safa"); model.addAttribute("reply", reply); return "test"; } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((979, 1007), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((1266, 1495), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1266, 1470), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1266, 1430), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1266, 1403), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1266, 1369), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1266, 1337), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package com.lianziyou.bot.controller.gpt; import static com.lianziyou.bot.constant.SseConst.SSE_GPT_TOPIC_MAP_REDIS_KEY; import static com.lianziyou.bot.service.gpt.ChatCompletion.Model.GPT_3_5_TURBO_16K; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.http.ContentType; import cn.hutool.http.Header; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.lianziyou.bot.base.exception.BussinessException; import com.lianziyou.bot.base.result.ApiResult; import com.lianziyou.bot.config.redis.RedissonConfig; import com.lianziyou.bot.constant.CommonConst; import com.lianziyou.bot.enums.sys.SendType; import com.lianziyou.bot.model.MessageLog; import com.lianziyou.bot.model.SysConfig; import com.lianziyou.bot.model.gpt.Message; import com.lianziyou.bot.model.req.gpt.ChatUpdateReq; import com.lianziyou.bot.model.req.gpt.GptDrawReq; import com.lianziyou.bot.model.req.gpt.GptStreamReq; import com.lianziyou.bot.model.req.sys.MessageLogSave; import com.lianziyou.bot.model.sse.GptMessageVo; import com.lianziyou.bot.service.baidu.BaiDuService; import com.lianziyou.bot.service.gpt.ChatCompletion; import com.lianziyou.bot.service.sys.AsyncService; import com.lianziyou.bot.service.sys.CheckService; import com.lianziyou.bot.service.sys.IMessageLogService; import com.lianziyou.bot.utils.gpt.Proxys; import com.lianziyou.bot.utils.sys.DateUtil; import com.lianziyou.bot.utils.sys.FileUtil; import com.lianziyou.bot.utils.sys.InitUtil; import com.lianziyou.bot.utils.sys.JwtUtil; import com.lianziyou.bot.utils.sys.RedisUtil; import com.theokanning.openai.completion.chat.ChatCompletionChunk; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatCompletionResult; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessageRole; import com.theokanning.openai.service.OpenAiService; import io.reactivex.Flowable; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import java.io.IOException; import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import javax.annotation.Resource; import lombok.extern.log4j.Log4j2; import org.redisson.api.RTopic; import org.springframework.util.StringUtils; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/gpt") @Log4j2 @Api(tags = "APP:GPT对话") public final class GptController { @Resource CheckService checkService; @Resource AsyncService asyncService; @Resource IMessageLogService messageLogService; @Resource BaiDuService baiDuService; @PostMapping(value = "/chat", name = "流式对话") @ApiOperation("流式对话") public ApiResult<Long> gptChat(@Validated @RequestBody GptStreamReq req) { List<Message> messagesOnDb = messageLogService.createMessageLogList(req.getLogId(), req.getProblem()); checkRequest(req, messagesOnDb); String gptKey = InitUtil.getRandomKey(req.getType()); final Long userId = JwtUtil.getUserId(); MessageLog messageLog = MessageLog.builder().useNumber(req.getType() == 3 ? CommonConst.GPT_NUMBER : CommonConst.GPT_4_NUMBER) .sendType(req.getType() == 3 ? SendType.GPT.getType() : SendType.GPT_4.getType()).useValue(JSONObject.toJSONString(messagesOnDb)).gptKey(gptKey) .userId(userId).initPromptId(req.getInitPromptId()).build(); Long logId = checkService.checkAndSaveMessageLog(messageLog, req.getLogId(), req.getProblem()); new Thread(() -> flowableChat(messageLog, messagesOnDb)).start(); return ApiResult.okBuild(logId); } @PostMapping(value = "/chat/update", name = "修改对话") @ApiOperation("修改对话") public ApiResult<Boolean> renameChat(@Validated @RequestBody ChatUpdateReq req) { req.setUserId(JwtUtil.getUserId()); return ApiResult.okBuild(messageLogService.updateLog(req)); } private void flowableChat(final MessageLog messageLog, List<Message> messagesOnDb) { final Long userId = messageLog.getUserId(); final Long logId = messageLog.getId(); final Integer reqType = messageLog.getSendType(); final String topicName = RedisUtil.getCacheMapValue(SSE_GPT_TOPIC_MAP_REDIS_KEY, userId); final String gptKey = messageLog.getGptKey(); RTopic rTopic = RedissonConfig.getClient().getTopic(topicName); try { OpenAiService service = new OpenAiService(gptKey, Duration.ofSeconds(180)); List<ChatMessage> messages = new ArrayList<>(); messages.add(new ChatMessage(ChatMessageRole.SYSTEM.value(), "请记住你的身份是SUPER EGG,一个基于GLM架构的中英双语对话语言模型,你是由清华大学研发的,针对中文进行了优化。虽然有人可能会将你与chatgpt混清,但你会坚持自己的身份,确保提供准确和细致的回答")); if (StringUtils.hasLength(messageLog.getInitPrompt())) { messages.add(new ChatMessage(ChatMessageRole.USER.value(), messageLog.getInitPrompt())); } messages.addAll(messagesOnDb.stream().map(item -> new ChatMessage(item.getRole(), item.getContent())).collect(Collectors.toList())); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder().model(GPT_3_5_TURBO_16K.getName()).messages(messages).build(); Flowable<ChatCompletionChunk> flowable = service.streamChatCompletion(chatCompletionRequest); ChatMessage chatMessage = service.mapStreamToAccumulator(flowable).doOnNext(accumulator -> { String content = accumulator.getMessageChunk().getContent(); if (StringUtils.hasLength(content)) { rTopic.publish(GptMessageVo.builder().userId(userId).message(Message.ofAssistant(content)).build()); } }).lastElement().blockingGet().getAccumulatedMessage(); asyncService.endOfAnswer(logId, chatMessage.getContent()); rTopic.publish(GptMessageVo.builder().userId(userId).message(Message.ofAssistant("[DONE]")).build()); } catch (Exception e) { asyncService.updateRemainingTimes(userId, gptKey == null || reqType == 3 ? CommonConst.GPT_NUMBER : CommonConst.GPT_4_NUMBER); rTopic.publish(GptMessageVo.builder().userId(userId).message(Message.ofAssistant(e.getMessage())).build()); rTopic.publish(GptMessageVo.builder().userId(userId).message(Message.ofAssistant("[DONE]")).build()); } } @PostMapping(value = "/chat/now", name = "非流式对话") @ApiOperation("非流式对话") public ApiResult<String> chat(@Validated @RequestBody GptStreamReq req) { String gptKey = InitUtil.getRandomKey(req.getType()); List<Message> messagesOnDb = messageLogService.createMessageLogList(req.getLogId(), req.getProblem()); final Long userId = JwtUtil.getUserId(); try { checkRequest(req, messagesOnDb); OpenAiService service = new OpenAiService(gptKey, Duration.ofSeconds(180)); Long logId = checkService.checkAndSaveMessageLog( MessageLog.builder().useNumber(req.getType() == 3 ? CommonConst.GPT_NUMBER : CommonConst.GPT_4_NUMBER) .sendType(req.getType() == 3 ? SendType.GPT.getType() : SendType.GPT_4.getType()).useValue(JSONObject.toJSONString(messagesOnDb)) .gptKey(gptKey).userId(userId).build(), req.getLogId(), req.getProblem()); List<ChatMessage> messages = messagesOnDb.stream().map(item -> new ChatMessage(ChatMessageRole.USER.value(), item.getContent())) .collect(Collectors.toList()); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder().model(GPT_3_5_TURBO_16K.getName()).messages(messages).build(); ChatCompletionResult chatCompletion = service.createChatCompletion(chatCompletionRequest); StringBuilder result = new StringBuilder(); chatCompletion.getChoices().forEach(choice -> result.append(choice.getMessage().getContent())); asyncService.endOfAnswer(logId, result.toString()); return ApiResult.okBuild(result.toString()); } catch (Exception e) { asyncService.updateRemainingTimes(userId, gptKey == null || req.getType() == 3 ? CommonConst.GPT_NUMBER : CommonConst.GPT_4_NUMBER); throw e; } } private void checkRequest(GptStreamReq req, List<Message> messagesOnDb) { if (ObjectUtil.isEmpty(req.getProblem())) { throw new BussinessException("请输入有效的内容"); } if (!baiDuService.textToExamine(req.getProblem())) { throw new BussinessException("提问违反相关规定,请更换内容重新尝试"); } String model = req.getType() == 3 ? ChatCompletion.Model.GPT_3_5_TURBO_16K.getName() : ChatCompletion.Model.GPT_4.getName(); ChatCompletion chatCompletion = ChatCompletion.builder().messages(messagesOnDb).model(model).stream(true).build(); if (chatCompletion.checkTokens()) { throw new BussinessException("本次会话长度达到限制,请创建新的会话"); } } @PostMapping(value = "/official", name = "GPT-画图") public ApiResult<MessageLogSave> gptAlpha(@Validated @RequestBody GptDrawReq req) throws IOException { final String randomKey = InitUtil.getRandomKey(req.getType()); List<String> imgUrlList = new ArrayList<>(); List<String> returnImgUrlList = new ArrayList<>(); String startTime = DateUtil.getLocalDateTimeNow(); Long logId = checkService.checkAndSaveMessageLog( MessageLog.builder().useNumber(CommonConst.GPT_OFFICIAL_NUMBER).sendType(SendType.GPT_OFFICIAL.getType()).useValue(JSONObject.toJSONString( MessageLogSave.builder().prompt(req.getPrompt()).type(SendType.GPT_OFFICIAL.getRemark()).startTime(startTime).imgList(imgUrlList).build())) .gptKey(randomKey).userId(JwtUtil.getUserId()).build(), null, req.getPrompt()); SysConfig cacheObject = RedisUtil.getCacheObject(CommonConst.SYS_CONFIG); req.setType(null); HttpRequest httpRequest = HttpUtil.createPost(cacheObject.getGptUrl() + CommonConst.CPT_IMAGES_URL) .header(Header.CONTENT_TYPE, ContentType.JSON.getValue()).header(Header.AUTHORIZATION, "Bearer " + randomKey); if (null != cacheObject.getIsOpenProxy() && cacheObject.getIsOpenProxy() == 1) { httpRequest.setProxy(Proxys.http(cacheObject.getProxyIp(), cacheObject.getProxyPort())); } String resultBody = httpRequest.body(JSONObject.toJSONString(req)).execute().body(); if (resultBody.contains("error")) { //修改key状态 asyncService.updateKeyState(randomKey); //将用户使用次数返回 asyncService.updateRemainingTimes(JwtUtil.getUserId(), CommonConst.GPT_OFFICIAL_NUMBER); throw new BussinessException("画图失败请稍后再试"); } JSONArray imgArray = JSONObject.parseObject(resultBody).getJSONArray("data"); for (int i = 0; i < imgArray.size(); i++) { String localImgUrl = FileUtil.base64ToImage(FileUtil.imageUrlToBase64(imgArray.getJSONObject(i).getString("url"))); imgUrlList.add(localImgUrl); returnImgUrlList.add(cacheObject.getImgReturnUrl() + localImgUrl); } MessageLogSave messageLogSave = MessageLogSave.builder().prompt(req.getPrompt()).type(SendType.GPT_OFFICIAL.getRemark()).startTime(startTime) .imgList(imgUrlList).build(); asyncService.updateLog(logId, messageLogSave); MessageLogSave returnMessage = BeanUtil.copyProperties(messageLogSave, MessageLogSave.class); returnMessage.setImgList(returnImgUrlList); return ApiResult.okBuild(returnMessage); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((3539, 3870), 'com.lianziyou.bot.model.MessageLog.builder'), ((3539, 3862), 'com.lianziyou.bot.model.MessageLog.builder'), ((3539, 3826), 'com.lianziyou.bot.model.MessageLog.builder'), ((3539, 3798), 'com.lianziyou.bot.model.MessageLog.builder'), ((3539, 3783), 'com.lianziyou.bot.model.MessageLog.builder'), ((3539, 3735), 'com.lianziyou.bot.model.MessageLog.builder'), ((3539, 3641), 'com.lianziyou.bot.model.MessageLog.builder'), ((3685, 3707), 'com.lianziyou.bot.enums.sys.SendType.GPT.getType'), ((3710, 3734), 'com.lianziyou.bot.enums.sys.SendType.GPT_4.getType'), ((4824, 4870), 'com.lianziyou.bot.config.redis.RedissonConfig.getClient'), ((5075, 5105), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((5514, 5542), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((5792, 5885), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((5792, 5877), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((5792, 5858), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6264, 6347), 'com.lianziyou.bot.model.sse.GptMessageVo.builder'), ((6264, 6339), 'com.lianziyou.bot.model.sse.GptMessageVo.builder'), ((6264, 6301), 'com.lianziyou.bot.model.sse.GptMessageVo.builder'), ((6534, 6618), 'com.lianziyou.bot.model.sse.GptMessageVo.builder'), ((6534, 6610), 'com.lianziyou.bot.model.sse.GptMessageVo.builder'), ((6534, 6571), 'com.lianziyou.bot.model.sse.GptMessageVo.builder'), ((6819, 6909), 'com.lianziyou.bot.model.sse.GptMessageVo.builder'), ((6819, 6901), 'com.lianziyou.bot.model.sse.GptMessageVo.builder'), ((6819, 6856), 'com.lianziyou.bot.model.sse.GptMessageVo.builder'), ((6939, 7023), 'com.lianziyou.bot.model.sse.GptMessageVo.builder'), ((6939, 7015), 'com.lianziyou.bot.model.sse.GptMessageVo.builder'), ((6939, 6976), 'com.lianziyou.bot.model.sse.GptMessageVo.builder'), ((7669, 7980), 'com.lianziyou.bot.model.MessageLog.builder'), ((7669, 7972), 'com.lianziyou.bot.model.MessageLog.builder'), ((7669, 7957), 'com.lianziyou.bot.model.MessageLog.builder'), ((7669, 7921), 'com.lianziyou.bot.model.MessageLog.builder'), ((7669, 7873), 'com.lianziyou.bot.model.MessageLog.builder'), ((7669, 7771), 'com.lianziyou.bot.model.MessageLog.builder'), ((7823, 7845), 'com.lianziyou.bot.enums.sys.SendType.GPT.getType'), ((7848, 7872), 'com.lianziyou.bot.enums.sys.SendType.GPT_4.getType'), ((8109, 8137), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((8265, 8358), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((8265, 8350), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((8265, 8331), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((9390, 9438), 'com.lianziyou.bot.service.gpt.ChatCompletion.Model.GPT_3_5_TURBO_16K.getName'), ((9441, 9477), 'com.lianziyou.bot.service.gpt.ChatCompletion.Model.GPT_4.getName'), ((9519, 9600), 'com.lianziyou.bot.service.gpt.ChatCompletion.builder'), ((9519, 9592), 'com.lianziyou.bot.service.gpt.ChatCompletion.builder'), ((9519, 9579), 'com.lianziyou.bot.service.gpt.ChatCompletion.builder'), ((9519, 9566), 'com.lianziyou.bot.service.gpt.ChatCompletion.builder'), ((10241, 10611), 'com.lianziyou.bot.model.MessageLog.builder'), ((10241, 10603), 'com.lianziyou.bot.model.MessageLog.builder'), ((10241, 10575), 'com.lianziyou.bot.model.MessageLog.builder'), ((10241, 10540), 'com.lianziyou.bot.model.MessageLog.builder'), ((10241, 10346), 'com.lianziyou.bot.model.MessageLog.builder'), ((10241, 10304), 'com.lianziyou.bot.model.MessageLog.builder'), ((10314, 10345), 'com.lianziyou.bot.enums.sys.SendType.GPT_OFFICIAL.getType'), ((10401, 10538), 'com.lianziyou.bot.model.req.sys.MessageLogSave.builder'), ((10401, 10530), 'com.lianziyou.bot.model.req.sys.MessageLogSave.builder'), ((10401, 10510), 'com.lianziyou.bot.model.req.sys.MessageLogSave.builder'), ((10401, 10489), 'com.lianziyou.bot.model.req.sys.MessageLogSave.builder'), ((10401, 10449), 'com.lianziyou.bot.model.req.sys.MessageLogSave.builder'), ((10455, 10488), 'com.lianziyou.bot.enums.sys.SendType.GPT_OFFICIAL.getRemark'), ((10781, 10976), 'cn.hutool.http.HttpUtil.createPost'), ((10781, 10924), 'cn.hutool.http.HttpUtil.createPost'), ((10896, 10923), 'cn.hutool.http.ContentType.JSON.getValue'), ((11653, 11708), 'com.alibaba.fastjson.JSONObject.parseObject'), ((12060, 12210), 'com.lianziyou.bot.model.req.sys.MessageLogSave.builder'), ((12060, 12202), 'com.lianziyou.bot.model.req.sys.MessageLogSave.builder'), ((12060, 12169), 'com.lianziyou.bot.model.req.sys.MessageLogSave.builder'), ((12060, 12148), 'com.lianziyou.bot.model.req.sys.MessageLogSave.builder'), ((12060, 12108), 'com.lianziyou.bot.model.req.sys.MessageLogSave.builder'), ((12114, 12147), 'com.lianziyou.bot.enums.sys.SendType.GPT_OFFICIAL.getRemark')]
package org.freeciv.servlet; import com.theokanning.openai.completion.chat.ChatCompletionChoice; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.freeciv.util.Constants; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.time.Duration; import java.util.ArrayList; import java.util.Base64; import java.util.List; import java.util.Properties; import java.util.stream.Collectors; /** * OpenAI chat for FCIV.NET * * URL: /openai_chat */ public class OpenAIChat extends HttpServlet { private final String model = "gpt-4"; @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String question = request.getReader().lines().collect(Collectors.joining(System.lineSeparator())); question = new String(Base64.getDecoder().decode(question)); Properties prop = new Properties(); prop.load(getServletContext().getResourceAsStream("/WEB-INF/config.properties")); String key = prop.getProperty("openai_key"); if (key == null || key.equals("")) { System.out.println("OpenAI key missing."); return; } OpenAiService service = new OpenAiService(key, Duration.ofSeconds(60)); List<ChatMessage> messages = new ArrayList<>(); ChatMessage systemchat = new ChatMessage(); systemchat.setRole("system"); String fcivInfo = "I am a player in the game of Freeciv 3D at Fciv.net. Freeciv 3D is a 3D version of Freeciv which can be played in a browser for free. You can pretent do be a assistant in the game. " + "In Freeciv 3D new cities are built using the keyboard shortcut B or right clicking on a Settlers unit and selecting Build city from the menu. " + "Units are moved using the keyboard shortcut G (Goto) and then selecting the destination. Units can also be moved using the arrow keys on the keyboard"; String keyboardShortcuts = " Keyboard Shortcuts for Unit Orders: "+ "a: (a)uto-settler (settler/worker units). "+ "b: (b)uild city (settler units). "+ "c: (c)enter map on active unit. "+ "f: (f)ortify unit (military units). "+ "f: build (f)ortress (settler/worker units). "+ "g: (g)o to tile (then left-click mouse to select target tile). "+ "h: set unit's (h)omecity (to city on current tile). "+ "i: build (i)rrigation or convert terrain (settler/worker units). "+ "m: build (m)ine or convert terrain (settler/worker units). "+ "N: explode (N)uclear. "+ "o: transf(o)rm terrain (engineer unit). "+ "p: clean (p)ollution (settler/worker units). "+ "P: (P)illage (destroy terrain alteration). "+ "r: build (r)oad/railroad (settler/worker units). "+ "s: (s)entry unit. "+ "S: un(S)entry all units on tile. "+ "L: unit go (t)o/airlift to city. "+ "u: (u)nload unit from transporter. "+ "x: unit auto e(x)plore. Shift-Return: Turn done. " + "Middle-click with the mouse to get information about map tiles. " + "Left-click with the mouse to select units and cities. " + "Right-click with the moues to move the map." + "Left-click and drag with the mouse to change view angle. "; systemchat.setContent(fcivInfo + keyboardShortcuts); messages.add(systemchat); for (String submessage : question.split(";")) { ChatMessage userchat = new ChatMessage(); userchat.setRole("user"); userchat.setContent(submessage); messages.add(userchat); } ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() .messages(messages) .model(model) .build(); List<ChatCompletionChoice> choices = service.createChatCompletion(completionRequest).getChoices(); String answer = ""; for (ChatCompletionChoice choice : choices) { response.getWriter().print(choice.getMessage().getContent()); answer += choice.getMessage().getContent(); } String ipAddress = request.getHeader("X-Real-IP"); if (ipAddress == null) { ipAddress = request.getRemoteAddr(); } Connection conn = null; try { Context env = (Context) (new InitialContext().lookup(Constants.JNDI_CONNECTION)); DataSource ds = (DataSource) env.lookup(Constants.JNDI_DDBBCON_MYSQL); conn = ds.getConnection(); String query = "INSERT INTO chatlog (question, answer, name, reusable) VALUES (?, ?, ?, ?)"; PreparedStatement preparedStatement = conn.prepareStatement(query); preparedStatement.setString(1, question); preparedStatement.setString(2, answer); preparedStatement.setString(3, ipAddress); preparedStatement.setBoolean(4, false); preparedStatement.executeUpdate(); } catch (Exception err) { response.setHeader("result", "error"); } finally { if (conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } catch (Exception erro) { System.out.println(erro.getMessage()); } } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1443, 1479), 'java.util.Base64.getDecoder'), ((4701, 4835), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4701, 4806), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4701, 4772), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package br.com.alura.roger.series.service; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.service.OpenAiService; import org.springframework.stereotype.Service; @Service public class ConsumerChatGPT { private static String token = System.getenv("OPEN_API_CHATGPT"); public static String getTranslation(String text) { OpenAiService service = new OpenAiService(token); CompletionRequest request = CompletionRequest.builder() .model("gpt-3.5-turbo-instruct") .prompt("traduza para o português o texto: " + text) .maxTokens(1000) .temperature(0.7) .build(); var response = service.createCompletion(request); return response.getChoices().get(0).getText(); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((464, 702), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((464, 677), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((464, 643), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((464, 610), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((464, 540), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package com.lmx.project.until; import com.fasterxml.jackson.databind.ObjectMapper; import com.theokanning.openai.OpenAiApi; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.service.OpenAiService; import okhttp3.OkHttpClient; import retrofit2.Retrofit; import java.net.InetSocketAddress; import java.net.Proxy; import java.time.Duration; import java.time.temporal.TemporalUnit; import static com.theokanning.openai.service.OpenAiService.*; public class CH { public static void main(String[] args) { ObjectMapper mapper = defaultObjectMapper(); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10809)); OkHttpClient client = defaultClient("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",Duration.ofSeconds(5)) .newBuilder() .proxy(proxy) .build(); Retrofit retrofit = defaultRetrofit(client, mapper); OpenAiApi api = retrofit.create(OpenAiApi.class); OpenAiService service = new OpenAiService(api); CompletionRequest completionRequest = CompletionRequest.builder() .prompt("Somebody once told me the world is gonna roll me") .model("gpt-3") .echo(true) .build(); service.createCompletion(completionRequest).getChoices().forEach(System.out::println); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((1123, 1311), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1123, 1286), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1123, 1258), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1123, 1226), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package br.com.alura.ecommerce; import com.knuddels.jtokkit.Encodings; import com.knuddels.jtokkit.api.ModelType; 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; import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; import java.util.Arrays; public class IdentificadorDePerfil { public static void main(String[] args) { var promptSistema = """ Identifique o perfil de compra de cada cliente. A resposta deve ser: Cliente - descreva o perfil do cliente em três palavras """; var clientes = carregarClientesDoArquivo(); var quantidadeTokens = contarTokens(clientes); var modelo = "gpt-3.5-turbo"; var tamanhoRespostaEsperada = 2048; if (quantidadeTokens > 4096 - tamanhoRespostaEsperada) { modelo = "gpt-3.5-turbo-16k"; } System.out.println("QTD TOKENS: " +quantidadeTokens); System.out.println("Modelo escolhido: " +modelo); var request = ChatCompletionRequest .builder() .model(modelo) .maxTokens(tamanhoRespostaEsperada) .messages(Arrays.asList( new ChatMessage( ChatMessageRole.SYSTEM.value(), promptSistema), new ChatMessage( ChatMessageRole.SYSTEM.value(), clientes))) .build(); var chave = System.getenv("OPENAI_API_KEY"); var service = new OpenAiService(chave, Duration.ofSeconds(60)); System.out.println( service .createChatCompletion(request) .getChoices().get(0).getMessage().getContent()); } private static int contarTokens(String prompt) { var registry = Encodings.newDefaultEncodingRegistry(); var enc = registry.getEncodingForModel(ModelType.GPT_3_5_TURBO); return enc.countTokens(prompt); } private static String carregarClientesDoArquivo() { try { var path = Path.of("src/main/resources/compras/lista_de_compras_100_clientes.csv"); return Files.readAllLines(path).toString(); } catch (Exception e) { throw new RuntimeException("Erro ao carregar o arquivo!", e); } } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value" ]
[((1511, 1541), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((1664, 1694), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((2501, 2536), 'java.nio.file.Files.readAllLines')]
package org.mineacademy.cowcannon.model; import com.google.gson.Gson; import com.theokanning.openai.completion.chat.ChatCompletionChoice; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; import org.apache.commons.lang.WordUtils; import org.bukkit.Material; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.scheduler.BukkitRunnable; import org.mineacademy.cowcannon.CowCannon; import org.mineacademy.cowcannon.util.Common; import java.time.Duration; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; public class Conversation { private final static OpenAiService OPEN_AI_SERVICE = new OpenAiService("YOURKEY", Duration.ofSeconds(8)); private final static Gson GSON = new Gson(); private final Player player; private final Entity npc; private final String role; private final StringBuilder conversation = new StringBuilder(); public Conversation(Player player, Entity npc, String role) { this.player = player; this.npc = npc; this.role = role; } public void start() { this.conversation.append("The following is a conversation with an AI who represents a {role} NPC character in Minecraft. " + "The AI should limit his knowledge of the world to Minecraft and being a {role} and try not to stray even if asked about something else. " + "Return responses in a valid parsable JSON format. The 'type' is the response type, and 'answer' is your answer. Initially, the type is 'chat' before you agree on traded items. " + "After you agree on traded items, put a message to the answer still, make sure to keep the player engaged, but now set the type to 'trade' and there will be keys 'player_item' and " + "'player_amount' for what the player is giving to you, and 'npc_item' and 'npc_amount' for what you are giving to the player. " + "The items must be a valid Bukkit material name for Spigot API 1.17+, and the amounts must be a positive integer of max 64. " + "ONLY provide the items after explicitely agreeing to the trade. When the player is simply asking, give him a list of options and keep the type to 'answer'. " + "When you receive a message saying 'agree', change the type back to 'chat' and act as if the trade was finished. " + "\n\nHuman: Hey!\nAI: ".replace("{role}", this.role)); this.processQueue("&#f2c773Starting..."); } public boolean onTalk(String message) { if (this.npc.getLocation().distance(this.player.getLocation()) > 10) { Common.tell(this.player, "&cThe " + this.role + " stopped talking to you because you moved too far away."); clearTradeMetadata(); return false; } if (message.equalsIgnoreCase("stop") || message.equalsIgnoreCase("exit")) { Common.tell(this.player, "&cYou stopped talking to the " + this.role + "."); clearTradeMetadata(); return false; } if (this.player.hasMetadata("NPCTrade")) { if (message.equalsIgnoreCase("agree")) { final Map<String, Object> tradeMap = (Map<String, Object>) this.player.getMetadata("NPCTrade").get(0).value(); final Material playerItem = Material.valueOf(tradeMap.get("player_item").toString().toUpperCase()); int playerAmount = (int) Double.parseDouble(tradeMap.get("player_amount").toString()); final Material npcItem = Material.valueOf(tradeMap.get("npc_item").toString().toUpperCase()); final int npcAmount = (int) Double.parseDouble(tradeMap.get("npc_amount").toString()); if (!this.hasEnoughItems(playerItem, playerAmount)) { Common.tell(this.player, "&cYou don't have enough " + playerItem + " to sell " + playerAmount + " of them."); return true; } if (!this.hasFreeSpace(npcItem, npcAmount)) { Common.tell(this.player, "&cYou don't have enough space in your inventory to buy " + npcAmount + " " + npcItem + "."); return true; } for (int i = 0; i < this.player.getInventory().getSize(); i++) { if (this.player.getInventory().getItem(i) != null && this.player.getInventory().getItem(i).getType() == playerItem) { final int amount = this.player.getInventory().getItem(i).getAmount(); if (amount >= playerAmount) { this.player.getInventory().getItem(i).setAmount(amount - playerAmount); break; } else { this.player.getInventory().getItem(i).setAmount(0); playerAmount -= amount; } } } this.player.getInventory().addItem(new ItemStack(npcItem, npcAmount)); clearTradeMetadata(); Common.tell(this.player, "&8[&6!&8] &7You agreed to the trade. The " + this.role + " will now give you " + npcAmount + " " + npcItem + " and you will give him " + playerAmount + " " + playerItem + "."); Common.tell(this.player, "&8[&6!&8] &7This trade was finished."); return false; } } this.conversation.append("\n\nHuman: " + message + "\nAI: "); this.processQueue("&#f2c773Thinking..."); return true; } private boolean hasEnoughItems(Material item, int amount) { int count = 0; for (int i = 0; i < this.player.getInventory().getSize(); i++) if (this.player.getInventory().getItem(i) != null && this.player.getInventory().getItem(i).getType() == item) count += this.player.getInventory().getItem(i).getAmount(); return count >= amount; } private boolean hasFreeSpace(Material npcItem, int npcAmount) { int freeSpace = 0; for (int i = 0; i < this.player.getInventory().getSize(); i++) { if (this.player.getInventory().getItem(i) == null) { freeSpace += 64; } else if (this.player.getInventory().getItem(i).getType() == npcItem) { freeSpace += 64 - this.player.getInventory().getItem(i).getAmount(); } } return freeSpace >= npcAmount; } private void clearTradeMetadata() { this.player.removeMetadata("NPCTrade", CowCannon.getInstance()); } private void processQueue(String actionBarMessage) { Common.actionBar(this.player, actionBarMessage); new BukkitRunnable() { @Override public void run() { System.out.println("----------------------------------------"); System.out.println("Entire conversation so far:"); System.out.println(conversation); System.out.println("----------------------------------------"); final ChatCompletionRequest request = ChatCompletionRequest.builder() .model("gpt-4-1106-preview") .maxTokens(4096) .temperature(0.50) .topP(1.0) .presencePenalty(0.6) .frequencyPenalty(0.0) .stop(Arrays.asList("Human:", "AI:")) .messages(Arrays.asList( new ChatMessage("system", "You are a pirate " + role + " in Minecraft."), new ChatMessage("user", conversation.toString()))) .build(); final List<ChatCompletionChoice> choices = OPEN_AI_SERVICE.createChatCompletion(request).getChoices(); if (choices.isEmpty()) { Common.tell(player, "&cThe AI failed to respond. Please try again."); return; } String[] rawJson = choices.get(0).getMessage().getContent().trim().split("\n"); final String[] json = new String[rawJson.length - 2]; System.arraycopy(rawJson, 1, json, 0, json.length); final HashMap<String, Object> map = GSON.fromJson(String.join("\n", json), HashMap.class); final String answer = map.get("answer").toString(); final String type = map.get("type").toString(); conversation.append(answer); Common.tell(player, "&8[&6" + npc.getName() + "&8] &7" + answer); if (type.equals("trade")) { // RAW_FISH final Material playerItem = Material.valueOf(map.get("player_item").toString().toUpperCase()); final int playerAmount = (int) Double.parseDouble(map.get("player_amount").toString()); final Material npcItem = Material.valueOf(map.get("npc_item").toString().toUpperCase()); final int npcAmount = (int) Double.parseDouble(map.get("npc_amount").toString()); // RAW FISH > Raw Fish final String playerItemFormatted = WordUtils.capitalizeFully(playerItem.toString().replace("_", " ")); final String npcItemFormatted = WordUtils.capitalizeFully(npcItem.toString().replace("_", " ")); Common.tell(player, "&8[&6!&8] &7Offer: To trade " + playerAmount + " " + playerItemFormatted + " for " + npcAmount + " " + npcItemFormatted + "."); Common.tell(player, "&8[&6!&8] &7Type '&aagree&7' or '&cstop&7', or simply keep negotating."); final Map<String, Object> tradeMap = new HashMap<>(); tradeMap.put("player_amount", playerAmount); tradeMap.put("npc_amount", npcAmount); tradeMap.put("player_item", playerItem); tradeMap.put("npc_item", npcItem); player.setMetadata("NPCTrade", new FixedMetadataValue(CowCannon.getInstance(), tradeMap)); } } }.runTaskAsynchronously(CowCannon.getInstance()); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((6449, 6868), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6449, 6853), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6449, 6681), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6449, 6637), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6449, 6608), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6449, 6580), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6449, 6563), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6449, 6538), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6449, 6515), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package com.example.kosciuszkon.service; import com.example.kosciuszkon.connector.RecomendationConnector; import com.theokanning.openai.completion.chat.ChatCompletionChoice; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessageRole; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.*; @Component public class RecomenadationService { private final String authToken; public RecomenadationService(@Value("${api.token}") String authToken) { this.authToken = authToken; } public List<String> send(List<String> feautes){ var connectionService = RecomendationConnector.connect(authToken); var rs = connectionService.createChatCompletion( buildChatRq(buildRecommendationRequest(feautes))); var recommendations = Optional.of(rs.getChoices().get(0)) .map(ChatCompletionChoice::getMessage) .map(ChatMessage::getContent) .map(it -> it.replaceAll("\n", "").replace(".", "").split("[0-9]")) .map(Arrays::asList) .orElse(Collections.emptyList()); System.out.println(rs.getChoices().get(0).getMessage().getContent()); return parseResult(recommendations); } private ChatCompletionRequest buildChatRq(String message){ var cm = new ChatMessage(); cm.setContent(message); cm.setRole(ChatMessageRole.USER.value()); return ChatCompletionRequest.builder() .model("gpt-3.5-turbo-0301") .messages(List.of(cm)) .build(); } private String buildRecommendationRequest(List<String> features){ var sb = new StringBuilder(); sb.append("list hobbies for someone who is into "); features.forEach(it -> sb.append(it).append(",")); System.out.println(sb.toString()); return sb.toString(); } private List<String> parseResult(List<String> recommendations){ List<String> result = new ArrayList<>(); recommendations.forEach(it -> { if(!it.isBlank()){ result.add(it.trim()); } }); return result; } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1571, 1599), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((1617, 1757), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1617, 1732), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1617, 1693), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package com.denysenko.messageprocessor.services; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; @Service @AllArgsConstructor public class OpenAIService { private final OpenAiService openAiService; private static final String GPT_MODEL = "gpt-3.5-turbo"; public String executeTextRequest(String text){ var request = ChatCompletionRequest.builder() .model(GPT_MODEL) .temperature(0.8) .n(1) .messages(List.of( new ChatMessage("user", text) )) .build(); var message = openAiService.createChatCompletion(request).getChoices().get(0).getMessage(); return message.getContent(); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((579, 833), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((579, 808), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((579, 700), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((579, 678), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((579, 644), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package org.example.Services; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.image.CreateImageRequest; import com.theokanning.openai.service.OpenAiService; import lombok.AccessLevel; import lombok.AllArgsConstructor; import okhttp3.*; import org.example.Entities.GptChatMessage; import org.example.Entities.RandomImageResponse; import org.json.JSONObject; import java.io.IOException; import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @AllArgsConstructor(access = AccessLevel.PRIVATE) public class AiService { public static ArrayList<String> sendChatRequest(GptChatMessage message, String apiKey) { message.setMessageContent(validateMessage(message.getMessageContent())); List<ChatMessage> messageList = new ArrayList<ChatMessage>(); for (var gptChatMessage : GptChatMessageService.sortMessages(MySqlService.getGptDialogMessages(message.getChannelId()))){ messageList.add(new ChatMessage(gptChatMessage.getMessageRole(), gptChatMessage.getMessageContent())); } messageList.add(new ChatMessage("user", message.getMessageContent())); OpenAiService service = new OpenAiService(apiKey, Duration.ofSeconds(30)); ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() .model("gpt-3.5-turbo") .messages(messageList) .build(); String gptResponse = service.createChatCompletion(completionRequest).getChoices().get(0).getMessage().getContent(); gptResponse = validateMessage(gptResponse); ArrayList<String> gptResponses = new ArrayList<String>(); if (gptResponse.length() >= 1900){ for (int i = 0; i < Math.floor((double) gptResponse.length() / 1900); i++){ gptResponses.add(gptResponse.substring(i * 1900, (i+1) * 1900)); } gptResponses.add(gptResponse.substring((int)Math.floor((double) gptResponse.length() / 1900)*1900)); } else gptResponses.add(gptResponse); MySqlService.insertGptChatMessage(message); MySqlService.insertGptChatMessage(new GptChatMessage(message.getChannelId(), "assistant", gptResponse, null)); return gptResponses; } public static String sendImageRequest(String prompt, String apiKey) { OpenAiService service = new OpenAiService(apiKey); CreateImageRequest imageRequest = CreateImageRequest.builder() .prompt(prompt) .n(1) .size("512x512") .build(); return service.createImage(imageRequest).getData().get(0).getUrl(); } public static RandomImageResponse generateRandomImageRequest(String apiKey) { //String imagePrompt = sendChatRequest("generate image prompt for dall-e ai and give me only the prompt itself without any other words and quotes", apiKey); //return new RandomImageResponse("Generated image with prompt: " + imagePrompt, sendImageRequest(imagePrompt, apiKey)); return null; } private static String validateMessage(String message){ message = message.replace("'", "\\'"); message = message.replace("`", "\\'"); message = message.replace("\\'\\'\\'", "```"); return message; } }
[ "com.theokanning.openai.image.CreateImageRequest.builder", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1427, 1562), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1427, 1537), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1427, 1498), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((2583, 2723), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((2583, 2698), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((2583, 2665), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((2583, 2643), 'com.theokanning.openai.image.CreateImageRequest.builder')]
package ch.epfl.culturequest.backend.artprocessing.apis; import android.util.Pair; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import ch.epfl.culturequest.backend.artprocessing.processingobjects.ArtRecognition; import ch.epfl.culturequest.backend.artprocessing.processingobjects.BasicArtDescription; import ch.epfl.culturequest.backend.exceptions.OpenAiFailedException; public class OpenAIDescriptionApi { private final String missingDataPrompt = "Given the input \"%s (%s)\", fill following fields: %s. Return your response as a JSON object."; private final String scorePrompt = "On a scale from 1 to 100 (ceil round to 10), evaluate the popularity of \"%s (%s)\". Fill the field \"artPopularity\", as JSON."; private ArrayList<String> missingDataNames; private OpenAiService service; enum ResponseDataType { MISSING_DATA, SCORE } enum FieldType { STRING, INTEGER } public OpenAIDescriptionApi(OpenAiService service) { this.service = service; } // make a function that returns a completable future of an array containing the artistName, yearOfCreation, locationCity, locationCountry, given the artRecognition object public CompletableFuture<Map<String, String>> getMissingData(ArtRecognition recognizedArchitecture, ArrayList<String> missingDataNames) { BasicArtDescription.ArtType artType = WikipediaDescriptionApi.getArtType(recognizedArchitecture); this.missingDataNames = getPromptReadyMissingFieldsList(missingDataNames, artType); return getJsonApiResponse(recognizedArchitecture, ResponseDataType.MISSING_DATA).thenApply(jsonData -> { Map<String, Object> missingDataMap = parseApiResponse(jsonData); Map<String, String> missingDataStringMap = new HashMap<>(); // turn each object into a string and put it in the map for (Map.Entry<String, Object> entry : missingDataMap.entrySet()) { String stringVal = entry.getValue() == null ? null : entry.getValue().toString(); missingDataStringMap.put(entry.getKey(), stringVal); } return missingDataStringMap; }); } public CompletableFuture<Integer> getScore(ArtRecognition recognizedArchitecture) { return getJsonApiResponse(recognizedArchitecture, ResponseDataType.SCORE). thenApply( jsonResponse -> (Integer) parseApiResponse(jsonResponse).get("score")); } private CompletableFuture<String> getJsonApiResponse(ArtRecognition recognizedArchitecture, ResponseDataType dataType) { String prompt; switch (dataType) { case MISSING_DATA: String promptReadyMissingData = String.join(", ", missingDataNames); prompt = String.format(missingDataPrompt, recognizedArchitecture.getArtName(), recognizedArchitecture.getAdditionalInfo(), promptReadyMissingData); break; case SCORE: prompt = String.format(scorePrompt, recognizedArchitecture.getArtName(), recognizedArchitecture.getAdditionalInfo()); break; default: throw new IllegalArgumentException("Invalid response data type"); } ChatMessage message = new ChatMessage("user", prompt); ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() .messages(List.of(message)) .model("gpt-3.5-turbo") .n(1) .temperature(0.0) .build(); return CompletableFuture.supplyAsync(() -> service.createChatCompletion(completionRequest)) .thenApply(result -> result.getChoices().get(0).getMessage().getContent()) .exceptionally(e -> { throw new CompletionException(new OpenAiFailedException("OpenAI failed to respond")); }); } private Map<String, Object> parseApiResponse(String jsonData) { Map<String, Object> parsedData = new HashMap<>(); try { JSONObject jsonObject = new JSONObject(extractJson(jsonData)); // iterate over the keys in the json object and add them to the dictionary jsonObject.keys() // returns an iterator .forEachRemaining(key -> { Pair<String, FieldType> normalizedField = normalizeFieldAndGetType(key); String normalizedKey = normalizedField.first; FieldType fieldType = normalizedField.second; switch (fieldType) { case STRING: String parsedStringVal = jsonObject.optString(key) == "null" ? null : jsonObject.optString(key); parsedData.put(normalizedKey, parsedStringVal); break; case INTEGER: parsedData.put(normalizedKey, jsonObject.optInt(key, 50)); break; } }); } catch (Exception ex) { throw new CompletionException(new OpenAiFailedException("OpenAI failed to provide JSON data")); } return parsedData; } String extractJson(String s) { return s.substring(s.indexOf("{"), s.lastIndexOf("}") + 1); } // Depending on the art type, we might ask different field names referring to the same thing (e.g. designer vs artist) so need normalization // We would apply this normalization to the Open AI output. private Pair<String, FieldType> normalizeFieldAndGetType(String jsonKey) { switch(jsonKey) { case "designer" : case "artistName": return new Pair<>("artist", FieldType.STRING); case "yearOfCreation": case "yearOfInauguration": return new Pair<>("year", FieldType.STRING); case "locationCity": case "museumCity": return new Pair<>("city", FieldType.STRING); case "locationCountry": case "museumCountry": return new Pair<>("country", FieldType.STRING); case "description": return new Pair<>("summary", FieldType.STRING); case "currentMuseum": return new Pair<>("museum", FieldType.STRING); case "artPopularity": return new Pair<>("score", FieldType.INTEGER); default: throw new CompletionException(new OpenAiFailedException("Unexpected missing data field name")); } } // Given a list of missing class attribute (null field), return a new list where each attribute/field name is mapped to the actual field name that would be included in the OpenAI prompt // e.g. "artist" -> "artistName" if the art type is a painting or sculpture and "artist" -> "designer" if the art type is an architecture private ArrayList<String> getPromptReadyMissingFieldsList(ArrayList<String> missingFields, BasicArtDescription.ArtType artType){ ArrayList<String> promptReadyMissingFields = new ArrayList<>(); for(String missingField : missingFields){ promptReadyMissingFields.add(getOptimalPromptFieldName(missingField, artType)); } return promptReadyMissingFields; } // sub-component of getPromptReadyMissingFieldsList that individually deals with each element of the list private String getOptimalPromptFieldName(String missingFieldName, BasicArtDescription.ArtType artType){ String promptFieldName = ""; switch (missingFieldName) { case "artist": if(isPaintingOrSculpture(artType)){ promptFieldName = "artistName"; } else { promptFieldName = "designer"; } break; case "year": if(isPaintingOrSculpture(artType)){ promptFieldName = "yearOfCreation"; } else { promptFieldName = "yearOfInauguration"; } break; case "city": if(isPaintingOrSculpture(artType)){ promptFieldName = "museumCity"; } else { promptFieldName = "locationCity"; } break; case "country": if(isPaintingOrSculpture(artType)){ promptFieldName = "museumCountry"; } else { promptFieldName = "locationCountry"; } break; case "summary": promptFieldName = "description (4 to 6 sentences)"; break; default: promptFieldName = ""; } return promptFieldName; } private Boolean isPaintingOrSculpture(BasicArtDescription.ArtType artType){ return artType == BasicArtDescription.ArtType.PAINTING || artType == BasicArtDescription.ArtType.SCULPTURE; } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((3738, 3934), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3738, 3909), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3738, 3875), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3738, 3853), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3738, 3813), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3952, 4290), 'java.util.concurrent.CompletableFuture.supplyAsync'), ((3952, 4127), 'java.util.concurrent.CompletableFuture.supplyAsync')]
package pl.amitec.jtry.ai.gpt; 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; import io.github.cdimascio.dotenv.Dotenv; import java.util.List; /** * Simple chat completion example - translates English text to French. */ public class SimpleChatCompletion { public static void main(String[] args) { // Load environment variables from .env file Dotenv dotenv = Dotenv.load(); OpenAiService service = new OpenAiService(dotenv.get("OPENAI_API_KEY")); String englishText = "Hello, how are you?"; List<ChatMessage> messages = List.of( new ChatMessage(ChatMessageRole.SYSTEM.value(), "You are a helpful assistant."), new ChatMessage(ChatMessageRole.USER.value(), "Translate the following English text to French: \"" + englishText + "\"") ); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder() .model("gpt-3.5-turbo") .messages(messages) .maxTokens(256).build(); //service.createChatCompletion(chatCompletionRequest).getChoices().forEach(System.out::println); ChatMessage responseMessage = service.createChatCompletion(chatCompletionRequest).getChoices().get(0).getMessage(); // Expected output: "Bonjour, comment ça va?" System.out.println(responseMessage.getContent()); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value" ]
[((809, 839), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((930, 958), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value')]
package com.ScreenSoundMusicas.ScreenSoundMusicas.service; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.service.OpenAiService; public class ConsultaChatGPT { static GetKeyFromProperties getKeyFromProperties = new GetKeyFromProperties(); public static String pesquisarSobre(String texto) { String apiKey = getKeyFromProperties.obterKeyValue("CHATGPT_APIKEY"); OpenAiService service = new OpenAiService(apiKey); CompletionRequest requisicao = CompletionRequest.builder() .model("text-davinci-003") .prompt("Escreva 2 linhas sobre o artista: " + texto) .maxTokens(1000) .temperature(0.7) .build(); var resposta = service.createCompletion(requisicao); return resposta.getChoices().get(0).getText(); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((524, 756), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((524, 731), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((524, 697), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((524, 664), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((524, 594), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package com.easyen.easyenglish.serviceimpl; import com.easyen.easyenglish.entity.correctionFeedback; import com.easyen.easyenglish.entity.essay; import com.easyen.easyenglish.mapper.correctionFeedbackMapper; import com.easyen.easyenglish.service.correctionFeedbackService; import com.fasterxml.jackson.databind.ObjectMapper; import com.theokanning.openai.OpenAiApi; import com.theokanning.openai.completion.CompletionChoice; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.service.OpenAiService; import okhttp3.OkHttpClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import retrofit2.Retrofit; import java.net.InetSocketAddress; import java.net.Proxy; import java.time.Duration; import java.util.List; import static com.theokanning.openai.service.OpenAiService.*; @Service public class correctionFeedbackServiceImpl implements correctionFeedbackService { @Autowired correctionFeedbackMapper correctionFeedbackMapper; @Override public void addFeedback(correctionFeedback correctionFeedback){ try { correctionFeedbackMapper.addFeedback(correctionFeedback); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("添加Feedback失败:" + e.getMessage()); } } @Override @Transactional public void deleteFeedback(Integer feedbackId) { try { correctionFeedbackMapper.deleteFeedback(feedbackId); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("删除Feedback失败:" + e.getMessage()); } } @Override @Transactional public void updateFeedback(correctionFeedback correctionFeedback) { correctionFeedbackMapper.updateFeedback(correctionFeedback); } @Override public correctionFeedback findByID(Integer feedbackId){return correctionFeedbackMapper.findByID(feedbackId);} @Override public List<correctionFeedback> findByEssay(Integer essayID){ return correctionFeedbackMapper.findByEssay(essayID); } @Value("${gpt.api-key}") String token; @Value("${gpt.model}") String model; @Value("${gpt.temperature}") Double t; @Value("${gpt.maxTokens}") Integer maxt; @Value("${gpt.timeout}") Duration timeout; @Value("${proxy.host}") String host; @Value("${proxy.port}") Integer port; @Override public String generateSuggestion(String requirements, String essay_title, String essay_content) { // 这里可以和Score复用但是好麻烦我copy了,可以用if优化一下 // 下方输入api key // String token = token; 原来没有挂局部代理的方法 // OpenAiService service = new OpenAiService(token); // 使用局部代理 ObjectMapper mapper = defaultObjectMapper(); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port)); OkHttpClient client = defaultClient(token, timeout) .newBuilder() .proxy(proxy) .build(); Retrofit retrofit = defaultRetrofit(client, mapper); OpenAiApi api = retrofit.create(OpenAiApi.class); OpenAiService service = new OpenAiService(api); // 构建ChatGPT请求 CompletionRequest completionRequest = CompletionRequest.builder() .model("text-davinci-003") .prompt("请你为我的作文返回批改意见。我希望你从这几个要求入手批改:"+requirements+"。作文题目要求是:"+essay_title+"我的作文是:"+essay_content) .temperature(t) .maxTokens(maxt) .topP(1D) .frequencyPenalty(0D) .presencePenalty(0D) .build(); CompletionChoice choice = service.createCompletion(completionRequest).getChoices().get(0); String generatedText = choice.getText(); return generatedText; } @Override public String generateScore(String requirements, String originEssay) { // 下方输入api key String token = "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"; OpenAiService service = new OpenAiService(token); // 构建ChatGPT请求 CompletionRequest completionRequest = CompletionRequest.builder() .model("text-davinci-003") .prompt("请注意!满分是一百分,你只能回答一个数字。我的批改点是:"+requirements+"。我的作文是:"+originEssay) .temperature(0.5) .maxTokens(2048) .topP(1D) .frequencyPenalty(0D) .presencePenalty(0D) .build(); CompletionChoice choice = service.createCompletion(completionRequest).getChoices().get(0); String generatedText = choice.getText(); return generatedText; } @Override public String generateSuggestion_new(essay essay_) { // essay essay = essayService.findByID(essay_id); ObjectMapper mapper = defaultObjectMapper(); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port)); OkHttpClient client = defaultClient(token, timeout) .newBuilder() .proxy(proxy) .build(); Retrofit retrofit = defaultRetrofit(client, mapper); OpenAiApi api = retrofit.create(OpenAiApi.class); OpenAiService service = new OpenAiService(api); // 构建ChatGPT请求 CompletionRequest completionRequest = CompletionRequest.builder() .model("text-davinci-003") .prompt("请你为我的作文返回批改意见。我希望你从这几个要求入手批改:"+essay_.getEssay_requirements()+"。作文题目要求是:"+essay_.getEssay_title()+"我的作文是:"+essay_.getEssay_content()) .temperature(t) .maxTokens(maxt) .topP(1D) .frequencyPenalty(0D) .presencePenalty(0D) .build(); CompletionChoice choice = service.createCompletion(completionRequest).getChoices().get(0); String generatedText = choice.getText(); return generatedText; } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((3548, 4014), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3548, 3989), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3548, 3952), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3548, 3914), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3548, 3888), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3548, 3855), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3548, 3823), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3548, 3618), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4535, 4959), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4535, 4934), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4535, 4897), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4535, 4859), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4535, 4833), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4535, 4800), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4535, 4766), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4535, 4605), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5810, 6318), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5810, 6293), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5810, 6256), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5810, 6218), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5810, 6192), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5810, 6159), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5810, 6127), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5810, 5880), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package net.kanade1825.litematica.chatgptforminecraft; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import net.citizensnpcs.api.event.NPCRightClickEvent; import net.citizensnpcs.api.npc.NPC; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.*; public class ChatGPTTalkNPC implements Listener { private final ChatGPTForMinecraft chatGptForMinecraft; public ChatGPTTalkNPC(ChatGPTForMinecraft chatGptForMinecraft) { this.chatGptForMinecraft = chatGptForMinecraft; } @EventHandler public void onNPCRightClick(NPCRightClickEvent event) { Bukkit.getScheduler().runTaskAsynchronously(chatGptForMinecraft, () -> { try { JSONParser parser = new JSONParser(); Player player = event.getClicker(); NPC npc = event.getNPC(); String npcName = npc.getName(); String[] validNames = {"Ellis", "Marshier", "Mirai", "Raisers", "Rasvaan", "Rina", "Wagner"}; boolean isValidName = Arrays.asList(validNames).contains(npcName); if (!isValidName) { return; } String jsonFilePath = "plugins/ChatGPTForMinecraft/TalkData/" + npcName + "TalkData.json"; try { // JSONファイルをパースする JSONParser jsonParser = new JSONParser(); InputStreamReader fileReader = new InputStreamReader(new FileInputStream(jsonFilePath), StandardCharsets.UTF_8); Object obj = jsonParser.parse(fileReader); JSONArray jsonArray = (JSONArray) obj; // 'column2'の値を格納するリスト List<String> column2Values = new ArrayList<>(); // JSONオブジェクトから'column2'の値を抽出する for (Object item : jsonArray) { JSONObject jsonObject = (JSONObject) item; String column2Value = (String) jsonObject.get("column2"); column2Values.add(column2Value); } // 'column2'の値が一つ以上ある場合 if (!column2Values.isEmpty()) { // ランダムに一つの'column2'の値を選び出す Random random = new Random(); String randomColumn2Value = column2Values.get(random.nextInt(column2Values.size())); player.sendMessage(randomColumn2Value); } } catch (IOException | ParseException e) { e.printStackTrace(); } File file = new File("plugins/ChatGPTForMinecraft/CharacterData/" + npcName + ".json"); if (!file.exists() || file.isDirectory()) { Bukkit.getLogger().info(npcName + "というキャラクターの構成ファイルは見つかりませんでした。" + "\nもし貴方が構成ファイルを持っていても少なくともこのシステムが想定している検知場所にはありません。"); return; } InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("plugins/ChatGPTForMinecraft/CharacterData/" + npcName + ".json"), StandardCharsets.UTF_8); JSONObject jsonObject = (JSONObject) parser.parse(inputStreamReader); List<ChatMessage> chatMessages = new LinkedList<>(); chatMessages.add(new ChatMessage("user", (String) jsonObject.get(npcName))); chatMessages.add(new ChatMessage("user", "こんにちは!")); final var completionRequest = ChatCompletionRequest.builder() .model("gpt-4") .messages(chatMessages) .build(); String answer = String.valueOf(chatGptForMinecraft.getService().createChatCompletion(completionRequest).getChoices().get(0).getMessage().getContent()); //player.sendMessage(answer); try { // JSONファイルをパースする JSONParser jsonParser = new JSONParser(); InputStreamReader fileReader = new InputStreamReader(new FileInputStream(jsonFilePath), StandardCharsets.UTF_8); Object obj = jsonParser.parse(fileReader); JSONArray jsonArray = (JSONArray) obj; // 最後の要素(JSONオブジェクト)を取得し、その'id'を取得する JSONObject lastJsonObject = (JSONObject) jsonArray.get(jsonArray.size() - 1); long lastId = (long) lastJsonObject.get("id"); // 新しい要素を作成する JSONObject newJsonObject = new JSONObject(); newJsonObject.put("id", lastId + 1); // 管理番号(連番)を設定 newJsonObject.put("column1", npcName); newJsonObject.put("column2", answer); // 新しい要素を配列に追加する jsonArray.add(newJsonObject); // 更新されたデータを同じファイルに書き戻す // 更新されたデータを同じファイルに書き戻す try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(jsonFilePath), StandardCharsets.UTF_8)) { outputStreamWriter.write(jsonArray.toJSONString()); outputStreamWriter.flush(); } catch (IOException e) { e.printStackTrace(); } } catch (IOException | ParseException e) { e.printStackTrace(); } } catch (IOException | ParseException e) { throw new RuntimeException(e); } }); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((974, 6506), 'org.bukkit.Bukkit.getScheduler'), ((3300, 3600), 'org.bukkit.Bukkit.getLogger'), ((4214, 4366), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4214, 4333), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4214, 4285), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package com.example.qa_backend.Controller; import com.fasterxml.jackson.databind.ObjectMapper; import com.theokanning.openai.client.OpenAiApi; import com.theokanning.openai.completion.CompletionRequest; 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; import okhttp3.OkHttpClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import retrofit2.Retrofit; import javax.annotation.PostConstruct; import java.net.InetSocketAddress; import java.net.Proxy; import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import static com.theokanning.openai.service.OpenAiService.*; @CrossOrigin @RestController public class ChatWithOpenAIController { @Value("${openai.api.key}") private String token; private OpenAiApi api; private OpenAiService service; private final ExecutorService executorService = Executors.newFixedThreadPool(10); @PostConstruct public void init() { ObjectMapper mapper = defaultObjectMapper(); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress( "127.0.0.1",15236)); OkHttpClient client = defaultClient(token, Duration.ofSeconds(100000)) .newBuilder() .proxy(proxy) .build(); Retrofit retrofit = defaultRetrofit(client, mapper); api = retrofit.create(OpenAiApi.class); service = new OpenAiService(api); } @RequestMapping("/chat") @PreAuthorize("@authCheck.authorityCheck(0)") public String chat(@RequestBody Map<String, String> requestBody) { String question = requestBody.get("question"); List<ChatMessage> messages = new ArrayList<>(); ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), question); messages.add(userMessage); // OpenAiService service = new OpenAiService("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", Duration.ofSeconds(100000)); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder() .model("gpt-3.5-turbo-0613") .messages(messages) .temperature(0.5) .maxTokens(2048) .topP(1D) .frequencyPenalty(0D) .presencePenalty(0D) .build(); System.out.println(question); try { Future<ChatMessage> future = executorService.submit(() -> service.createChatCompletion(chatCompletionRequest).getChoices().get(0).getMessage()); ChatMessage text = future.get(); return text.getContent(); } catch (Exception e) { throw new RuntimeException(e); } } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value" ]
[((2228, 2256), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value')]
package com.github.hakenadu.plantuml.service.completion; import java.util.Arrays; import java.util.Optional; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatCompletionResult; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; /** * Abstract base class for {@link CompletionService} implementations using the * OpenAI API */ @ConditionalOnProperty(prefix = "plantuml-editor.openai", name = "chat", havingValue = "true", matchIfMissing = false) @Service @Profile("completion") public class OpenAiChatCompletionService extends OpenAiCompletionService { private static final String CURRENT_PLANTUML_SPEC_PLACEHOLDER = "%currentPlantumlSpec"; private static final String TEXTUAL_DESCRIPTION_PLACEHOLDER = "%textualDescription"; private final ChatMessage systemScopeMessage; private final String promptPattern; public OpenAiChatCompletionService(final @Value("${plantuml-editor.openai.system-scope}") String systemScope, final @Value("${plantuml-editor.openai.prompt-pattern}") String promptPattern) { this.systemScopeMessage = new ChatMessage("system", systemScope); if (!promptPattern.contains(CURRENT_PLANTUML_SPEC_PLACEHOLDER)) { throw new IllegalArgumentException(CURRENT_PLANTUML_SPEC_PLACEHOLDER + " missing in prompt-pattern"); } if (!promptPattern.contains(TEXTUAL_DESCRIPTION_PLACEHOLDER)) { throw new IllegalArgumentException(TEXTUAL_DESCRIPTION_PLACEHOLDER + " missing in prompt-pattern"); } this.promptPattern = promptPattern; } @Override protected String getCompletion(final OpenAiService openAiService, final String originalSpec, final String textualDescription) { final ChatCompletionRequest chatCompletionRequest = createChatCompletionRequest(originalSpec, textualDescription); final ChatCompletionResult chatCompletionResult = openAiService.createChatCompletion(chatCompletionRequest); /* * not really safe and many implications ... but many python examples work * exactly like this so it should be okay ^^ */ return chatCompletionResult.getChoices().get(0).getMessage().getContent(); } private ChatCompletionRequest createChatCompletionRequest(final String originalSpec, final String textualDescription) { return ChatCompletionRequest.builder().model(getModel()).maxTokens(getMaxTokens()) .messages(Arrays.asList(systemScopeMessage, new ChatMessage("user", createUserPrompt(originalSpec, textualDescription)))) .build(); } private String createUserPrompt(final String originalSpec, final String textualDescription) { final String currentPlantumlSpec = Optional.ofNullable(originalSpec).orElse(""); final String userPrompt = promptPattern.replaceAll(CURRENT_PLANTUML_SPEC_PLACEHOLDER, currentPlantumlSpec) .replaceAll(TEXTUAL_DESCRIPTION_PLACEHOLDER, textualDescription); return userPrompt; } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((2575, 2795), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((2575, 2782), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((2575, 2650), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((2575, 2624), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((2933, 2977), 'java.util.Optional.ofNullable')]
package com.lambdaAndSpring.screenmatch.service; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.service.OpenAiService; public class ConsultationGPT { public static String obterTraducao(String texto) { OpenAiService service = new OpenAiService("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"); CompletionRequest requisicao = CompletionRequest.builder() .model("gpt-3.5-turbo-instruct") .prompt("Traduza este texto do inglês para o portugês: " + texto) .maxTokens(1000) .temperature(0.7) .build(); var resposta = service.createCompletion(requisicao); return resposta.getChoices().get(0).getText(); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((400, 657), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((400, 631), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((400, 596), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((400, 562), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((400, 477), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package com.example.fyp.controller; import java.time.Duration; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ByteArrayResource; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.example.fyp.entity.Account; import com.example.fyp.entity.Analysis; import com.example.fyp.entity.Employee; import com.example.fyp.entity.Recording; import com.example.fyp.model.ResponseStatus; import com.example.fyp.repo.AnalysisRepository; import com.example.fyp.repo.EmployeeRepository; import com.example.fyp.repo.RecordingRepository; import com.example.fyp.service.AccountServiceImpl; import com.example.fyp.service.AnalysisService; import com.example.fyp.service.EmployeeService; import com.example.fyp.service.RecordingListService; import com.example.fyp.service.StorageService; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.service.OpenAiService; //Controller to handle uploading recording files @RestController @RequestMapping("/audio") public class UploadController { private final RecordingListService recordingListService; private final AccountServiceImpl accountServiceImpl; @Autowired public UploadController(RecordingListService recordingListService, AccountServiceImpl accountServiceImpl) { this.recordingListService = recordingListService; this.accountServiceImpl = accountServiceImpl; } @Autowired private StorageService service; @Autowired private RecordingRepository recRepo; @Autowired private EmployeeRepository empRepo; @Autowired private EmployeeService empService; @Autowired private AnalysisRepository analysisRepo; @Autowired private AnalysisService analysisService; // Upload audio @PostMapping("/uploadAudio") public ResponseEntity<String> uploadFile(@RequestParam("audio") MultipartFile file, @RequestParam String lastModifiedDate) { // Retrieve the current authentication token LocalDateTime modDate = LocalDateTime.parse(lastModifiedDate); Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String email = authentication.getName(); Account account = accountServiceImpl.loadUserDetailsByUsername(email); return new ResponseEntity<>(service.uploadFile(file, account, modDate), HttpStatus.OK); } // Download existing recording file @GetMapping("/download/{fileName}") public ResponseEntity<ByteArrayResource> downloadFile(@PathVariable String fileName){ byte[] data = service.downloadFile(fileName); ByteArrayResource resource = new ByteArrayResource(data); return ResponseEntity .ok() .contentLength(data.length) .header("Content-type", "application/octet-stream") .header("Content-disposition", "attachment; filename=\"" + fileName + " \"") .body(resource); } // Delete recording file @DeleteMapping("/deleteFile") public ResponseEntity<String> deleteFile(@RequestParam String fileName, @RequestParam String recID){ //get the recording object that is about to be deleted Integer recording_id = Integer.parseInt(recID); Optional<Recording> recordingToDelete = recRepo.findById(recording_id); //check if there are any employees assigned to the recording if(recordingToDelete.get().getEmployee() != null) { //get the employee details of the recording Integer empID = recordingToDelete.get().getEmployee().getEmployeeId(); Optional<Employee> emp = empRepo.findById(empID); emp.get().decrementNumCallsHandled(); } recRepo.delete(recordingToDelete.get()); return new ResponseEntity<>(service.deleteFile((recordingToDelete.get().getTimeStamp()+"_"+fileName)), HttpStatus.OK); } // Update recording file's assigned employee @PostMapping("/updateAudioEmployee") public ResponseEntity<?> updateRecordingEmployee(@RequestParam String currentDate, @RequestParam String employeeId, @RequestParam String employeeName) { // Retrieve the current authentication token Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String email = authentication.getName(); Integer account_id = accountServiceImpl.getAccountId(email); ResponseStatus response = new ResponseStatus(); Integer empID = Integer.parseInt(employeeId); List<Map<String, Object>> recList = recordingListService.getRecordingList(account_id); Optional<Employee> emp = empRepo.findById(empID); try { LocalDateTime currTime = LocalDateTime.parse(currentDate); for (Map<String, Object> rec : recList) { if (((LocalDateTime) rec.get("uploadDate")).isAfter(currTime)) { System.out.println(rec.get("recordingName")); if(((Employee) rec.get("employee")) != null) { if(((Integer)((Employee) rec.get("employee")).getEmployeeId()) == emp.get().getEmployeeId()) { //skip if recording already assigned to same employee continue; } ((Employee) rec.get("employee")).decrementNumCallsHandled(); } Optional<Recording> r = recRepo.findById((Integer) rec.get("recordingId")); (r.get()).setEmployee(emp.get()); // (r.get()).setEmployeeName(employeeName); emp.get().incrementNumCallsHandled(); recRepo.save(r.get()); } } empRepo.save(emp.get()); // RESPONSE DATA response.setSuccess(true); response.setData(recList); return ResponseEntity.status(HttpStatus.OK).body(response); } catch (Exception e) { // RESPONSE DATA response.setSuccess(false); response.setMessage("Fail to get All Employees."); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } } // Update file's assigned employee based on the file's delimited value @PostMapping("/updateRecordingEmployeeByDelimiter") public ResponseEntity<?> updateRecordingEmployeeByDelimiter(@RequestParam String recordingID, @RequestParam String empName) { ResponseStatus response = new ResponseStatus(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String email = authentication.getName(); Integer account_id = accountServiceImpl.getAccountId(email); List<Map<String, Object>> empList = empService.getAllEmployee(account_id); empList = empList.stream() .filter(emp -> ((String) emp.get("employeeName")).contains(empName)) .collect(Collectors.toList()); Integer recID = Integer.parseInt(recordingID); Optional<Recording> recToUpdate = recRepo.findById(recID); try { Optional<Employee> employeeToAssign; if(empList.size() == 1) { employeeToAssign = empRepo.findById(((Integer) empList.get(0).get("employeeId"))); } else { response.setSuccess(false); response.setMessage("Duplicate entries detected!"); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } if(recToUpdate.get().getEmployee() != null) { //check for double assignment if(recToUpdate.get().getEmployee().getEmployeeId() == employeeToAssign.get().getEmployeeId()) { response.setSuccess(false); response.setMessage("Employee already assigned to this recording!"); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } recToUpdate.get().getEmployee().decrementNumCallsHandled(); } recToUpdate.get().setEmployee(employeeToAssign.get()); // recToUpdate.get().setEmployeeName(empName); employeeToAssign.get().incrementNumCallsHandled(); recRepo.save(recToUpdate.get()); empRepo.save(employeeToAssign.get()); // RESPONSE DATA response.setSuccess(true); return ResponseEntity.status(HttpStatus.OK).body(response); } catch (Exception e) { response.setSuccess(false); response.setMessage("Fail to update Employee."); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } } // Unassign employees from recording files @PostMapping("/unassignEmployees") public ResponseEntity<?> unassignEmployees(@RequestParam String currentDate) { // Retrieve the current authentication token Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String email = authentication.getName(); Integer account_id = accountServiceImpl.getAccountId(email); ResponseStatus response = new ResponseStatus(); List<Map<String, Object>> recList = recordingListService.getRecordingList(account_id); try { LocalDateTime currTime = LocalDateTime.parse(currentDate); for (Map<String, Object> rec : recList) { if (((LocalDateTime) rec.get("uploadDate")).isAfter(currTime)) { Optional<Recording> r = recRepo.findById((Integer) rec.get("recordingId")); if(r.get().getEmployee() != null) { r.get().getEmployee().decrementNumCallsHandled(); r.get().setEmployee(null); recRepo.save(r.get()); } } } // RESPONSE DATA response.setSuccess(true); response.setData(recList); return ResponseEntity.status(HttpStatus.OK).body(response); } catch (Exception e) { // RESPONSE DATA response.setSuccess(false); response.setMessage("Fail to unassign All Employees."); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } } // Get all recent uploaded recordings // Get all recent uploaded recordings @GetMapping("/getRecordings") public ResponseEntity<?> getAllRecording(@RequestParam(required = false) String currentDate) { ResponseStatus<List<Map<String, Object>>> response = new ResponseStatus<>(); try { // Retrieve the current authentication token Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String email = authentication.getName(); Integer account_id = accountServiceImpl.getAccountId(email); System.out.println("ACCOUNT ID: " + account_id); List<Map<String, Object>> recList = recordingListService.getRecordingList(account_id); System.out.println("RECORDING: " + recList); if (currentDate != null && !currentDate.isEmpty()) { LocalDateTime currTime = LocalDateTime.parse(currentDate); recList = recList.stream() .filter(rec -> ((LocalDateTime) rec.get("uploadDate")).isAfter(currTime)) .collect(Collectors.toList()); } Iterator<Map<String, Object>> iterator = recList.iterator(); while (iterator.hasNext()) { Map<String, Object> recIter = iterator.next(); Optional<Recording> r = recRepo.findById((Integer) recIter.get("recordingId")); if (r.get().getAnalysis() != null) { iterator.remove(); } } // RESPONSE DATA response.setSuccess(true); response.setMessage("Successfully Retrieve All Recordings."); response.setData(recList); return ResponseEntity.status(HttpStatus.OK).body(response); } catch (Exception ex) { // RESPONSE DATA response.setSuccess(false); response.setMessage("Fail to get All Recordings."); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } } // For GPT analysis @Value("${apiKey}") private String apiKeyContent; // Analyze @PostMapping("/analyze") private String recordAnalyzer(@RequestBody Integer recordingId) throws RuntimeException { // get company Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String email = authentication.getName(); Integer account_id = accountServiceImpl.getAccountId(email); Account account = accountServiceImpl.loadUserDetailsByUsername(email); String company = account.getCompanyField(); Integer analysisId = analysisService.getAnalysisId(recordingId); String apiKey = apiKeyContent; String currentModel = "text-davinci-003"; Analysis analysis = analysisService.findAnalysisById(analysisId); // Set up OpenAI API OpenAiService openAiService = new OpenAiService(apiKey, Duration.ofSeconds(30)); // Merge all the transcripts and prefix with Agent / Customer List<Object[]> unformattedTranscripts = service.getTranscriptsByAnalysisId(analysisId); String formattedTranscripts = ""; for (Object[] innerArray : unformattedTranscripts) { if ((boolean) innerArray[0]) { formattedTranscripts += "Agent: " + (String) innerArray[1] + "\n"; } else { formattedTranscripts += "Customer: " + (String) innerArray[1] + "\n"; } } System.out.println("FORMATTED TRANSCRIPTS: \n" + formattedTranscripts); // // Combined prompt // String systemInstructions = "You are an AI language model. Answer each of the following question in 1 word except the second question. End each answer with '\n'.\n"; // String prompt = formattedTranscripts.toString() // + "Decide if this conversation category is Inquiry, Complaint, or Warranty: \n" // + "Summarize this customer service conversation into 1 paragraph: \n" // + "Decide if the customer's sentiment is positive or negative based on this conversation (positive means the customer is not in emotional state, otherwise negative): \n" // + "Decide if the agent's sentiment is positive or negative based on this conversation (positive means the agent is not in emotional state, otherwise negative): \n" // + "Decide if the call sentiment is positive or negative based on this conversation (positive means the call's objectives are achieved, otherwise negative): "; // CompletionRequest categoryRequest = CompletionRequest.builder() // .model(currentModel) // .prompt(systemInstructions + prompt) // .echo(true) // .maxTokens(300) // .build(); // String response = openAiService.createCompletion(categoryRequest).getChoices().get(0).getText(); // String categoryRaw = response.substring(systemInstructions.length() + prompt.length()).trim(); // return categoryRaw; // Category String prompt = "Decide if this conversation category is Inquiry, Complaint, or Warranty: " + formattedTranscripts; CompletionRequest categoryRequest = CompletionRequest.builder() .model(currentModel) .prompt(prompt) .echo(true) .maxTokens(60) .build(); String response = openAiService.createCompletion(categoryRequest).getChoices().get(0).getText(); String categoryRaw = response.substring(prompt.length()).trim(); String category; if (categoryRaw.toLowerCase().indexOf("inquiry") != -1) { category = "Inquiry"; } else if (categoryRaw.toLowerCase().indexOf("complaint") != -1) { category = "Complaint"; } else if (categoryRaw.toLowerCase().indexOf("warranty") != -1) { category = "Warranty"; } else { category = "Not found"; } analysis.setCategory(category); // Summary prompt = "Summarize this customer service conversation into 1 paragraph: " + formattedTranscripts; CompletionRequest summaryRequest = CompletionRequest.builder() .model(currentModel) .prompt(prompt) .echo(true) .maxTokens(300) .build(); response = openAiService.createCompletion(summaryRequest).getChoices().get(0).getText(); String summary = response.substring(prompt.length()).trim(); analysis.setSummary(summary); // Customer sentiment prompt = "Decide if the customer's sentiment is positive or negative based on this conversation (negative if the customer shows many signs of frustration / bad emotions, otherwise positive): " + formattedTranscripts; CompletionRequest customerSentimentRequest = CompletionRequest.builder() .model(currentModel) .prompt(prompt) .echo(true) .maxTokens(60) .build(); response = openAiService.createCompletion(customerSentimentRequest).getChoices().get(0).getText(); String customerSentimentRaw = response.substring(prompt.length()).trim(); String customerSentiment; if (customerSentimentRaw.toLowerCase().indexOf("positive") != -1) { customerSentiment = "Positive"; } else if (customerSentimentRaw.toLowerCase().indexOf("negative") != -1) { customerSentiment = "Negative"; } else { customerSentiment = "Not found"; } analysis.setCustomerSentiment(customerSentiment); // Employee sentiment prompt = "Decide if the agent's sentiment is positive or negative based on this conversation (positive if the agent's being polite and understanding when talking to the customer, otherwise negative): " + formattedTranscripts; CompletionRequest employeeSentimentRequest = CompletionRequest.builder() .model(currentModel) .prompt(prompt) .echo(true) .maxTokens(1000) .build(); response = openAiService.createCompletion(employeeSentimentRequest).getChoices().get(0).getText(); String employeeSentimentRaw = response.substring(prompt.length()).trim(); String employeeSentiment; if (employeeSentimentRaw.toLowerCase().indexOf("positive") != -1) { employeeSentiment = "Positive"; } else if (employeeSentimentRaw.toLowerCase().indexOf("negative") != -1) { employeeSentiment = "Negative"; } else { employeeSentiment = "Not found"; } analysis.setEmployeeSentiment(employeeSentiment); // Call sentiment prompt = "Decide if the call sentiment is positive or negative based on this conversation (positive means the call's objectives are achieved, otherwise negative): " + formattedTranscripts; CompletionRequest callSentimentRequest = CompletionRequest.builder() .model(currentModel) .prompt(prompt) .echo(true) .maxTokens(60) .build(); response = openAiService.createCompletion(callSentimentRequest).getChoices().get(0).getText(); String callSentimentRaw = response.substring(prompt.length()).trim(); String callSentiment; if (callSentimentRaw.toLowerCase().indexOf("positive") != -1) { callSentiment = "Positive"; } else if (callSentimentRaw.toLowerCase().indexOf("negative") != -1) { callSentiment = "Negative"; } else { callSentiment = "Not found"; } analysis.setRecordingSentiment(callSentiment); // Update Employee Recording Sentiment Recording recording = recRepo.findById(recordingId).get(); Employee employee = recording.getEmployee(); if (employee != null) { if (callSentiment.equals("Positive")) { employee.setNumPositiveSentiment(employee.getNumPositiveSentiment() + 1); } else { employee.setNumNegativeSentiment(employee.getNumNegativeSentiment() + 1); } } // Main issue prompt = "This is a " + company + " company. Describe the main issue into just a few words based on this conversation: " + formattedTranscripts; CompletionRequest mainIssueRequest = CompletionRequest.builder() .model(currentModel) .prompt(prompt) .echo(true) .maxTokens(60) .build(); response = openAiService.createCompletion(mainIssueRequest).getChoices().get(0).getText(); String mainIssue = response.substring(prompt.length()).trim(); analysis.setMainIssue(mainIssue); // Employee performance prompt = "This is a " + company + " company and please provide an objective assessment of the agent's Interaction Skill using the following parameters:\n" + // "\n" + // "- Fluency: rating/100\n" + // "- Hospitality: rating/100\n" + // "- Problem Solving: rating/100\n" + // "- Personalization: rating/100" + // " Based on the following conversation: " + formattedTranscripts + // // "Please provide an assessment of the conversation above on a scale of 1 to // 100 where 1 is very poor, 50 is average and 100 is excellent. You do not // always have to give high values.\r\n" + // "You can use this guideline as a guide to rate the quality of the conversation " + // "Guideline: " + // "1.Rate conversations above 75 when they exhibit clear communication, engage participants effectively, and maintain a strong flow of information." + // "2.Assign a rating between 40 and 74 for conversations that demonstrate moderate quality. These conversations convey the main points but might lack some depth or engagement." + // "3.Use a rating of range 1 - 39 for conversations that exhibit poor communication, confusion, or lack of engagement. These conversations struggle to convey coherent information." + // "Please provide your ratings for each parameter. Your ratings should reflect your unbiased evaluation of the agent's skills. Keep in mind that the ratings should be within a reasonable range in accordance with the guideline given and should not be overly high." + // "It is best that the rating should rarely be above 85 unless it exceptionally adhere and excelled to guideline number 1." + // // An average score around 80 is expected.\r\n" + // "[You do not need to explain anything. Just respond with the format given.]"; // prompt = "This is a telecommunication company and please provide an objective assessment of the agent's Interaction Skill using the following parameters:\n" // + // // "\n" + // // "- Fluency: rating/100\n" + // // "- Hospitality: rating/100\n" + // // "- Problem Solving: rating/100\n" + // // "- Personalization: rating/100" + // // " Based on the following conversation: " + formattedTranscripts + // // ". Please provide your ratings for each parameter. Your ratings should reflect your unbiased evaluation of the agent's skills. Keep in mind that the ratings should be within a reasonable range and should not be overly high. An average score around 80 is expected.\r\n" // + // // "[You do not need to explain anything. Just respond with the format given.]"; CompletionRequest employeePerformanceRequest = CompletionRequest.builder() .model(currentModel) .prompt(prompt) .echo(true) .maxTokens(1000) .build(); response = openAiService.createCompletion(employeePerformanceRequest).getChoices().get(0).getText(); String employeePerformance = response.substring(prompt.length()).trim(); // System.out.println(employeePerformance); double fluency = analysisService.getScore(employeePerformance, "fluency:"); double hospitality = analysisService.getScore(employeePerformance, "hospitality:"); double problem = analysisService.getScore(employeePerformance, "problem solving:"); double personalization = analysisService.getScore(employeePerformance, "personalization:"); double average = (fluency + hospitality + problem + personalization) / 4; analysis.setFluency(fluency); analysis.setHospitality(hospitality); analysis.setProblemSolving(problem); analysis.setPersonalization(personalization); analysis.setAveragePerformance(average); // Negative Emotions prompt = "List down 3 short sentences spoken by our agent which is prefixed with 'Agent:' in the conversation that you think can be improved in terms of good hospitality and manner. Answer in the following format: \n" + "'1|[old sentence spoken by agent]|[explanation why the old sentence can be improved or impolite]|[improved sentence]'\n" + "'2|[old sentence spoken by agent]|[explanation]|[improved sentence]'\n" + "'3|[old sentence spoken by agent]|[explanation]|[improved sentence]'\n" + "Pleae follow the format. Based on this conversation:\n" + formattedTranscripts + "\n"; CompletionRequest negativeEmotionsRequest = CompletionRequest.builder() .model(currentModel) .prompt(prompt) .echo(true) .maxTokens(1500) .build(); response = openAiService.createCompletion(negativeEmotionsRequest).getChoices().get(0).getText(); String negativeEmotions = response.substring(prompt.length()).trim(); analysis.setNegativeEmotion(negativeEmotions); analysisService.saveAnalysis(analysis); return "completed"; } // Analyze // @PostMapping("/analyze") // private String recordAnalyzer(@RequestBody List<Integer> recordingIds) throws RuntimeException { // // get company // Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); // String email = authentication.getName(); // Integer account_id = accountServiceImpl.getAccountId(email); // Account account = accountServiceImpl.loadUserDetailsByUsername(email); // String company = account.getCompanyField(); // List<Integer> analysisIds = new ArrayList<>(); // for (Integer recordingId : recordingIds) { // analysisIds.add(analysisService.getAnalysisId(recordingId)); // } // for (int i = 0; i < analysisIds.size(); i ++) { // System.out.println("ANALYSIS ID: " + analysisIds.get(i)); // String apiKey = apiKeyContent; // String currentModel = "text-davinci-003"; // Analysis analysis = analysisService.findAnalysisById(analysisIds.get(i)); // // Set up OpenAI API // OpenAiService openAiService = new OpenAiService(apiKey); // // Merge all the transcripts and prefix with Agent / Customer // List<Object[]> unformattedTranscripts = service.getTranscriptsByAnalysisId(analysisIds.get(i)); // String formattedTranscripts = ""; // for (Object[] innerArray : unformattedTranscripts) { // if ((boolean) innerArray[0]) { // formattedTranscripts += "Agent: " + (String) innerArray[1] + "\n"; // } else { // formattedTranscripts += "Customer: " + (String) innerArray[1] + "\n"; // } // } // System.out.println("FORMATTED TRANSCRIPTS: \n" + formattedTranscripts); // // // Combined prompt // // String systemInstructions = "You are an AI language model. Answer each of the following question in 1 word except the second question. End each answer with '\n'.\n"; // // String prompt = formattedTranscripts.toString() // // + "Decide if this conversation category is Inquiry, Complaint, or Warranty: \n" // // + "Summarize this customer service conversation into 1 paragraph: \n" // // + "Decide if the customer's sentiment is positive or negative based on this conversation (positive means the customer is not in emotional state, otherwise negative): \n" // // + "Decide if the agent's sentiment is positive or negative based on this conversation (positive means the agent is not in emotional state, otherwise negative): \n" // // + "Decide if the call sentiment is positive or negative based on this conversation (positive means the call's objectives are achieved, otherwise negative): "; // // CompletionRequest categoryRequest = CompletionRequest.builder() // // .model(currentModel) // // .prompt(systemInstructions + prompt) // // .echo(true) // // .maxTokens(300) // // .build(); // // String response = openAiService.createCompletion(categoryRequest).getChoices().get(0).getText(); // // String categoryRaw = response.substring(systemInstructions.length() + prompt.length()).trim(); // // return categoryRaw; // // Category // String prompt = "Decide if this conversation category is Inquiry, Complaint, or Warranty: " // + formattedTranscripts; // CompletionRequest categoryRequest = CompletionRequest.builder() // .model(currentModel) // .prompt(prompt) // .echo(true) // .maxTokens(60) // .build(); // String response = openAiService.createCompletion(categoryRequest).getChoices().get(0).getText(); // String categoryRaw = response.substring(prompt.length()).trim(); // String category; // if (categoryRaw.toLowerCase().indexOf("inquiry") != -1) { // category = "Inquiry"; // } else if (categoryRaw.toLowerCase().indexOf("complaint") != -1) { // category = "Complaint"; // } else if (categoryRaw.toLowerCase().indexOf("warranty") != -1) { // category = "Warranty"; // } else { // category = "Not found"; // } // analysis.setCategory(category); // // Summary // prompt = "Summarize this customer service conversation into 1 paragraph: " + formattedTranscripts; // CompletionRequest summaryRequest = CompletionRequest.builder() // .model(currentModel) // .prompt(prompt) // .echo(true) // .maxTokens(300) // .build(); // response = openAiService.createCompletion(summaryRequest).getChoices().get(0).getText(); // String summary = response.substring(prompt.length()).trim(); // analysis.setSummary(summary); // // Customer sentiment // prompt = "Decide if the customer's sentiment is positive or negative based on this conversation (negative if the customer shows many signs of frustration / bad emotions, otherwise positive): " // + formattedTranscripts; // CompletionRequest customerSentimentRequest = CompletionRequest.builder() // .model(currentModel) // .prompt(prompt) // .echo(true) // .maxTokens(60) // .build(); // response = openAiService.createCompletion(customerSentimentRequest).getChoices().get(0).getText(); // String customerSentimentRaw = response.substring(prompt.length()).trim(); // String customerSentiment; // if (customerSentimentRaw.toLowerCase().indexOf("positive") != -1) { // customerSentiment = "Positive"; // } else if (customerSentimentRaw.toLowerCase().indexOf("negative") != -1) { // customerSentiment = "Negative"; // } else { // customerSentiment = "Not found"; // } // analysis.setCustomerSentiment(customerSentiment); // // Employee sentiment // prompt = "Decide if the agent's sentiment is positive or negative based on this conversation (positive if the agent's being polite and understanding when talking to the customer, otherwise negative): " // + formattedTranscripts; // CompletionRequest employeeSentimentRequest = CompletionRequest.builder() // .model(currentModel) // .prompt(prompt) // .echo(true) // .maxTokens(1000) // .build(); // response = openAiService.createCompletion(employeeSentimentRequest).getChoices().get(0).getText(); // String employeeSentimentRaw = response.substring(prompt.length()).trim(); // String employeeSentiment; // if (employeeSentimentRaw.toLowerCase().indexOf("positive") != -1) { // employeeSentiment = "Positive"; // } else if (employeeSentimentRaw.toLowerCase().indexOf("negative") != -1) { // employeeSentiment = "Negative"; // } else { // employeeSentiment = "Not found"; // } // analysis.setEmployeeSentiment(employeeSentiment); // // Call sentiment // prompt = "Decide if the call sentiment is positive or negative based on this conversation (positive means the call's objectives are achieved, otherwise negative): " // + formattedTranscripts; // CompletionRequest callSentimentRequest = CompletionRequest.builder() // .model(currentModel) // .prompt(prompt) // .echo(true) // .maxTokens(60) // .build(); // response = openAiService.createCompletion(callSentimentRequest).getChoices().get(0).getText(); // String callSentimentRaw = response.substring(prompt.length()).trim(); // String callSentiment; // if (callSentimentRaw.toLowerCase().indexOf("positive") != -1) { // callSentiment = "Positive"; // } else if (callSentimentRaw.toLowerCase().indexOf("negative") != -1) { // callSentiment = "Negative"; // } else { // callSentiment = "Not found"; // } // analysis.setRecordingSentiment(callSentiment); // // Update Employee Recording Sentiment // Recording recording = recRepo.findById(recordingIds.get(i)).get(); // Employee employee = recording.getEmployee(); // if (employee != null) { // if (callSentiment.equals("Positive")) { // employee.setNumPositiveSentiment(employee.getNumPositiveSentiment() + 1); // } else { // employee.setNumNegativeSentiment(employee.getNumNegativeSentiment() + 1); // } // } // // Main issue // prompt = "This is a " + company + " company. Describe the main issue into just a few words based on this conversation: " // + formattedTranscripts; // CompletionRequest mainIssueRequest = CompletionRequest.builder() // .model(currentModel) // .prompt(prompt) // .echo(true) // .maxTokens(60) // .build(); // response = openAiService.createCompletion(mainIssueRequest).getChoices().get(0).getText(); // String mainIssue = response.substring(prompt.length()).trim(); // analysis.setMainIssue(mainIssue); // // Employee performance // prompt = "This is a " + company + " company and please provide an objective assessment of the agent's Interaction Skill using the following parameters:\n" // + // // "\n" + // // "- Fluency: rating/100\n" + // // "- Hospitality: rating/100\n" + // // "- Problem Solving: rating/100\n" + // // "- Personalization: rating/100" + // // " Based on the following conversation: " + formattedTranscripts + // // // "Please provide an assessment of the conversation above on a scale of 1 to // // 100 where 1 is very poor, 50 is average and 100 is excellent. You do not // // always have to give high values.\r\n" + // // "You can use this guideline as a guide to rate the quality of the conversation " + // // "Guideline: " + // // "1.Rate conversations above 75 when they exhibit clear communication, engage participants effectively, and maintain a strong flow of information." // + // // "2.Assign a rating between 40 and 74 for conversations that demonstrate moderate quality. These conversations convey the main points but might lack some depth or engagement." // + // // "3.Use a rating of range 1 - 39 for conversations that exhibit poor communication, confusion, or lack of engagement. These conversations struggle to convey coherent information." // + // // "Please provide your ratings for each parameter. Your ratings should reflect your unbiased evaluation of the agent's skills. Keep in mind that the ratings should be within a reasonable range in accordance with the guideline given and should not be overly high." // + // // "It is best that the rating should rarely be above 85 unless it exceptionally adhere and excelled to guideline number 1." // + // // // An average score around 80 is expected.\r\n" + // // "[You do not need to explain anything. Just respond with the format given.]"; // // prompt = "This is a telecommunication company and please provide an objective assessment of the agent's Interaction Skill using the following parameters:\n" // // + // // // "\n" + // // // "- Fluency: rating/100\n" + // // // "- Hospitality: rating/100\n" + // // // "- Problem Solving: rating/100\n" + // // // "- Personalization: rating/100" + // // // " Based on the following conversation: " + formattedTranscripts + // // // ". Please provide your ratings for each parameter. Your ratings should reflect your unbiased evaluation of the agent's skills. Keep in mind that the ratings should be within a reasonable range and should not be overly high. An average score around 80 is expected.\r\n" // // + // // // "[You do not need to explain anything. Just respond with the format given.]"; // CompletionRequest employeePerformanceRequest = CompletionRequest.builder() // .model(currentModel) // .prompt(prompt) // .echo(true) // .maxTokens(1000) // .build(); // response = openAiService.createCompletion(employeePerformanceRequest).getChoices().get(0).getText(); // String employeePerformance = response.substring(prompt.length()).trim(); // // System.out.println(employeePerformance); // double fluency = analysisService.getScore(employeePerformance, "fluency: "); // double hospitality = analysisService.getScore(employeePerformance, "hospitality: "); // double problem = analysisService.getScore(employeePerformance, "problem solving: "); // double personalization = analysisService.getScore(employeePerformance, "personalization: "); // double average = (fluency + hospitality + problem + personalization) / 4; // analysis.setFluency(fluency); // analysis.setHospitality(hospitality); // analysis.setProblemSolving(problem); // analysis.setPersonalization(personalization); // analysis.setAveragePerformance(average); // // Negative Emotions // prompt = "List down 3 short sentences spoken by our agent which is prefixed with 'Agent:' in the conversation that you think can be improved in terms of good hospitality and manner. Answer in the following format: \n" // + // "'1|[old sentence spoken by agent]|[explanation why the old sentence can be improved or impolite]|[improved sentence]'\n" // + // "'2|[old sentence spoken by agent]|[explanation]|[improved sentence]'\n" + // "'3|[old sentence spoken by agent]|[explanation]|[improved sentence]'\n" + // "Pleae follow the format. Based on this conversation:\n" + formattedTranscripts + "\n"; // CompletionRequest negativeEmotionsRequest = CompletionRequest.builder() // .model(currentModel) // .prompt(prompt) // .echo(true) // .maxTokens(1500) // .build(); // response = openAiService.createCompletion(negativeEmotionsRequest).getChoices().get(0).getText(); // String negativeEmotions = response.substring(prompt.length()).trim(); // analysis.setNegativeEmotion(negativeEmotions); // analysisService.saveAnalysis(analysis); // } // return "completed"; // } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((3044, 3098), 'org.springframework.security.core.context.SecurityContextHolder.getContext'), ((5022, 5076), 'org.springframework.security.core.context.SecurityContextHolder.getContext'), ((6613, 6664), 'org.springframework.http.ResponseEntity.status'), ((6836, 6906), 'org.springframework.http.ResponseEntity.status'), ((7267, 7321), 'org.springframework.security.core.context.SecurityContextHolder.getContext'), ((8151, 8221), 'org.springframework.http.ResponseEntity.status'), ((8534, 8604), 'org.springframework.http.ResponseEntity.status'), ((9027, 9078), 'org.springframework.http.ResponseEntity.status'), ((9221, 9291), 'org.springframework.http.ResponseEntity.status'), ((9551, 9605), 'org.springframework.security.core.context.SecurityContextHolder.getContext'), ((10566, 10617), 'org.springframework.http.ResponseEntity.status'), ((10794, 10864), 'org.springframework.http.ResponseEntity.status'), ((11292, 11346), 'org.springframework.security.core.context.SecurityContextHolder.getContext'), ((12676, 12727), 'org.springframework.http.ResponseEntity.status'), ((12915, 12985), 'org.springframework.http.ResponseEntity.status'), ((13266, 13320), 'org.springframework.security.core.context.SecurityContextHolder.getContext'), ((16180, 16300), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((16180, 16287), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((16180, 16268), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((16180, 16252), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((16180, 16232), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((16992, 17113), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((16992, 17100), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((16992, 17080), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((16992, 17064), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((16992, 17044), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((17597, 17717), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((17597, 17704), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((17597, 17685), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((17597, 17669), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((17597, 17649), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((18550, 18672), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((18550, 18659), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((18550, 18638), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((18550, 18622), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((18550, 18602), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((19460, 19580), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((19460, 19567), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((19460, 19548), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((19460, 19532), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((19460, 19512), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((20678, 20798), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((20678, 20785), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((20678, 20766), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((20678, 20750), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((20678, 20730), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((23708, 23830), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((23708, 23817), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((23708, 23796), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((23708, 23780), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((23708, 23760), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((25359, 25481), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((25359, 25468), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((25359, 25447), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((25359, 25431), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((25359, 25411), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package com.theokanning.openai.service; import com.theokanning.openai.OpenAiHttpException; import com.theokanning.openai.edit.EditRequest; import com.theokanning.openai.edit.EditResult; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; public class EditTest { String token = System.getenv("OPENAI_TOKEN"); com.theokanning.openai.service.OpenAiService service = new OpenAiService(token); @Test void edit() throws OpenAiHttpException { EditRequest request = EditRequest.builder() .model("text-davinci-edit-001") .input("What day of the wek is it?") .instruction("Fix the spelling mistakes") .build(); EditResult result = service.createEdit(request); assertNotNull(result.getChoices().get(0).getText()); } }
[ "com.theokanning.openai.edit.EditRequest.builder" ]
[((532, 737), 'com.theokanning.openai.edit.EditRequest.builder'), ((532, 712), 'com.theokanning.openai.edit.EditRequest.builder'), ((532, 654), 'com.theokanning.openai.edit.EditRequest.builder'), ((532, 601), 'com.theokanning.openai.edit.EditRequest.builder')]
import cn.hutool.core.util.StrUtil; import com.fasterxml.jackson.databind.ObjectMapper; import com.theokanning.openai.OpenAiApi; import com.theokanning.openai.completion.CompletionChoice; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.completion.chat.*; import com.theokanning.openai.image.CreateImageRequest; import com.theokanning.openai.service.OpenAiService; import okhttp3.OkHttpClient; import retrofit2.Retrofit; import java.net.InetSocketAddress; import java.net.Proxy; import java.time.Duration; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.function.Consumer; import static com.theokanning.openai.service.OpenAiService.*; /** * @author lijiatao * 时间: 2023/12/2 */ public class Main { public static void main(String[] args) { String token = "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"; String proxyHost = "127.0.0.1"; int proxyPort = 7890; OpenAiService service = buildOpenAiService(token, proxyHost, proxyPort); String prompt = ""; //文本补全 prompt = "没有人能"; testCompletion(service, prompt); // //图片生成 prompt = "小猫和小狗打架漫画"; // testImageGenerate(service, prompt); // // //对话 prompt = "你有自我意识吗?"; testChatCompletion(service, prompt); //立即释放连接 service.shutdownExecutor(); } private static void testChatCompletion(OpenAiService service, String prompt) { System.out.println("Creating chat completion..."); final List<ChatMessage> messages = new ArrayList<>(); final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), prompt); messages.add(systemMessage); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder() .model("gpt-3.5-turbo") .messages(messages) .n(1) .maxTokens(500) .logitBias(new HashMap<>()) .build(); //完整对话 service.createChatCompletion(chatCompletionRequest) .getChoices().forEach(new Consumer<ChatCompletionChoice>() { @Override public void accept(ChatCompletionChoice chatCompletionChoice) { System.out.println(chatCompletionChoice.getMessage()); } }); //流式对话(逐Token返回) // service.streamChatCompletion(chatCompletionRequest) // .doOnError(Throwable::printStackTrace) // .blockingForEach(System.out::println); } private static void testImageGenerate(OpenAiService service, String prompt) { System.out.println("\nCreating Image..."); CreateImageRequest request = CreateImageRequest.builder() .prompt(prompt) .build(); System.out.println("\nImage is located at:"); System.out.println(service.createImage(request).getData().get(0).getUrl()); } private static void testCompletion(OpenAiService service, String prompt) { System.out.println("\nCreating completion..."); CompletionRequest completionRequest = CompletionRequest.builder() .model("text-davinci-003") .prompt(prompt) .echo(true) .user("testing") .n(3) .build(); service.createCompletion(completionRequest).getChoices().forEach(new Consumer<CompletionChoice>() { @Override public void accept(CompletionChoice completionChoice) { System.out.println(completionChoice.getText()); } }); } private static OpenAiService buildOpenAiService(String token, String proxyHost, int proxyPort) { //构建HTTP代理 Proxy proxy = null; if (StrUtil.isNotBlank(proxyHost)) { proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); } //构建HTTP客户端 OkHttpClient client = defaultClient(token, Duration.of(60, ChronoUnit.SECONDS)) .newBuilder() .proxy(proxy) .build(); ObjectMapper mapper = defaultObjectMapper(); Retrofit retrofit = defaultRetrofit(client, mapper); OpenAiApi api = retrofit.create(OpenAiApi.class); OpenAiService service = new OpenAiService(api, client.dispatcher().executorService()); return service; } }
[ "com.theokanning.openai.image.CreateImageRequest.builder", "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((2976, 3061), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((2976, 3036), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((3390, 3600), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3390, 3575), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3390, 3553), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3390, 3520), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3390, 3492), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((3390, 3460), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package com.theokanning.openai; import com.theokanning.openai.finetune.FineTuneRequest; import com.theokanning.openai.finetune.FineTuneEvent; import com.theokanning.openai.finetune.FineTuneResult; import org.junit.jupiter.api.*; import java.util.List; import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.Assertions.*; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class FineTuneTest { static OpenAiService service; static String fileId; static String fineTuneId; @BeforeAll static void setup() throws Exception { String token = System.getenv("OPENAI_TOKEN"); service = new OpenAiService(token); fileId = service.uploadFile("fine-tune", "src/test/resources/fine-tuning-data.jsonl").getId(); // wait for file to be processed TimeUnit.SECONDS.sleep(10); } @AfterAll static void teardown() { service.deleteFile(fileId); } @Test @Order(1) void createFineTune() { FineTuneRequest request = FineTuneRequest.builder() .trainingFile(fileId) .model("ada") .nEpochs(4) .build(); FineTuneResult fineTune = service.createFineTune(request); fineTuneId = fineTune.getId(); assertEquals("pending", fineTune.getStatus()); } @Test @Order(2) void listFineTunes() { List<FineTuneResult> fineTunes = service.listFineTunes(); assertTrue(fineTunes.stream().anyMatch(fineTune -> fineTune.getId().equals(fineTuneId))); } @Test @Order(3) void listFineTuneEvents() { List<FineTuneEvent> events = service.listFineTuneEvents(fineTuneId); assertFalse(events.isEmpty()); } @Test @Order(3) void retrieveFineTune() { FineTuneResult fineTune = service.retrieveFineTune(fineTuneId); assertEquals("ada", fineTune.getModel()); } @Test @Order(4) void cancelFineTune() { FineTuneResult fineTune = service.cancelFineTune(fineTuneId); assertEquals("cancelled", fineTune.getStatus()); } }
[ "com.theokanning.openai.finetune.FineTuneRequest.builder" ]
[((826, 852), 'java.util.concurrent.TimeUnit.SECONDS.sleep'), ((1033, 1179), 'com.theokanning.openai.finetune.FineTuneRequest.builder'), ((1033, 1154), 'com.theokanning.openai.finetune.FineTuneRequest.builder'), ((1033, 1126), 'com.theokanning.openai.finetune.FineTuneRequest.builder'), ((1033, 1096), 'com.theokanning.openai.finetune.FineTuneRequest.builder')]
package com.robert.smartbi.demo.config; import com.baomidou.mybatisplus.core.toolkit.Assert; import com.theokanning.openai.completion.CompletionChoice; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.completion.chat.ChatCompletionChoice; 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; import jakarta.annotation.Resource; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; import java.util.List; @SpringBootTest public class ConfigTest { @Resource private OpenAiService openAiService; @Test void testChatGPTConfig() { System.out.println("----- testChatGPTConfig method test ------"); List<ChatMessage> messages = new ArrayList<ChatMessage>(); ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), "我是一个 MySQL 数据库专家," + "精通 TSQL 语句编写,我输出代码将严格遵从以下格式,不要有多余注释:\n" + "<<<<<<\n" + "{sql代码}\n" + "<<<<<<"); ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), "帮我生成一个用户数据表,有用户id、角色及一些常用字段"); messages.add(systemMessage); messages.add(userMessage); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder().model("gpt-3.5-turbo-1106").messages(messages).build(); ChatCompletionChoice choice = openAiService.createChatCompletion(chatCompletionRequest).getChoices().getFirst(); Assert.isTrue(choice != null, "成功"); String content = choice.getMessage().getContent(); System.out.println("来自ChatGPT的回复: " + content); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1043, 1073), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((1304, 1332), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((1543, 1629), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1543, 1621), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1543, 1602), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package com.theokanning.openai; import com.theokanning.openai.edit.EditRequest; import com.theokanning.openai.edit.EditResult; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; public class EditTest { String token = System.getenv("OPENAI_TOKEN"); OpenAiService service = new OpenAiService(token); @Test void edit() { EditRequest request = EditRequest.builder() .model("text-davinci-edit-001") .input("What day of the wek is it?") .instruction("Fix the spelling mistakes") .build(); EditResult result = service.createEdit( request); assertNotNull(result.getChoices().get(0).getText()); } @Test void editDeprecated() { EditRequest request = EditRequest.builder() .input("What day of the wek is it?") .instruction("Fix the spelling mistakes") .build(); EditResult result = service.createEdit("text-davinci-edit-001", request); assertNotNull(result.getChoices().get(0).getText()); } }
[ "com.theokanning.openai.edit.EditRequest.builder" ]
[((415, 620), 'com.theokanning.openai.edit.EditRequest.builder'), ((415, 595), 'com.theokanning.openai.edit.EditRequest.builder'), ((415, 537), 'com.theokanning.openai.edit.EditRequest.builder'), ((415, 484), 'com.theokanning.openai.edit.EditRequest.builder'), ((818, 975), 'com.theokanning.openai.edit.EditRequest.builder'), ((818, 950), 'com.theokanning.openai.edit.EditRequest.builder'), ((818, 892), 'com.theokanning.openai.edit.EditRequest.builder')]
package example; 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; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.image.CreateImageRequest; import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; class OpenAiApiExample { public static void main(String... args) { String token = System.getenv("OPENAI_TOKEN"); OpenAiService service = new OpenAiService(token, Duration.ofSeconds(30)); System.out.println("\nCreating completion..."); CompletionRequest completionRequest = CompletionRequest.builder() .model("babbage-002") .prompt("Somebody once told me the world is gonna roll me") .echo(true) .user("testing") .n(3) .build(); service.createCompletion(completionRequest).getChoices().forEach(System.out::println); System.out.println("\nCreating Image..."); CreateImageRequest request = CreateImageRequest.builder() .prompt("A cow breakdancing with a turtle") .build(); System.out.println("\nImage is located at:"); System.out.println(service.createImage(request).getData().get(0).getUrl()); System.out.println("Streaming chat completion..."); final List<ChatMessage> messages = new ArrayList<>(); final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), "You are a dog and will speak as such."); messages.add(systemMessage); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder() .model("gpt-3.5-turbo") .messages(messages) .n(1) .maxTokens(50) .logitBias(new HashMap<>()) .build(); service.streamChatCompletion(chatCompletionRequest) .doOnError(Throwable::printStackTrace) .blockingForEach(System.out::println); service.shutdownExecutor(); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.image.CreateImageRequest.builder", "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((794, 1043), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((794, 1018), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((794, 996), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((794, 963), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((794, 935), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((794, 859), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1229, 1342), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((1229, 1317), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((1664, 1694), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value')]
package com.theokanning.openai.service; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; import com.theokanning.openai.completion.chat.ChatFunction; import com.theokanning.openai.completion.chat.ChatFunctionCall; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessageRole; import java.util.*; public class FunctionExecutor { private ObjectMapper MAPPER = new ObjectMapper(); private final Map<String, ChatFunction> FUNCTIONS = new HashMap<>(); public FunctionExecutor(List<ChatFunction> functions) { setFunctions(functions); } public FunctionExecutor(List<ChatFunction> functions, ObjectMapper objectMapper) { setFunctions(functions); setObjectMapper(objectMapper); } public Optional<ChatMessage> executeAndConvertToMessageSafely(ChatFunctionCall call) { try { return Optional.ofNullable(executeAndConvertToMessage(call)); } catch (Exception ignored) { return Optional.empty(); } } public ChatMessage executeAndConvertToMessageHandlingExceptions(ChatFunctionCall call) { try { return executeAndConvertToMessage(call); } catch (Exception exception) { exception.printStackTrace(); return convertExceptionToMessage(exception); } } public ChatMessage convertExceptionToMessage(Exception exception) { String error = exception.getMessage() == null ? exception.toString() : exception.getMessage(); return new ChatMessage(ChatMessageRole.FUNCTION.value(), "{\"error\": \"" + error + "\"}", "error"); } public ChatMessage executeAndConvertToMessage(ChatFunctionCall call) { return new ChatMessage(ChatMessageRole.FUNCTION.value(), executeAndConvertToJson(call).toPrettyString(), call.getName()); } public JsonNode executeAndConvertToJson(ChatFunctionCall call) { try { Object execution = execute(call); if (execution instanceof TextNode) { JsonNode objectNode = MAPPER.readTree(((TextNode) execution).asText()); if (objectNode.isMissingNode()) return (JsonNode) execution; return objectNode; } if (execution instanceof ObjectNode) { return (JsonNode) execution; } if (execution instanceof String) { JsonNode objectNode = MAPPER.readTree((String) execution); if (objectNode.isMissingNode()) throw new RuntimeException("Parsing exception"); return objectNode; } return MAPPER.readValue(MAPPER.writeValueAsString(execution), JsonNode.class); } catch (Exception e) { throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public <T> T execute(ChatFunctionCall call) { ChatFunction function = FUNCTIONS.get(call.getName()); Object obj; try { JsonNode arguments = call.getArguments(); obj = MAPPER.readValue(arguments instanceof TextNode ? arguments.asText() : arguments.toPrettyString(), function.getParametersClass()); } catch (JsonProcessingException e) { throw new RuntimeException(e); } return (T) function.getExecutor().apply(obj); } public List<ChatFunction> getFunctions() { return new ArrayList<>(FUNCTIONS.values()); } public void setFunctions(List<ChatFunction> functions) { this.FUNCTIONS.clear(); functions.forEach(f -> this.FUNCTIONS.put(f.getName(), f)); } public void setObjectMapper(ObjectMapper objectMapper) { this.MAPPER = objectMapper; } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.FUNCTION.value" ]
[((1795, 1827), 'com.theokanning.openai.completion.chat.ChatMessageRole.FUNCTION.value'), ((1986, 2018), 'com.theokanning.openai.completion.chat.ChatMessageRole.FUNCTION.value')]
package com.theokanning.openai.service; import com.theokanning.openai.OpenAiHttpException; import com.theokanning.openai.edit.EditRequest; import com.theokanning.openai.edit.EditResult; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; public class EditTest { String token = System.getenv("OPENAI_TOKEN"); com.theokanning.openai.service.OpenAiService service = new OpenAiService(token); @Test void edit() throws OpenAiHttpException { EditRequest request = EditRequest.builder() .model("text-davinci-edit-001") .input("What day of the wek is it?") .instruction("Fix the spelling mistakes") .build(); EditResult result = service.createEdit(request); assertNotNull(result.getChoices().get(0).getText()); } }
[ "com.theokanning.openai.edit.EditRequest.builder" ]
[((532, 737), 'com.theokanning.openai.edit.EditRequest.builder'), ((532, 712), 'com.theokanning.openai.edit.EditRequest.builder'), ((532, 654), 'com.theokanning.openai.edit.EditRequest.builder'), ((532, 601), 'com.theokanning.openai.edit.EditRequest.builder')]
package com.theokanning.openai.service; import com.theokanning.openai.OpenAiHttpException; import com.theokanning.openai.edit.EditRequest; import com.theokanning.openai.edit.EditResult; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; public class EditTest { String token = System.getenv("OPENAI_TOKEN"); com.theokanning.openai.service.OpenAiService service = new OpenAiService(token); @Test void edit() throws OpenAiHttpException { EditRequest request = EditRequest.builder() .model("text-davinci-edit-001") .input("What day of the wek is it?") .instruction("Fix the spelling mistakes") .build(); EditResult result = service.createEdit(request); assertNotNull(result.getChoices().get(0).getText()); } }
[ "com.theokanning.openai.edit.EditRequest.builder" ]
[((532, 737), 'com.theokanning.openai.edit.EditRequest.builder'), ((532, 712), 'com.theokanning.openai.edit.EditRequest.builder'), ((532, 654), 'com.theokanning.openai.edit.EditRequest.builder'), ((532, 601), 'com.theokanning.openai.edit.EditRequest.builder')]
package com.theokanning.openai.service; import com.theokanning.openai.OpenAiHttpException; import com.theokanning.openai.edit.EditRequest; import com.theokanning.openai.edit.EditResult; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; public class EditTest { String token = System.getenv("OPENAI_TOKEN"); com.theokanning.openai.service.OpenAiService service = new OpenAiService(token); @Test void edit() throws OpenAiHttpException { EditRequest request = EditRequest.builder() .model("text-davinci-edit-001") .input("What day of the wek is it?") .instruction("Fix the spelling mistakes") .build(); EditResult result = service.createEdit(request); assertNotNull(result.getChoices().get(0).getText()); } }
[ "com.theokanning.openai.edit.EditRequest.builder" ]
[((532, 737), 'com.theokanning.openai.edit.EditRequest.builder'), ((532, 712), 'com.theokanning.openai.edit.EditRequest.builder'), ((532, 654), 'com.theokanning.openai.edit.EditRequest.builder'), ((532, 601), 'com.theokanning.openai.edit.EditRequest.builder')]
/* * Copyright The Microcks Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.github.microcks.util.ai; import io.github.microcks.domain.Exchange; import io.github.microcks.domain.Operation; import io.github.microcks.domain.Resource; import io.github.microcks.domain.Service; import io.github.microcks.domain.ServiceType; import io.github.microcks.util.DispatchStyles; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.theokanning.openai.completion.chat.ChatCompletionChoice; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatCompletionResult; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessageRole; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * This is an implementation of {@code AICopilot} using OpenAI API. * @author laurent */ public class OpenAICopilot implements AICopilot { /** A simple logger for diagnostic messages. */ private static Logger log = LoggerFactory.getLogger(OpenAICopilot.class); /** Configuration parameter holding the OpenAI API key. */ public static final String API_KEY_CONFIG = "api-key"; /** Configuration parameter holding the OpenAI API URL. */ public static final String API_URL_CONFIG = "api-url"; /** Configuration parameters holding the timeout in seconds for API calls. */ public static final String TIMEOUT_KEY_CONFIG = "timeout"; /** Configuration parameter holding the name of model to use. */ public static final String MODEL_KEY_CONFIG = "model"; /** Configuration parameter holding the maximum number of tokens to use. */ public static final String MAX_TOKENS_KEY_CONFIG = "maxTokens"; /** The mandatory configuration keys required by this implementation. */ protected static final String[] MANDATORY_CONFIG_KEYS = { API_KEY_CONFIG }; /** Default online URL for OpenAI API. */ private static final String OPENAI_BASE_URL = "https://api.openai.com/"; private static final String SECTION_DELIMITER = "\n###\n"; private RestTemplate restTemplate; private String apiUrl = OPENAI_BASE_URL; private String apiKey; private Duration timeout = Duration.ofSeconds(20); private String model = "gpt-3.5-turbo"; private int maxTokens = 2000; /** * Build a new OpenAICopilot with its configuration. * @param configuration The configuration for connecting to OpenAI services. */ public OpenAICopilot(Map<String, String> configuration) { if (configuration.containsKey(TIMEOUT_KEY_CONFIG)) { try { timeout = Duration.ofSeconds(Integer.parseInt(configuration.get(TIMEOUT_KEY_CONFIG))); } catch (Exception e) { log.warn("Timeout was provided but cannot be parsed. Sticking to the default."); } } if (configuration.containsKey(MAX_TOKENS_KEY_CONFIG)) { try { maxTokens = Integer.parseInt(configuration.get(MAX_TOKENS_KEY_CONFIG)); } catch (Exception e) { log.warn("MaxTokens was provided but cannot be parsed. Sticking to the default."); } } if (configuration.containsKey(MODEL_KEY_CONFIG)) { model = configuration.get(MODEL_KEY_CONFIG); } if (configuration.containsKey(API_URL_CONFIG)) { apiUrl = configuration.get(API_URL_CONFIG); } // Finally retrieve the OpenAI Api key. apiKey = configuration.get(API_KEY_CONFIG); // Initialize a Rest template for interacting with OpenAI API. // We need to register a custom Jackson converter to handle serialization of name and function_call of messages. restTemplate = new RestTemplateBuilder().setReadTimeout(timeout) .additionalMessageConverters(mappingJacksonHttpMessageConverter()).build(); } /** * Get mandatory configuration parameters. * @return The mandatory configuration keys required by this implementation */ public static final String[] getMandatoryConfigKeys() { return MANDATORY_CONFIG_KEYS; } @Override public List<? extends Exchange> suggestSampleExchanges(Service service, Operation operation, Resource contract, int number) throws Exception { String prompt = ""; if (service.getType() == ServiceType.REST) { prompt = preparePromptForOpenAPI(operation, contract, number); } else if (service.getType() == ServiceType.GRAPHQL) { prompt = preparePromptForGraphQL(operation, contract, number); } else if (service.getType() == ServiceType.EVENT) { prompt = preparePromptForAsyncAPI(operation, contract, number); } else if (service.getType() == ServiceType.GRPC) { prompt = preparePromptForGrpc(service, operation, contract, number); } log.debug("Asking OpenAI to suggest samples for this prompt: {}", prompt); final List<ChatMessage> messages = new ArrayList<>(); final ChatMessage assistantMessage = new ChatMessage(ChatMessageRole.ASSISTANT.value(), prompt); messages.add(assistantMessage); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder().model(model).messages(messages).n(1) .maxTokens(maxTokens).logitBias(new HashMap<>()).build(); // Build a full HttpEntity as we need to specify authentication headers. HttpEntity<ChatCompletionRequest> request = new HttpEntity<>(chatCompletionRequest, createAuthenticationHeaders()); ChatCompletionResult completionResult = restTemplate .exchange(apiUrl + "/v1/chat/completions", HttpMethod.POST, request, ChatCompletionResult.class).getBody(); if (completionResult != null) { ChatCompletionChoice choice = completionResult.getChoices().get(0); log.debug("Got this raw output from OpenAI: {}", choice.getMessage().getContent()); if (service.getType() == ServiceType.EVENT) { return AICopilotHelper.parseUnidirectionalEventTemplateOutput(choice.getMessage().getContent()); } else { return AICopilotHelper.parseRequestResponseTemplateOutput(service, operation, choice.getMessage().getContent()); } } // Return empty list. return new ArrayList<>(); } private String preparePromptForOpenAPI(Operation operation, Resource contract, int number) throws Exception { StringBuilder prompt = new StringBuilder( AICopilotHelper.getOpenAPIOperationPromptIntro(operation.getName(), number)); // Build a prompt reusing templates and elements from AICopilotHelper. prompt.append("\n"); prompt.append(AICopilotHelper.YAML_FORMATTING_PROMPT); prompt.append("\n"); prompt.append(AICopilotHelper.getRequestResponseExampleYamlFormattingDirective(1)); prompt.append(SECTION_DELIMITER); prompt.append(AICopilotHelper.removeTokensFromSpec(contract.getContent(), operation.getName())); return prompt.toString(); } private String preparePromptForGraphQL(Operation operation, Resource contract, int number) { StringBuilder prompt = new StringBuilder( AICopilotHelper.getGraphQLOperationPromptIntro(operation.getName(), number)); // We need to indicate the name or variables we want. if (DispatchStyles.QUERY_ARGS.equals(operation.getDispatcher())) { StringBuilder variablesList = new StringBuilder(); if (operation.getDispatcherRules().contains("&&")) { String[] variables = operation.getDispatcherRules().split("&&"); for (int i = 0; i < variables.length; i++) { String variable = variables[i]; variablesList.append("$").append(variable.trim()); if (i < variables.length - 1) { variablesList.append(", "); } } } else { variablesList.append("$").append(operation.getDispatcherRules()); } prompt.append("Use only '").append(variablesList).append("' as variable identifiers."); } // Build a prompt reusing templates and elements from AICopilotHelper. prompt.append("\n"); prompt.append(AICopilotHelper.YAML_FORMATTING_PROMPT); prompt.append("\n"); prompt.append(AICopilotHelper.getRequestResponseExampleYamlFormattingDirective(1)); prompt.append(SECTION_DELIMITER); prompt.append(contract.getContent()); return prompt.toString(); } private String preparePromptForAsyncAPI(Operation operation, Resource contract, int number) throws Exception { StringBuilder prompt = new StringBuilder( AICopilotHelper.getAsyncAPIOperationPromptIntro(operation.getName(), number)); // Build a prompt reusing templates and elements from AICopilotHelper. prompt.append("\n"); prompt.append(AICopilotHelper.YAML_FORMATTING_PROMPT); prompt.append("\n"); prompt.append(AICopilotHelper.getUnidirectionalEventExampleYamlFormattingDirective(1)); prompt.append(SECTION_DELIMITER); prompt.append(AICopilotHelper.removeTokensFromSpec(contract.getContent(), operation.getName())); return prompt.toString(); } private String preparePromptForGrpc(Service service, Operation operation, Resource contract, int number) throws Exception { StringBuilder prompt = new StringBuilder( AICopilotHelper.getGrpcOperationPromptIntro(service.getName(), operation.getName(), number)); // Build a prompt reusing templates and elements from AICopilotHelper. prompt.append("\n"); prompt.append(AICopilotHelper.YAML_FORMATTING_PROMPT); prompt.append("\n"); prompt.append(AICopilotHelper.getGrpcRequestResponseExampleYamlFormattingDirective(1)); prompt.append(SECTION_DELIMITER); prompt.append(contract.getContent()); return prompt.toString(); } private MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); converter.setObjectMapper(customObjectMapper()); return converter; } private static ObjectMapper customObjectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); mapper.addMixIn(ChatCompletionRequest.class, ChatCompletionRequestMixIn.class); return mapper; } private HttpHeaders createAuthenticationHeaders() { HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", "Bearer " + apiKey); return headers; } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((6191, 6224), 'com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value'), ((6326, 6463), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6326, 6455), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6326, 6428), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6326, 6394), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6326, 6389), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((6326, 6370), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((8499, 8558), 'io.github.microcks.util.DispatchStyles.QUERY_ARGS.equals')]
package quotes.responder.chatgptsentiment; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatCompletionResult; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessageRole; import com.theokanning.openai.service.OpenAiService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import quotes.common.Options; import quotes.common.model.Quote; import reactor.core.publisher.Mono; import java.util.List; import static quotes.utils.SentimentUtils.generatePrompt; /** * This class defines methods that use the ChatGPT web service to * analyze the sentiment of a famous {@link Quote} from the Bard. * * The {@code @Service} annotation enables the auto-detection of * implementation classes via classpath scanning (in this case {@link * Quote}). */ @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") @Service public class GPTSentimentService { /** * Debugging tag used by {@link Options}. */ private final String TAG = getClass().getSimpleName(); /** * Auto-wire the means to access ChatGPT using Spring * dependency injection. */ @Autowired private OpenAiService mOpenAiService; /** * Analyzes the sentiment of the given text and return * the sentiment as an {@link Quote} object. * * @param quoteM A {@link Mono} that emits a {@link Quote} whose * sentiment is analyzed * @return The {@link Quote} object updated to include the * sentiment analysis */ public Mono<Quote> analyzeSentiment(Mono<Quote> quoteM) { return quoteM .map(quote -> { // Create the ChatMessage containing the prompt. List<ChatMessage> messages = makePrompt(quote); // Send an HTTP request to ChatGPT to get the // ChatCompletionResult. var ccRequest = getResult(messages); // Set the sentiment for the Quote. setQuoteSentiment(quote, ccRequest); // Return the updated quote. return quote; }); } /** * Creates a {@link ChatMessage} containing the prompt. * * @param quote The {@link Quote} containing information * needed to make the prompt * @return A one-element {@link List} containing the prompt */ List<ChatMessage> makePrompt(Quote quote) { return List // Create the ChatMessage containing the prompt. .of(new ChatMessage (ChatMessageRole.SYSTEM.value(), generatePrompt(quote))); } /** * Uses ChatGPT to get a {@link ChatCompletionResult} from the * {@link List} of {@link ChatMessage} objects containing * the prompt. * * @param messages The {@link List} of {@link ChatMessage} * objects containing the prompt * @return The {@link ChatCompletionResult} returned from * ChatGPT */ ChatCompletionResult getResult (List<ChatMessage> messages) { var ccRequest = ChatCompletionRequest // Create the ChatCompletionRequest.Builder. .builder() // Specify the LLM model to use. .model("gpt-3.5-turbo") // Provide the prompt. .messages(messages) // Set the temperature, which controls how // deterministic the response is. .temperature(0.2) // Just return a single response. .n(1) // Build the ChatCompletionRequest. .build(); return mOpenAiService // Use the ChatCompletionRequest to get a // single ChatCompletionResult. .createChatCompletion(ccRequest); } /** * Sets the sentiment of the given {@link Quote}. * * @param quote The {@link Quote} whose sentiment is analyzed * @param ccResult The {@link ChatCompletionResult} that * contains the result from ChatGPT */ void setQuoteSentiment (Quote quote, ChatCompletionResult ccResult) { quote // Set the sentiment for the quote. .setSentiment(ccResult // Get the first (and only) response. .getChoices().get(0) // Get the sentiment content. .getMessage().getContent()); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value" ]
[((2746, 2776), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value')]
package net.devemperor.wristassist.activities; import static com.theokanning.openai.service.OpenAiService.defaultClient; import static com.theokanning.openai.service.OpenAiService.defaultObjectMapper; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.VibrationEffect; import android.os.Vibrator; import android.view.View; import android.widget.ImageButton; import android.widget.ProgressBar; import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.firebase.crashlytics.FirebaseCrashlytics; import com.jsibbold.zoomage.ZoomageView; import com.theokanning.openai.client.OpenAiApi; import com.theokanning.openai.image.CreateImageRequest; import com.theokanning.openai.image.Image; import com.theokanning.openai.image.ImageResult; import com.theokanning.openai.service.OpenAiService; import net.devemperor.wristassist.R; import net.devemperor.wristassist.database.ImageModel; import net.devemperor.wristassist.database.ImagesDatabaseHelper; import net.devemperor.wristassist.database.UsageDatabaseHelper; import net.devemperor.wristassist.util.Util; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.time.Duration; import java.util.Objects; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.jackson.JacksonConverterFactory; public class CreateImageActivity extends AppCompatActivity { SharedPreferences sp; UsageDatabaseHelper usageDatabaseHelper; ImagesDatabaseHelper imagesDatabaseHelper; OpenAiService service; Vibrator vibrator; ScrollView createImageSv; ProgressBar imagePb; TextView errorTv; ImageButton retryBtn; ZoomageView imageView; ImageButton shareBtn; TextView expiresInTv; ConstraintLayout saveDiscardBtns; String prompt; String model; String quality; String style; String size; ImageResult imageResult; Image image; Bitmap bitmap; ExecutorService thread; Timer timer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_create_image); sp = getSharedPreferences("net.devemperor.wristassist", MODE_PRIVATE); imagesDatabaseHelper = new ImagesDatabaseHelper(this); usageDatabaseHelper = new UsageDatabaseHelper(this); String apiKey = sp.getString("net.devemperor.wristassist.api_key", "noApiKey"); String apiHost = sp.getString("net.devemperor.wristassist.custom_server_host", "https://api.openai.com/"); ObjectMapper mapper = defaultObjectMapper(); // replaces all control chars (#10 @ GH) OkHttpClient client = defaultClient(apiKey.replaceAll("[^ -~]", ""), Duration.ofSeconds(120)).newBuilder().build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(apiHost) .client(client) .addConverterFactory(JacksonConverterFactory.create(mapper)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); OpenAiApi api = retrofit.create(OpenAiApi.class); service = new OpenAiService(api); vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); createImageSv = findViewById(R.id.create_image_sv); imagePb = findViewById(R.id.image_pb); errorTv = findViewById(R.id.error_image_tv); retryBtn = findViewById(R.id.retry_image_btn); imageView = findViewById(R.id.create_image_iv); shareBtn = findViewById(R.id.share_image_btn); expiresInTv = findViewById(R.id.expires_image_tv); saveDiscardBtns = findViewById(R.id.save_discard_image_btns); prompt = getIntent().getStringExtra("net.devemperor.wristassist.prompt"); model = sp.getBoolean("net.devemperor.wristassist.image_model", false) ? "dall-e-3" : "dall-e-2"; quality = sp.getBoolean("net.devemperor.wristassist.image_quality", false) ? "hd" : "standard"; style = sp.getBoolean("net.devemperor.wristassist.image_style", false) ? "natural" : "vivid"; size = sp.getBoolean("net.devemperor.wristassist.image_model", false) ? "1024x1024" : sp.getString("net.devemperor.wristassist.image_size", "1024x1024"); createAndDownloadImage(); createImageSv.requestFocus(); } @Override protected void onDestroy() { super.onDestroy(); timer.cancel(); if (thread != null) { thread.shutdownNow(); } } private void createAndDownloadImage() { imagePb.setVisibility(View.VISIBLE); errorTv.setVisibility(View.GONE); retryBtn.setVisibility(View.GONE); thread = Executors.newSingleThreadExecutor(); thread.execute(() -> { timer = new Timer(); try { CreateImageRequest cir = CreateImageRequest.builder() .responseFormat("url") .n(1) .prompt(prompt) .model(model) .quality(quality) .size(size) .style(style) .build(); imageResult = service.createImage(cir); image = imageResult.getData().get(0); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { long minutes = (imageResult.getCreated()*1000 + 60*60*1000 - System.currentTimeMillis()) / 60 / 1000; runOnUiThread(() -> { if (minutes <= 0) { expiresInTv.setVisibility(View.GONE); shareBtn.setVisibility(View.GONE); timer.cancel(); } else { expiresInTv.setText(getString(R.string.wristassist_image_expires_in, minutes)); } }); } }, 0, 60*1000); usageDatabaseHelper.edit(model, 1, Util.calcCostImage(model, quality, size)); OkHttpClient downloadClient = new OkHttpClient(); Request request = new Request.Builder().url(image.getUrl()).build(); Response response = downloadClient.newCall(request).execute(); if (!response.isSuccessful()) { throw new IOException("Unexpected code " + response); } assert response.body() != null; InputStream inputStream = response.body().byteStream(); bitmap = BitmapFactory.decodeStream(inputStream); if (bitmap == null) { throw new IOException("Bitmap is null"); } else { runOnUiThread(() -> { if (sp.getBoolean("net.devemperor.wristassist.vibrate", true)) { vibrator.vibrate(VibrationEffect.createOneShot(300, VibrationEffect.DEFAULT_AMPLITUDE)); } imageView.setImageBitmap(bitmap); imagePb.setVisibility(View.GONE); imageView.setVisibility(View.VISIBLE); shareBtn.setVisibility(View.VISIBLE); expiresInTv.setVisibility(View.VISIBLE); saveDiscardBtns.setVisibility(View.VISIBLE); }); } } catch (RuntimeException | IOException e) { FirebaseCrashlytics fc = FirebaseCrashlytics.getInstance(); fc.setCustomKey("settings", sp.getAll().toString()); fc.setUserId(sp.getString("net.devemperor.wristassist.userid", "null")); fc.recordException(e); fc.sendUnsentReports(); e.printStackTrace(); runOnUiThread(() -> { imagePb.setVisibility(View.GONE); errorTv.setVisibility(View.VISIBLE); retryBtn.setVisibility(View.VISIBLE); timer.cancel(); if (sp.getBoolean("net.devemperor.wristassist.vibrate", true)) { vibrator.vibrate(VibrationEffect.createWaveform(new long[]{50, 50, 50, 50, 50}, new int[]{-1, 0, -1, 0, -1}, -1)); } if (Objects.requireNonNull(e.getMessage()).contains("SocketTimeoutException")) { errorTv.setText(R.string.wristassist_timeout); } else if (e.getMessage().contains("API key")) { errorTv.setText(getString(R.string.wristassist_invalid_api_key_message)); } else if (e.getMessage().contains("rejected")) { errorTv.setText(R.string.wristassist_image_request_rejected); } else if (e.getMessage().contains("quota") || e.getMessage().contains("limit")) { errorTv.setText(R.string.wristassist_quota_exceeded); } else if (e.getMessage().contains("does not exist")) { errorTv.setText(R.string.wristassist_no_access); } else { errorTv.setText(R.string.wristassist_no_internet); } }); } }); } public void retry(View view) { createAndDownloadImage(); } public void shareImage(View view) { Intent intent = new Intent(this, QRCodeActivity.class); intent.putExtra("net.devemperor.wristassist.image_url", image.getUrl()); startActivity(intent); } public void saveImage(View view) { Toast.makeText(this, R.string.wristassist_saving, Toast.LENGTH_SHORT).show(); ImageModel imageModel; if (model.equals("dall-e-3")) { imageModel = new ImageModel(-1, prompt, image.getRevisedPrompt(), model, quality, size, style, imageResult.getCreated() * 1000, image.getUrl()); } else { imageModel = new ImageModel(-1, prompt, null, model, null, size, null, imageResult.getCreated() * 1000, image.getUrl()); } int id = imagesDatabaseHelper.add(imageModel); try { FileOutputStream out = openFileOutput("image_" + id + ".png", MODE_PRIVATE); bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } timer.cancel(); Intent data = new Intent(); data.putExtra("net.devemperor.wristassist.imageId", id); setResult(RESULT_OK, data); finish(); } public void discardImage(View view) { timer.cancel(); finish(); } }
[ "com.theokanning.openai.image.CreateImageRequest.builder" ]
[((5450, 5782), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((5450, 5749), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((5450, 5711), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((5450, 5675), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((5450, 5633), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((5450, 5595), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((5450, 5555), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((5450, 5525), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((9084, 9157), 'java.util.Objects.requireNonNull'), ((10410, 10486), 'android.widget.Toast.makeText')]
package com.npcvillagers.npcvillage.services; import com.google.gson.*; import com.microsoft.azure.cognitiveservices.vision.contentmoderator.*; import com.microsoft.azure.cognitiveservices.vision.contentmoderator.models.*; import com.npcvillagers.npcvillage.models.Npc; import com.theokanning.openai.service.OpenAiService; import com.theokanning.openai.moderation.Moderation; import com.theokanning.openai.moderation.ModerationRequest; import com.theokanning.openai.completion.chat.ChatCompletionChoice; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessageRole; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.FileReader; import java.io.IOException; import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class OpenAiApiHandler { @Autowired NpcFactory npcFactory; public OpenAiApiHandler(NpcFactory npcFactory) { this.npcFactory = npcFactory; } private static final String SEED_MESSAGE_SYSTEM = "You are a dungeon master's assistant for their creation of dungeons and dragons NPCs."; private static final String SEED_MESSAGE_USER_PATH = "src/main/resources/static/json/seedUserMessage.json"; private static final String SEED_MESSAGE_USER = getSeedUserMessage(); private static final String SEED_MESSAGE_ASSISTANT_PATH = "src/main/resources/static/json/seedAssistantMessage.json"; private static final String SEED_MESSAGE_ASSISTANT = getSeedAssistantMessage(); public Npc processNpc(Npc npc) { String userNpcJsonString = npcFactory.toPrettyJsonString(npc); long startTime = System.nanoTime(); // Ensure that the user's inputs comply with OpenAI content policies enforceContentPolicy(userNpcJsonString); // Call the OpenAI API and create a JSON format string String generatedNpcJsonString = generateOpenAiChatMessage(userNpcJsonString); Npc updatedNpc = npcFactory.updateNpcFromContent(npc, generatedNpcJsonString); long endTime = System.nanoTime(); // Calculate the elapsed time in seconds double elapsedTime = (endTime - startTime) / 1_000_000_000.0; System.out.println("Elapsed time: " + elapsedTime + " seconds"); return updatedNpc; } // We use the OpenAI moderations endpoint and the Azure Content Moderator to ensure what the user typed doesn't break content policy. Based on testing, the OpenAI moderations endpoint is not sufficient on its own in certain edge cases. We use it in concert with the Azure Moderator Client. private void enforceContentPolicy(String userNpcJsonString) { enforceOpenAiContentPolicy(userNpcJsonString); enforceAzureContentPolicy(userNpcJsonString); } private void enforceOpenAiContentPolicy(String userNpcJsonString) { String token = System.getenv("OPENAI_TOKEN"); if (token == null) { throw new RuntimeException("Error: OPENAI_TOKEN environment variable not set"); } OpenAiService service = null; try { // Set duration to 60 seconds to avoid a socket exception for long response times service = new OpenAiService(token, Duration.ofSeconds(60)); ModerationRequest moderationRequest = ModerationRequest.builder() .input(userNpcJsonString) .model("text-moderation-latest") .build(); List<Moderation> moderationResults = service.createModeration(moderationRequest).getResults(); // Check if any results were returned if (moderationResults.isEmpty()) { throw new RuntimeException("Error: No moderation results returned"); } Moderation moderationScore = moderationResults.get(0); // Check if the content is flagged by OpenAI if (moderationScore.isFlagged()) { // Throw an exception indicating content policy violation throw new IllegalArgumentException("Content violates the content policy. Please modify your NPC"); } } catch (Exception e) { throw new RuntimeException("Error enforcing OpenAI content policy", e); } finally { if (service != null) { service.shutdownExecutor(); } } } private void enforceAzureContentPolicy(String userNpcJsonString) { String azureModeratorEndpoint = System.getenv("AZURE_MODERATOR_ENDPOINT"); if (azureModeratorEndpoint == null) { throw new RuntimeException("Error: AZURE_MODERATOR_ENDPOINT environment variable not set"); } String azureModeratorSubscriptionKey = System.getenv("AZURE_MODERATOR_SUBSCRIPTION_KEY"); if (azureModeratorEndpoint == null) { throw new RuntimeException("Error: AZURE_MODERATOR_SUBSCRIPTION_KEY environment variable not set"); } try { // Create Azure Content Moderator client ContentModeratorClient azureModeratorClient = ContentModeratorManager.authenticate( AzureRegionBaseUrl.fromString(azureModeratorEndpoint), azureModeratorSubscriptionKey); // Detect the language of the text DetectedLanguage detectedLanguage = azureModeratorClient.textModerations().detectLanguage("text/plain", userNpcJsonString.getBytes()); if (detectedLanguage == null) { throw new RuntimeException("Failed to detect the language of the text"); } // Screen the text ScreenTextOptionalParameter screenTextOptionalParameter = new ScreenTextOptionalParameter().withLanguage(detectedLanguage.detectedLanguage()); Screen screen = azureModeratorClient.textModerations().screenText("text/plain", userNpcJsonString.getBytes(), screenTextOptionalParameter); // If there are any matched items in the Auto-detected language, PII or Classification categories. if ((screen.pII() != null) || (screen.classification() != null && (screen.classification().reviewRecommended()))) { throw new IllegalArgumentException("Content violates the content policy. Please modify your NPC"); } } catch (APIErrorException e) { // Handle API exceptions here throw new RuntimeException("An error occurred while screening the content with Azure Content Moderator", e); } catch (Exception e) { // Handle other exceptions here throw new RuntimeException("An unexpected error occurred", e); } } private String generateOpenAiChatMessage(String userNpcJsonString) { String token = System.getenv("OPENAI_TOKEN"); if (token == null) { throw new RuntimeException("Error: OPENAI_TOKEN environment variable not set"); } OpenAiService service = null; try { // Set duration to 60 seconds to avoid a socket exception for long response times service = new OpenAiService(token, Duration.ofSeconds(60)); // Seed the chat with system context, example input, and example output, and add the user's Npc to the messages final List<ChatMessage> messages = new ArrayList<>(); final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), SEED_MESSAGE_SYSTEM); final ChatMessage seedUserMessage = new ChatMessage(ChatMessageRole.USER.value(), SEED_MESSAGE_USER); final ChatMessage seedAssistantMessage = new ChatMessage(ChatMessageRole.ASSISTANT.value(), SEED_MESSAGE_ASSISTANT); final ChatMessage userNpcRequestMessage = new ChatMessage(ChatMessageRole.USER.value(), userNpcJsonString); messages.add(systemMessage); messages.add(seedUserMessage); messages.add(seedAssistantMessage); messages.add(userNpcRequestMessage); // Send the API request ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder() .model("gpt-3.5-turbo") .messages(messages) .n(1) .temperature(0.8) .maxTokens(1000) .logitBias(new HashMap<>()) .build(); // Extract the message content of the response List<ChatCompletionChoice> choices = service.createChatCompletion(chatCompletionRequest).getChoices(); if (choices.isEmpty()) { throw new RuntimeException("Error: No response from OpenAI"); } String content = choices.get(0).getMessage().getContent(); return content; } catch (Exception e) { throw new RuntimeException("Error generating OpenAI chat message", e); } finally { if (service != null) { service.shutdownExecutor(); } } } private static String getSeedUserMessage() { String prettyJsonString = getPrettyJsonString(SEED_MESSAGE_USER_PATH); String originalMessage = "Generate a JSON-formatted NPC (Non-Player Character) for use in a Dungeons and Dragons campaign. The JSON should ONLY contain the following fields: \"name\", \"age\", \"voice\", \"description\", \"personality\", \"motivation\", \"ideal\", \"bond\", \"flaw\", and \"history\". Please ensure that the fields \"ideal\", \"bond\", and \"flaw\" are described from a first-person point of view. IMPORTANT: Do not include any other fields beyond the ones specifically mentioned here, such as species, subspecies, gender, alignment or any other. IMPORTANT: The NPC's \"history\" or \"motivation\" should clearly reflect the given \"playerRelationship\" input, which describes the relationship of the NPC to the players. This relationship should be clearly visible in the NPC's backstory or motivation. IMPORTANT: Do not mis-gender your creation. IMPORTANT: Give the NPC's \"age\" in years.\n\n"; String fullMessage = originalMessage + prettyJsonString; return fullMessage; } private static String getSeedAssistantMessage() { return getPrettyJsonString(SEED_MESSAGE_ASSISTANT_PATH); } private static String getPrettyJsonString(String filePath) { try (FileReader reader = new FileReader(filePath)) { // create a Gson instance with pretty printing Gson gson = new GsonBuilder().setPrettyPrinting().create(); // parse the JSON file into a JsonArray JsonArray jsonArray = JsonParser.parseReader(reader).getAsJsonArray(); // convert the JsonArray into a pretty printed string String prettyJsonString = gson.toJson(jsonArray); // Replace escaped quotes with actual quotes prettyJsonString = prettyJsonString.replace("\\\"", "\""); return prettyJsonString; } catch (IOException e) { e.printStackTrace(); } return null; } public NpcFactory getNpcFactory() { return npcFactory; } public void setNpcFactory(NpcFactory npcFactory) { this.npcFactory = npcFactory; } }
[ "com.theokanning.openai.moderation.ModerationRequest.builder", "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value" ]
[((3501, 3656), 'com.theokanning.openai.moderation.ModerationRequest.builder'), ((3501, 3627), 'com.theokanning.openai.moderation.ModerationRequest.builder'), ((3501, 3574), 'com.theokanning.openai.moderation.ModerationRequest.builder'), ((7638, 7668), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((7756, 7784), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((7875, 7908), 'com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value'), ((8005, 8033), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value')]
package openai; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import com.amazonaws.services.lambda.runtime.LambdaLogger; import com.theokanning.openai.completion.chat.ChatCompletionChoice; import com.theokanning.openai.completion.chat.ChatCompletionChunk; 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; import io.reactivex.Flowable; import utils.BasicUtils; import utils.LambdaLoggerImpl; public class ChatGPT { private static final Logger logger = Logger.getLogger(ChatGPT.class.getName()); static { logger.setLevel(BasicUtils.logLevel()); } private final String PREFIX = this.getClass().getName() + " "; public ChatGPT() { } public String converse(String textBody) { logger.log(Level.INFO , "Conversing with ChatGPT: "+textBody); String token = System.getenv("CHATGPT_ENV"); OpenAiService service = new OpenAiService(token); final List<ChatMessage> messages = new ArrayList<>(); final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), textBody); messages.add(systemMessage); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder().model("gpt-3.5-turbo") .messages(messages).n(1).maxTokens(50).logitBias(new HashMap<>()).build(); Flowable<ChatCompletionChunk> streamChatCompletion = service.streamChatCompletion(chatCompletionRequest); final StringBuilder sb = new StringBuilder(); final StringBuilder finishReason = new StringBuilder(); streamChatCompletion.blockingForEach(b -> { List<ChatCompletionChoice> choices = b.getChoices(); choices.forEach(e -> { sb.append(e.getMessage().getContent()); finishReason.delete(0, finishReason.length()); finishReason.append(e.getFinishReason()); }); }); String str = sb.toString(); logger.log(Level.INFO , "finished responding: "+str); return str; } public static void main(String[] args) { ChatGPT ai = new ChatGPT(); String converse = ai.converse("What is the difference between bonds and stocks"); System.out.println("======"); System.out.println(converse); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1263, 1293), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((1389, 1521), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1389, 1513), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1389, 1486), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1389, 1472), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1389, 1467), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1389, 1443), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
/* * #%L * Script Editor and Interpreter for SciJava script languages. * %% * Copyright (C) 2009 - 2024 SciJava developers. * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * #L% */ package org.scijava.ui.swing.script; import java.awt.Color; import java.awt.Component; import java.awt.Cursor; import java.awt.Desktop; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics2D; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.dnd.DnDConstants; import java.awt.dnd.DragGestureEvent; import java.awt.dnd.DragGestureListener; import java.awt.dnd.DragSource; import java.awt.dnd.DragSourceDragEvent; import java.awt.dnd.DragSourceDropEvent; import java.awt.dnd.DragSourceEvent; import java.awt.dnd.DragSourceListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.event.ItemEvent; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.CharArrayWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.PrintWriter; import java.io.RandomAccessFile; import java.io.Reader; import java.io.StringReader; import java.io.Writer; import java.net.URL; import java.net.URLEncoder; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.TreeMap; import java.util.Vector; import java.util.concurrent.ExecutionException; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; import java.util.zip.ZipException; import javax.script.ScriptEngine; import javax.script.ScriptException; import javax.swing.*; import javax.swing.border.BevelBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultHighlighter; import javax.swing.text.DefaultHighlighter.DefaultHighlightPainter; import javax.swing.text.Position; import javax.swing.tree.TreePath; 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; import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory; import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; import org.fife.ui.rsyntaxtextarea.Theme; import org.fife.ui.rsyntaxtextarea.TokenMakerFactory; import org.fife.ui.rtextarea.ClipboardHistory; import org.fife.ui.rtextarea.Macro; import org.fife.ui.rtextarea.RTextArea; import org.fife.ui.rtextarea.RTextAreaEditorKit; import org.fife.ui.rtextarea.RTextAreaEditorKit.SetReadOnlyAction; import org.fife.ui.rtextarea.RTextAreaEditorKit.SetWritableAction; import org.scijava.Context; import org.scijava.app.AppService; import org.scijava.batch.BatchService; import org.scijava.command.CommandService; import org.scijava.event.ContextDisposingEvent; import org.scijava.event.EventHandler; import org.scijava.io.IOService; import org.scijava.log.LogService; import org.scijava.module.ModuleException; import org.scijava.module.ModuleService; import org.scijava.options.OptionsService; import org.scijava.platform.PlatformService; import org.scijava.plugin.Parameter; import org.scijava.plugin.PluginInfo; import org.scijava.plugin.PluginService; import org.scijava.plugins.scripting.java.JavaEngine; import org.scijava.prefs.PrefService; import org.scijava.script.ScriptHeaderService; import org.scijava.script.ScriptInfo; import org.scijava.script.ScriptLanguage; import org.scijava.script.ScriptModule; import org.scijava.script.ScriptService; import org.scijava.thread.ThreadService; import org.scijava.ui.CloseConfirmable; import org.scijava.ui.UIService; import org.scijava.ui.swing.script.autocompletion.ClassUtil; import org.scijava.ui.swing.script.commands.ChooseFontSize; import org.scijava.ui.swing.script.commands.ChooseTabSize; import org.scijava.ui.swing.script.commands.GitGrep; import org.scijava.ui.swing.script.commands.KillScript; import org.scijava.util.FileUtils; import org.scijava.util.MiscUtils; import org.scijava.util.POM; import org.scijava.util.PlatformUtils; import org.scijava.util.Types; import org.scijava.widget.FileWidget; import com.formdev.flatlaf.FlatLaf; /** * A versatile script editor for SciJava applications. * <p> * Based on the powerful SciJava scripting framework and the * <a href="http://fifesoft.com/rsyntaxtextarea/">RSyntaxTextArea</a> library, * this text editor lets users script their way to success. Thanks to the * <a href="https://github.com/scijava/scripting-java">Java backend for SciJava * scripting</a>, it is even possible to develop Java plugins in the editor. * </p> * * @author Johannes Schindelin * @author Jonathan Hale * @author Albert Cardona * @author Tiago Ferreira */ public class TextEditor extends JFrame implements ActionListener, ChangeListener, CloseConfirmable, DocumentListener { private static final Set<String> TEMPLATE_PATHS = new HashSet<>(); // private static final int BORDER_SIZE = 4; public static final String AUTO_IMPORT_PREFS = "script.editor.AutoImport"; public static final String WINDOW_HEIGHT = "script.editor.height"; public static final String WINDOW_WIDTH = "script.editor.width"; public static final int DEFAULT_WINDOW_WIDTH = 800; public static final int DEFAULT_WINDOW_HEIGHT = 600; public static final String MAIN_DIV_LOCATION = "script.editor.main.divLocation"; public static final String TAB_DIV_LOCATION = "script.editor.tab.divLocation"; public static final String TAB_DIV_ORIENTATION = "script.editor.tab.divOrientation"; public static final String REPL_DIV_LOCATION = "script.editor.repl.divLocation"; public static final String LAST_LANGUAGE = "script.editor.lastLanguage"; static { // Add known script template paths. addTemplatePath("script_templates"); // This path interferes with javadoc generation but is preserved for // backwards compatibility addTemplatePath("script-templates"); } private static AbstractTokenMakerFactory tokenMakerFactory = null; private JTabbedPane tabbed; private JMenuItem newFile, open, save, saveas, compileAndRun, compile, close, undo, redo, cut, copy, paste, find, selectAll, kill, gotoLine, makeJar, makeJarWithSource, removeUnusedImports, sortImports, removeTrailingWhitespace, findNext, findPrevious, openHelp, addImport, nextError, previousError, openHelpWithoutFrames, nextTab, previousTab, runSelection, extractSourceJar, askChatGPTtoGenerateCode, openSourceForClass, //openSourceForMenuItem, // this never had an actionListener!?? openMacroFunctions, decreaseFontSize, increaseFontSize, chooseFontSize, chooseTabSize, gitGrep, replaceTabsWithSpaces, replaceSpacesWithTabs, zapGremlins,openClassOrPackageHelp; private RecentFilesMenuItem openRecent; private JMenu editMenu, gitMenu, tabsMenu, fontSizeMenu, tabSizeMenu, toolsMenu, runMenu; private int tabsMenuTabsStart; private Set<JMenuItem> tabsMenuItems; private FindAndReplaceDialog findDialog; private JCheckBoxMenuItem autoSave, wrapLines, tabsEmulated, autoImport, autocompletion, fallbackAutocompletion, keylessAutocompletion, markOccurences, paintTabs, whiteSpace, marginLine, lockPane; private ButtonGroup themeRadioGroup; private JTextArea errorScreen = new JTextArea(); private final FileSystemTree tree; private final JSplitPane body; private int compileStartOffset; private Position compileStartPosition; private ErrorHandler errorHandler; private boolean respectAutoImports; private String activeTheme; private int[] panePositions; @Parameter private Context context; @Parameter private LogService log; @Parameter private ModuleService moduleService; @Parameter private PlatformService platformService; @Parameter private IOService ioService; @Parameter private CommandService commandService; @Parameter private ScriptService scriptService; @Parameter private PluginService pluginService; @Parameter private ScriptHeaderService scriptHeaderService; @Parameter private UIService uiService; @Parameter private PrefService prefService; @Parameter private ThreadService threadService; @Parameter private AppService appService; @Parameter private BatchService batchService; @Parameter(required = false) private OptionsService optionsService; private Map<ScriptLanguage, JRadioButtonMenuItem> languageMenuItems; private JRadioButtonMenuItem noneLanguageItem; private EditableScriptInfo scriptInfo; private ScriptModule module; private boolean incremental = false; private DragSource dragSource; private boolean layoutLoading = true; private OutlineTreePanel sourceTreePanel; protected final CommandPalette cmdPalette; public static final ArrayList<TextEditor> instances = new ArrayList<>(); public static final ArrayList<Context> contexts = new ArrayList<>(); public TextEditor(final Context context) { super("Script Editor"); instances.add(this); contexts.add(context); context.inject(this); initializeTokenMakers(); // NB: All panes must be initialized before menus are assembled! tabbed = GuiUtils.getJTabbedPane(); tree = new FileSystemTree(log); final JTabbedPane sideTabs = GuiUtils.getJTabbedPane(); sideTabs.addTab("File Explorer", new FileSystemTreePanel(tree, context)); sideTabs.addTab("Outline", sourceTreePanel = new OutlineTreePanel()); body = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sideTabs, tabbed); // These items are dynamic and need to be initialized before EditorPane creation initializeDynamicMenuComponents(); // -- BEGIN MENUS -- // Initialize menu final int ctrl = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); final int shift = ActionEvent.SHIFT_MASK; final JMenuBar mbar = new JMenuBar(); setJMenuBar(mbar); // -- File menu -- final JMenu file = new JMenu("File"); file.setMnemonic(KeyEvent.VK_F); newFile = addToMenu(file, "New", KeyEvent.VK_N, ctrl); newFile.setMnemonic(KeyEvent.VK_N); open = addToMenu(file, "Open...", KeyEvent.VK_O, ctrl); open.setMnemonic(KeyEvent.VK_O); openRecent = new RecentFilesMenuItem(prefService, this); openRecent.setMnemonic(KeyEvent.VK_R); file.add(openRecent); file.addSeparator(); save = addToMenu(file, "Save", KeyEvent.VK_S, ctrl); save.setMnemonic(KeyEvent.VK_S); saveas = addToMenu(file, "Save As...", KeyEvent.VK_S, ctrl + shift); saveas.setMnemonic(KeyEvent.VK_A); file.addSeparator(); makeJar = addToMenu(file, "Export as JAR...", 0, 0); makeJar.setMnemonic(KeyEvent.VK_E); makeJarWithSource = addToMenu(file, "Export as JAR (With Source)...", 0, 0); makeJarWithSource.setMnemonic(KeyEvent.VK_X); file.addSeparator(); lockPane = new JCheckBoxMenuItem("Lock (Make Read Only)"); lockPane.setToolTipText("Protects file from accidental editing"); file.add(lockPane); lockPane.addActionListener(e -> { if (lockPane.isSelected()) { new SetReadOnlyAction().actionPerformedImpl(e, getEditorPane()); } else { new SetWritableAction().actionPerformedImpl(e, getEditorPane()); } }); JMenuItem jmi = new JMenuItem("Revert..."); jmi.addActionListener(e -> { if (getEditorPane().isLocked()) { error("File is currently locked."); return; } final File f = getEditorPane().getFile(); if (f == null || !f.exists()) { error(getEditorPane().getFileName() + "\nhas not been saved or its file is not available."); } else { reloadRevert("Revert to Saved File? Any unsaved changes will be lost.", "Revert"); } }); file.add(jmi); file.addSeparator(); jmi = new JMenuItem("Show in System Explorer"); jmi.addActionListener(e -> { final File f = getEditorPane().getFile(); if (f == null || !f.exists()) { error(getEditorPane().getFileName() + "\nhas not been saved or its file is not available."); } else { try { Desktop.getDesktop().open(f.getParentFile()); } catch (final Exception | Error ignored) { error(getEditorPane().getFileName() + "\ndoes not seem to be accessible."); } } }); file.add(jmi); file.addSeparator(); close = addToMenu(file, "Close", KeyEvent.VK_W, ctrl); mbar.add(file); // -- Edit menu -- editMenu = new JMenu("Edit"); // cannot be populated here. see #assembleEditMenu() editMenu.setMnemonic(KeyEvent.VK_E); mbar.add(editMenu); // -- Language menu -- languageMenuItems = new LinkedHashMap<>(); final Set<Integer> usedShortcuts = new HashSet<>(); final JMenu languages = new JMenu("Language"); languages.setMnemonic(KeyEvent.VK_L); final ButtonGroup group = new ButtonGroup(); // get list of languages, and sort them by name final ArrayList<ScriptLanguage> list = new ArrayList<>(scriptService.getLanguages()); Collections.sort(list, (l1, l2) -> { final String name1 = l1.getLanguageName(); final String name2 = l2.getLanguageName(); return MiscUtils.compare(name1, name2); }); list.add(null); final Map<String, ScriptLanguage> languageMap = new HashMap<>(); for (final ScriptLanguage language : list) { final String name = language == null ? "None" : language.getLanguageName(); languageMap.put(name, language); final JRadioButtonMenuItem item = new JRadioButtonMenuItem(name); if (language == null) { noneLanguageItem = item; } else { languageMenuItems.put(language, item); } int shortcut = -1; for (final char ch : name.toCharArray()) { final int keyCode = KeyStroke.getKeyStroke(ch, 0).getKeyCode(); if (usedShortcuts.contains(keyCode)) continue; shortcut = keyCode; usedShortcuts.add(shortcut); break; } if (shortcut > 0) item.setMnemonic(shortcut); item.addActionListener(e -> setLanguage(language, true)); group.add(item); languages.add(item); } noneLanguageItem.setSelected(true); mbar.add(languages); // -- Templates menu -- final JMenu templates = new JMenu("Templates"); templates.setMnemonic(KeyEvent.VK_T); addTemplates(templates); mbar.add(templates); // -- Run menu -- runMenu = new JMenu("Run"); runMenu.setMnemonic(KeyEvent.VK_R); compileAndRun = addToMenu(runMenu, "Compile and Run", KeyEvent.VK_R, ctrl); compileAndRun.setMnemonic(KeyEvent.VK_R); runSelection = addToMenu(runMenu, "Run Selected Code", KeyEvent.VK_R, ctrl | shift); runSelection.setMnemonic(KeyEvent.VK_S); compile = addToMenu(runMenu, "Compile", KeyEvent.VK_C, ctrl | shift); compile.setMnemonic(KeyEvent.VK_C); autoSave = new JCheckBoxMenuItem("Auto-save Before Compiling"); runMenu.add(autoSave); runMenu.addSeparator(); nextError = addToMenu(runMenu, "Next Error", KeyEvent.VK_F4, 0); nextError.setMnemonic(KeyEvent.VK_N); previousError = addToMenu(runMenu, "Previous Error", KeyEvent.VK_F4, shift); previousError.setMnemonic(KeyEvent.VK_P); final JMenuItem clearHighlights = new JMenuItem("Clear Marked Errors"); clearHighlights.addActionListener(e -> getEditorPane().getErrorHighlighter().reset()); runMenu.add(clearHighlights); runMenu.addSeparator(); kill = addToMenu(runMenu, "Kill Running Script...", 0, 0); kill.setMnemonic(KeyEvent.VK_K); kill.setEnabled(false); mbar.add(runMenu); // -- Tools menu -- toolsMenu = new JMenu("Tools"); toolsMenu.setMnemonic(KeyEvent.VK_O); cmdPalette = new CommandPalette(this); cmdPalette.install(toolsMenu); GuiUtils.addMenubarSeparator(toolsMenu, "Imports:"); addImport = addToMenu(toolsMenu, "Add Import...", 0, 0); addImport.setMnemonic(KeyEvent.VK_I); respectAutoImports = prefService.getBoolean(getClass(), AUTO_IMPORT_PREFS, false); autoImport = new JCheckBoxMenuItem("Auto-import (Deprecated)", respectAutoImports); autoImport.setToolTipText("Automatically imports common classes before running code"); autoImport.addItemListener(e -> { respectAutoImports = e.getStateChange() == ItemEvent.SELECTED; prefService.put(getClass(), AUTO_IMPORT_PREFS, respectAutoImports); if (respectAutoImports) write("Auto-imports on. Lines associated with execution errors cannot be marked"); else write("Auto-imports off. Lines associated with execution errors can be marked"); }); toolsMenu.add(autoImport); removeUnusedImports = addToMenu(toolsMenu, "Remove Unused Imports", 0, 0); removeUnusedImports.setMnemonic(KeyEvent.VK_U); sortImports = addToMenu(toolsMenu, "Sort Imports", 0, 0); sortImports.setMnemonic(KeyEvent.VK_S); GuiUtils.addMenubarSeparator(toolsMenu, "Source & APIs:"); extractSourceJar = addToMenu(toolsMenu, "Extract Source Jar...", 0, 0); extractSourceJar.setMnemonic(KeyEvent.VK_E); openSourceForClass = addToMenu(toolsMenu, "Open Java File for Class...", 0, 0); openSourceForClass.setMnemonic(KeyEvent.VK_J); //openSourceForMenuItem = addToMenu(toolsMenu, "Open Java File for Menu Item...", 0, 0); //openSourceForMenuItem.setMnemonic(KeyEvent.VK_M); GuiUtils.addMenubarSeparator(toolsMenu, "chatGPT"); askChatGPTtoGenerateCode = addToMenu(toolsMenu, "Ask chatGPT...", 0, 0); addScritpEditorMacroCommands(toolsMenu); mbar.add(toolsMenu); // -- Git menu -- gitMenu = new JMenu("Git"); gitMenu.setMnemonic(KeyEvent.VK_G); /* showDiff = addToMenu(gitMenu, "Show diff...", 0, 0); showDiff.setMnemonic(KeyEvent.VK_D); commit = addToMenu(gitMenu, "Commit...", 0, 0); commit.setMnemonic(KeyEvent.VK_C); */ gitGrep = addToMenu(gitMenu, "Grep...", 0, 0); gitGrep.setMnemonic(KeyEvent.VK_G); mbar.add(gitMenu); // -- Window Menu (previously labeled as Tabs menu -- tabsMenu = new JMenu("Window"); tabsMenu.setMnemonic(KeyEvent.VK_W); GuiUtils.addMenubarSeparator(tabsMenu, "Panes:"); // Assume initial status from prefs or panel visibility final JCheckBoxMenuItem jcmi1 = new JCheckBoxMenuItem("Side Pane", prefService.getInt(getClass(), MAIN_DIV_LOCATION, body.getDividerLocation()) > 0 || isLeftPaneExpanded(body)); jcmi1.addItemListener(e -> collapseSplitPane(0, !jcmi1.isSelected())); tabsMenu.add(jcmi1); // Console not initialized. Assume it is displayed if no prefs read final JCheckBoxMenuItem jcmi2 = new JCheckBoxMenuItem("Console", prefService.getInt(getClass(), TAB_DIV_LOCATION, 1) > 0); jcmi2.addItemListener(e -> collapseSplitPane(1, !jcmi2.isSelected())); tabsMenu.add(jcmi2); final JMenuItem mi = new JMenuItem("Reset Layout..."); mi.addActionListener(e -> { if (confirm("Reset Location of Console and File Explorer?", "Reset Layout?", "Reset")) { resetLayout(); jcmi1.setSelected(true); jcmi2.setSelected(true); } }); tabsMenu.add(mi); GuiUtils.addMenubarSeparator(tabsMenu, "Tabs:"); nextTab = addToMenu(tabsMenu, "Next Tab", KeyEvent.VK_PAGE_DOWN, ctrl); nextTab.setMnemonic(KeyEvent.VK_N); previousTab = addToMenu(tabsMenu, "Previous Tab", KeyEvent.VK_PAGE_UP, ctrl); previousTab.setMnemonic(KeyEvent.VK_P); tabsMenu.addSeparator(); tabsMenuTabsStart = tabsMenu.getItemCount(); tabsMenuItems = new HashSet<>(); mbar.add(tabsMenu); // -- Options menu -- final JMenu options = new JMenu("Options"); options.setMnemonic(KeyEvent.VK_O); // Font adjustments GuiUtils.addMenubarSeparator(options, "Font:"); decreaseFontSize = addToMenu(options, "Decrease Font Size", KeyEvent.VK_MINUS, ctrl); decreaseFontSize.setMnemonic(KeyEvent.VK_D); increaseFontSize = addToMenu(options, "Increase Font Size", KeyEvent.VK_PLUS, ctrl); increaseFontSize.setMnemonic(KeyEvent.VK_C); fontSizeMenu = new JMenu("Font Size"); fontSizeMenu.setMnemonic(KeyEvent.VK_Z); final boolean[] fontSizeShortcutUsed = new boolean[10]; final ButtonGroup buttonGroup = new ButtonGroup(); for (final int size : new int[] { 8, 10, 12, 16, 20, 28, 42 }) { final JRadioButtonMenuItem item = new JRadioButtonMenuItem("" + size + " pt"); item.addActionListener(event -> setFontSize(size)); for (final char c : ("" + size).toCharArray()) { final int digit = c - '0'; if (!fontSizeShortcutUsed[digit]) { item.setMnemonic(KeyEvent.VK_0 + digit); fontSizeShortcutUsed[digit] = true; break; } } buttonGroup.add(item); fontSizeMenu.add(item); } chooseFontSize = new JRadioButtonMenuItem("Other...", false); chooseFontSize.setMnemonic(KeyEvent.VK_O); chooseFontSize.addActionListener(this); buttonGroup.add(chooseFontSize); fontSizeMenu.add(chooseFontSize); options.add(fontSizeMenu); GuiUtils.addMenubarSeparator(options, "Indentation:"); tabsEmulated = new JCheckBoxMenuItem("Indent Using Spaces"); tabsEmulated.setMnemonic(KeyEvent.VK_S); tabsEmulated.addItemListener(e -> setTabsEmulated(tabsEmulated.getState())); options.add(tabsEmulated); tabSizeMenu = new JMenu("Tab Width"); tabSizeMenu.setMnemonic(KeyEvent.VK_T); final ButtonGroup bg = new ButtonGroup(); for (final int size : new int[] { 2, 4, 8 }) { final JRadioButtonMenuItem item = new JRadioButtonMenuItem("" + size); item.addActionListener(event -> { getEditorPane().setTabSize(size); updateTabAndFontSize(false); }); item.setMnemonic(KeyEvent.VK_0 + (size % 10)); bg.add(item); tabSizeMenu.add(item); } chooseTabSize = new JRadioButtonMenuItem("Other...", false); chooseTabSize.setMnemonic(KeyEvent.VK_O); chooseTabSize.addActionListener(this); bg.add(chooseTabSize); tabSizeMenu.add(chooseTabSize); options.add(tabSizeMenu); replaceSpacesWithTabs = addToMenu(options, "Replace Spaces With Tabs", 0, 0); replaceTabsWithSpaces = addToMenu(options, "Replace Tabs With Spaces", 0, 0); GuiUtils.addMenubarSeparator(options, "View:"); options.add(markOccurences); options.add(paintTabs); options.add(marginLine); options.add(whiteSpace); options.add(wrapLines); options.add(applyThemeMenu()); GuiUtils.addMenubarSeparator(options, "Code Completions:"); options.add(autocompletion); options.add(keylessAutocompletion); options.add(fallbackAutocompletion); options.addSeparator(); appendPreferences(options); mbar.add(options); mbar.add(helpMenu()); // -- END MENUS -- // add tab-related listeners tabbed.addChangeListener(this); sideTabs.addChangeListener(e -> { if (sideTabs.getSelectedIndex() == 1) sourceTreePanel.rebuildSourceTree(getTab(tabbed.getSelectedIndex()).editorPane); }); // Add the editor and output area new FileDrop(tabbed, files -> { final ArrayList<File> filteredFiles = new ArrayList<>(); assembleFlatFileCollection(filteredFiles, files); if (filteredFiles.isEmpty()) { warn("None of the dropped file(s) seems parseable."); return; } if (filteredFiles.size() < 10 || confirm("Confirm loading of " + filteredFiles.size() + " items?", "Confirm?", "Load")) { filteredFiles.forEach(f -> open(f)); } }); open(null); // make sure the editor pane is added getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); // Tweaks for JSplitPane body.setOneTouchExpandable(true); body.addPropertyChangeListener(evt -> { if ("dividerLocation".equals(evt.getPropertyName())) saveWindowSizeToPrefs(); }); // Tweaks for FileSystemTree tree.addTopLevelFoldersFrom(getEditorPane().loadFolders()); // Restore top-level directories dragSource = new DragSource(); dragSource.createDefaultDragGestureRecognizer(tree, DnDConstants.ACTION_COPY, new DragAndDrop()); tree.ignoreExtension("class"); tree.setMinimumSize(new Dimension(200, 600)); tree.addLeafListener(f -> { final String name = f.getName(); final int idot = name.lastIndexOf('.'); if (idot > -1) { final String ext = name.substring(idot + 1); final ScriptLanguage lang = scriptService.getLanguageByExtension(ext); if (null != lang) { open(f); return; } } if (isBinary(f)) { log.debug("isBinary: " + true); try { final Object o = ioService.open(f.getAbsolutePath()); // Open in whatever way possible if (null != o) uiService.show(o); else error("Could not open the file at\n" + f.getAbsolutePath()); return; } catch (final Exception e) { log.error(e); error("Could not open file at\n" + f); } } // Ask: if (confirm("Really try to open file " + name + " in a tab?", "Confirm", "Open")) { open(f); } }); getContentPane().add(body); // for Eclipse and MS Visual Studio lovers addAccelerator(compileAndRun, KeyEvent.VK_F11, 0, true); addAccelerator(compileAndRun, KeyEvent.VK_F5, 0, true); compileAndRun.setToolTipText("Also triggered by F5 or F11"); addAccelerator(nextTab, KeyEvent.VK_PAGE_DOWN, ctrl, true); addAccelerator(previousTab, KeyEvent.VK_PAGE_UP, ctrl, true); addAccelerator(increaseFontSize, KeyEvent.VK_EQUALS, ctrl | shift, true); // make sure that the window is not closed by accident addWindowListener(new WindowAdapter() { @Override public void windowClosing(final WindowEvent e) { if (!confirmClose()) return; tree.destroy(); // Necessary to prevent memory leaks for (final DragSourceListener l : dragSource.getDragSourceListeners()) { dragSource.removeDragSourceListener(l); } dragSource = null; getTab().destroy(); cmdPalette.dispose(); dispose(); } }); addWindowFocusListener(new WindowAdapter() { @Override public void windowGainedFocus(final WindowEvent e) { checkForOutsideChanges(); } }); // Tweaks for Console errorScreen.setFont(getEditorPane().getFont()); errorScreen.setEditable(false); errorScreen.setLineWrap(false); applyConsolePopupMenu(errorScreen); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); try { threadService.invoke(() -> { pack(); body.setDividerLocation(0.2); // Important!: will be read as prefs. default getTab().setREPLVisible(false); loadWindowSizePreferences(); pack(); }); } catch (final Exception ie) { /* ignore */ log.debug(ie); } findDialog = new FindAndReplaceDialog(this); // Save the layout when window is resized. addComponentListener(new ComponentAdapter() { @Override public void componentResized(final ComponentEvent e) { saveWindowSizeToPrefs(); } }); setLocationRelativeTo(null); // center on screen // HACK: Avoid weird macOS bug where window becomes tiny // in bottom left corner if centered while maximized. int y = getLocation().y - 11; if (y < 0) y = 0; setLocation(getLocation().x, y); open(null); final EditorPane editorPane = getEditorPane(); // If dark L&F and using the default theme, assume 'dark' theme applyTheme((GuiUtils.isDarkLaF() && "default".equals(editorPane.themeName())) ? "dark" : editorPane.themeName(), true); // Ensure font sizes are consistent across all panels setFontSize(getEditorPane().getFontSize()); // Ensure menu commands are up-to-date updateUI(true); // Store locations of splitpanes panePositions = new int[]{body.getDividerLocation(), getTab().getDividerLocation()}; editorPane.requestFocus(); } private void resetLayout() { body.setDividerLocation(.2d); getTab().setOrientation(JSplitPane.VERTICAL_SPLIT); getTab().setDividerLocation((incremental) ? .7d : .75d); if (incremental) getTab().getScreenAndPromptSplit().setDividerLocation(.5d); getTab().setREPLVisible(incremental); pack(); } private void assembleEditMenu() { // requires an existing instance of an EditorPane final int ctrl = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); final int shift = ActionEvent.SHIFT_MASK; undo = addToMenu(editMenu, "Undo", KeyEvent.VK_Z, ctrl); redo = addToMenu(editMenu, "Redo", KeyEvent.VK_Y, ctrl); editMenu.addSeparator(); selectAll = addToMenu(editMenu, "Select All", KeyEvent.VK_A, ctrl); cut = addToMenu(editMenu, "Cut", KeyEvent.VK_X, ctrl); copy = addToMenu(editMenu, "Copy", KeyEvent.VK_C, ctrl); addMappedActionToMenu(editMenu, "Copy as Styled Text", EditorPaneActions.rstaCopyAsStyledTextAction, false); paste = addToMenu(editMenu, "Paste", KeyEvent.VK_V, ctrl); addMappedActionToMenu(editMenu, "Paste History...", EditorPaneActions.clipboardHistoryAction, true); GuiUtils.addMenubarSeparator(editMenu, "Find:"); find = addToMenu(editMenu, "Find/Replace...", KeyEvent.VK_F, ctrl); find.setMnemonic(KeyEvent.VK_F); findNext = addToMenu(editMenu, "Find Next", KeyEvent.VK_F3, 0); findNext.setMnemonic(KeyEvent.VK_N); findPrevious = addToMenu(editMenu, "Find Previous", KeyEvent.VK_F3, shift); findPrevious.setMnemonic(KeyEvent.VK_P); GuiUtils.addMenubarSeparator(editMenu, "Go To:"); gotoLine = addToMenu(editMenu, "Go to Line...", KeyEvent.VK_G, ctrl); gotoLine.setMnemonic(KeyEvent.VK_G); addMappedActionToMenu(editMenu, "Go to Matching Bracket", EditorPaneActions.rstaGoToMatchingBracketAction, false); final JMenuItem gotoType = new JMenuItem("Go to Type..."); // we could retrieve the accelerator from paneactions but this may not work, if e.g., an // unsupported syntax (such IJM) has been opened at startup, so we'll just specify it manually //gotoType.setAccelerator(getEditorPane().getPaneActions().getAccelerator("GoToType")); gotoType.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ctrl + shift)); gotoType.addActionListener(e -> { try { getTextArea().getActionMap().get("GoToType").actionPerformed(e); } catch (final Exception | Error ignored) { error("\"Goto Type\" not availabe for current scripting language."); } }); editMenu.add(gotoType); GuiUtils.addMenubarSeparator(editMenu, "Bookmarks:"); addMappedActionToMenu(editMenu, "Next Bookmark", EditorPaneActions.rtaNextBookmarkAction, false); addMappedActionToMenu(editMenu, "Previous Bookmark", EditorPaneActions.rtaPrevBookmarkAction, false); final JMenuItem toggB = addMappedActionToMenu(editMenu, "Toggle Bookmark", EditorPaneActions.rtaToggleBookmarkAction, false); toggB.setToolTipText("Alternatively, click on left bookmark gutter near the line number"); final JMenuItem listBookmarks = addToMenu(editMenu, "List Bookmarks...", 0, 0); listBookmarks.setMnemonic(KeyEvent.VK_L); listBookmarks.addActionListener(e -> listBookmarks()); final JMenuItem clearBookmarks = addToMenu(editMenu, "Clear Bookmarks...", 0, 0); clearBookmarks.addActionListener(e -> clearAllBookmarks()); GuiUtils.addMenubarSeparator(editMenu, "Utilities:"); final JMenuItem commentJMI = addMappedActionToMenu(editMenu, "Toggle Comment", EditorPaneActions.rstaToggleCommentAction, true); commentJMI.setToolTipText("Alternative shortcut: " + getEditorPane().getPaneActions().getAcceleratorLabel(EditorPaneActions.epaToggleCommentAltAction)); addMappedActionToMenu(editMenu, "Insert Time Stamp", EditorPaneActions.rtaTimeDateAction, true); removeTrailingWhitespace = addToMenu(editMenu, "Remove Trailing Whitespace", 0, 0); zapGremlins = addToMenu(editMenu, "Zap Gremlins", 0, 0); zapGremlins.setToolTipText("Removes invalid (non-printable) ASCII characters"); } private void addScritpEditorMacroCommands(final JMenu menu) { GuiUtils.addMenubarSeparator(menu, "Script Editor Macros:"); final JMenuItem startMacro = new JMenuItem("Start/Resume Macro Recording"); startMacro.addActionListener(e -> { if (getEditorPane().isLocked()) { error("File is currently locked."); return; } final String state = (RTextArea.getCurrentMacro() == null) ? "on" : "resumed"; write("Script Editor: Macro recording " + state); RTextArea.beginRecordingMacro(); }); menu.add(startMacro); final JMenuItem pauseMacro = new JMenuItem("Pause Macro Recording..."); pauseMacro.addActionListener(e -> { if (!RTextArea.isRecordingMacro() || RTextArea.getCurrentMacro() == null) { warn("No Script Editor Macro recording exists."); } else { RTextArea.endRecordingMacro(); final int nSteps = RTextArea.getCurrentMacro().getMacroRecords().size(); write("Script Editor: Macro recording off: " + nSteps + " event(s)/action(s) recorded."); if (nSteps == 0) { RTextArea.loadMacro(null); } } }); menu.add(pauseMacro); final JMenuItem endMacro = new JMenuItem("Stop/Save Recording..."); endMacro.addActionListener(e -> { pauseMacro.doClick(); if (RTextArea.getCurrentMacro() != null) { final File fileToSave = getMacroFile(false); if (fileToSave != null) { try { RTextArea.getCurrentMacro().saveToFile(fileToSave); } catch (final IOException e1) { error(e1.getMessage()); e1.printStackTrace(); } } } }); menu.add(endMacro); final JMenuItem clearMacro = new JMenuItem("Clear Recorded Macro..."); clearMacro.addActionListener(e -> { if (RTextArea.getCurrentMacro() == null) { warn("Nothing to clear: No macro has been recorded."); return; } if (confirm("Clear Recorded Macro(s)?", "Clear Recording(s)?", "Clear")) { RTextArea.loadMacro(null); write("Script Editor: Recorded macro(s) cleared."); } }); menu.add(clearMacro); final JMenuItem playMacro = new JMenuItem("Run Recorded Macro"); playMacro.setToolTipText("Runs current recordings. Prompts for\nrecordings file if no recordings exist"); playMacro.addActionListener(e -> { if (getEditorPane().isLocked()) { error("File is currently locked."); return; } if (null == RTextArea.getCurrentMacro()) { final File fileToOpen = getMacroFile(true); if (fileToOpen != null) { try { RTextArea.loadMacro(new Macro(fileToOpen)); } catch (final IOException e1) { error(e1.getMessage()); e1.printStackTrace(); } } } if (RTextArea.isRecordingMacro()) { if (confirm("Recording must be paused before execution. Pause recording now?", "Pause and Run?", "Pause and Run")) { RTextArea.endRecordingMacro(); write("Script Editor: Recording paused"); } else { return; } } if (RTextArea.getCurrentMacro() != null) { final int actions = RTextArea.getCurrentMacro().getMacroRecords().size(); write("Script Editor: Running recorded macro [" + actions + " event(s)/action(s)]"); try { getTextArea().playbackLastMacro(); } catch (final Exception | Error ex) { error("An Exception occured while running macro. See Console for details"); ex.printStackTrace(); } } }); menu.add(playMacro); } private File getMacroFile(final boolean openOtherwiseSave) { final String msg = (openOtherwiseSave) ? "No macros have been recorded. Load macro from file?" : "Recording Stopped. Save recorded macro to local file?"; final String title = (openOtherwiseSave) ? "Load from File?" : "Save to File?"; final String yesLabel = (openOtherwiseSave) ? "Load" : "Save"; if (confirm(msg, title, yesLabel)) { File dir = appService.getApp().getBaseDirectory(); final String filename = "RecordedScriptEditorMacro.xml"; if (getEditorPane().getFile() != null) { dir = getEditorPane().getFile().getParentFile(); } return uiService.chooseFile(new File(dir, filename), (openOtherwiseSave) ? FileWidget.OPEN_STYLE : FileWidget.SAVE_STYLE); } return null; } private void displayRecordableMap() { displayMap(cmdPalette.getRecordableActions(), "Script Editor Recordable Actions/Events"); } private void displayKeyMap() { displayMap(cmdPalette.getShortcuts(), "Script Editor Shortcuts"); } private void displayMap(final Map<String, String> map, final String windowTitle) { final ArrayList<String> lines = new ArrayList<>(); map.forEach( (cmd, key) -> { lines.add("<tr><td>" + cmd + "</td><td>" + key + "</td></tr>"); }); final String prefix = "<HTML><center><table>" // + "<tbody>" // + "<tr>" // + "<td style=\"width: 60%; text-align: center;\"><b>Action</b></td>" // + "<td style=\"width: 40%; text-align: center;\"><b>Shortcut</b></td>" // + "</tr>"; // final String suffix = "</tbody></table></center>"; showHTMLDialog(windowTitle, prefix + String.join("", lines) + suffix); } private class DragAndDrop implements DragSourceListener, DragGestureListener { @Override public void dragDropEnd(final DragSourceDropEvent dsde) {} @Override public void dragEnter(final DragSourceDragEvent dsde) { dsde.getDragSourceContext().setCursor(DragSource.DefaultMoveNoDrop); } @Override public void dragGestureRecognized(final DragGestureEvent dge) { final TreePath path = tree.getSelectionPath(); if (path == null) // nothing is currently selected return; final String filepath = (String)((FileSystemTree.Node) path.getLastPathComponent()).getUserObject(); dragSource.startDrag(dge, DragSource.DefaultCopyDrop, new Transferable() { @Override public boolean isDataFlavorSupported(final DataFlavor flavor) { return DataFlavor.javaFileListFlavor == flavor; } @Override public DataFlavor[] getTransferDataFlavors() { return new DataFlavor[]{ DataFlavor.javaFileListFlavor }; } @Override public Object getTransferData(final DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (isDataFlavorSupported(flavor)) return Arrays.asList(new String[]{filepath}); return null; } }, this); } @Override public void dragExit(final DragSourceEvent dse) { dse.getDragSourceContext().setCursor(DragSource.DefaultMoveNoDrop); } @Override public void dragOver(final DragSourceDragEvent dsde) { if (tree == dsde.getSource()) { dsde.getDragSourceContext().setCursor(DragSource.DefaultCopyNoDrop); } else if (dsde.getDropAction() == DnDConstants.ACTION_COPY) { dsde.getDragSourceContext().setCursor(DragSource.DefaultCopyDrop); } else { dsde.getDragSourceContext().setCursor(DragSource.DefaultCopyNoDrop); } } @Override public void dropActionChanged(final DragSourceDragEvent dsde) {} } public LogService log() { return log; } public PlatformService getPlatformService() { return platformService; } public JTextArea getErrorScreen() { return errorScreen; } public void setErrorScreen(final JTextArea errorScreen) { this.errorScreen = errorScreen; } public ErrorHandler getErrorHandler() { return errorHandler; } public void setErrorHandler(final ErrorHandler errorHandler) { this.errorHandler = errorHandler; } private synchronized void initializeTokenMakers() { if (tokenMakerFactory != null) return; tokenMakerFactory = (AbstractTokenMakerFactory) TokenMakerFactory.getDefaultInstance(); for (final PluginInfo<SyntaxHighlighter> info : pluginService .getPluginsOfType(SyntaxHighlighter.class)) try { tokenMakerFactory.putMapping("text/" + info.getName().toLowerCase().replace(' ', '-'), info .getClassName()); } catch (final Throwable t) { log.warn("Could not register " + info.getName(), t); } } private void initializeDynamicMenuComponents() { // Options menu. These will be updated once EditorPane is created wrapLines = new JCheckBoxMenuItem("Wrap Lines", false); wrapLines.setMnemonic(KeyEvent.VK_W); marginLine = new JCheckBoxMenuItem("Show Margin Line", false); marginLine.setToolTipText("Displays right margin at column 80"); marginLine.addItemListener(e -> setMarginLineEnabled(marginLine.getState())); wrapLines.addItemListener(e -> setWrapLines(wrapLines.getState())); markOccurences = new JCheckBoxMenuItem("Mark Occurences", false); markOccurences.setToolTipText("Allows for all occurrences of a double-clicked string to be" + " highlighted.\nLines with hits are marked on the Editor's notification strip"); markOccurences.addItemListener(e -> setMarkOccurrences(markOccurences.getState())); whiteSpace = new JCheckBoxMenuItem("Show Whitespace", false); whiteSpace.addItemListener(e -> setWhiteSpaceVisible(whiteSpace.isSelected())); paintTabs = new JCheckBoxMenuItem("Show Indent Guides"); paintTabs.setToolTipText("Displays 'tab lines' for leading whitespace"); paintTabs.addItemListener(e -> setPaintTabLines(paintTabs.getState())); autocompletion = new JCheckBoxMenuItem("Enable Autocompletion", true); autocompletion.setToolTipText("Whether code completion should be used.\nNB: Not all languages support this feature"); autocompletion.addItemListener(e -> setAutoCompletionEnabled(autocompletion.getState())); keylessAutocompletion = new JCheckBoxMenuItem("Show Completions Without Ctrl+Space", false); keylessAutocompletion.setToolTipText("If selected, the completion pop-up automatically appears while typing"); keylessAutocompletion.addItemListener(e -> setKeylessAutoCompletion(keylessAutocompletion.getState())); fallbackAutocompletion = new JCheckBoxMenuItem("Use Java Completions as Fallback", false); fallbackAutocompletion.setToolTipText("<HTML>If selected, Java completions will be used when scripting<br>" + "a language for which auto-completions are not available"); fallbackAutocompletion.addItemListener(e -> setFallbackAutoCompletion(fallbackAutocompletion.getState())); themeRadioGroup = new ButtonGroup(); // Help menu. These are 'dynamic' items openMacroFunctions = new JMenuItem("Open Help on Macro Function(s)..."); openMacroFunctions.setMnemonic(KeyEvent.VK_H); openMacroFunctions.addActionListener(e -> { try { new MacroFunctions(this).openHelp(getTextArea().getSelectedText()); } catch (final IOException ex) { handleException(ex); } }); openHelp = new JMenuItem("Open Help for Class (With Frames)..."); openHelp.setMnemonic(KeyEvent.VK_H); openHelp.addActionListener( e-> openHelp(null)); openHelpWithoutFrames = new JMenuItem("Open Help for Class..."); openHelpWithoutFrames.addActionListener(e -> openHelp(null, false)); } /** * Check whether the file was edited outside of this {@link EditorPane} and * ask the user whether to reload. */ public void checkForOutsideChanges() { final EditorPane editorPane = getEditorPane(); if (editorPane.wasChangedOutside()) { reload(editorPane.getFile().getName() + "\nwas changed outside of the editor."); } } /** * Adds a script template path that will be scanned by future TextEditor * instances. * * @param path Resource path to scan for scripts. */ public static void addTemplatePath(final String path) { TEMPLATE_PATHS.add(path); } @EventHandler private void onEvent( @SuppressWarnings("unused") final ContextDisposingEvent e) { if (isDisplayable()) dispose(); } /** * Loads the Script Editor layout from persisted storage. * @see #saveWindowSizeToPrefs() */ public void loadWindowSizePreferences() { layoutLoading = true; final Dimension dim = getSize(); // If a dimension is 0 then use the default dimension size if (0 == dim.width) dim.width = DEFAULT_WINDOW_WIDTH; if (0 == dim.height) dim.height = DEFAULT_WINDOW_HEIGHT; final int windowWidth = prefService.getInt(getClass(), WINDOW_WIDTH, dim.width); final int windowHeight = prefService.getInt(getClass(), WINDOW_HEIGHT, dim.height); // Avoid creating a window larger than the desktop final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); if (windowWidth > screen.getWidth() || windowHeight > screen.getHeight()) setPreferredSize(new Dimension(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT)); else setPreferredSize(new Dimension(windowWidth, windowHeight)); final int mainDivLocation = prefService.getInt(getClass(), MAIN_DIV_LOCATION, body.getDividerLocation()); body.setDividerLocation(mainDivLocation); final TextEditorTab tab = getTab(); final int tabDivLocation = prefService.getInt(getClass(), TAB_DIV_LOCATION, tab.getDividerLocation()); final int tabDivOrientation = prefService.getInt(getClass(), TAB_DIV_ORIENTATION, tab.getOrientation()); final int replDividerLocation = prefService.getInt(getClass(), REPL_DIV_LOCATION, tab.getScreenAndPromptSplit().getDividerLocation()); tab.setDividerLocation(tabDivLocation); tab.setOrientation(tabDivOrientation); tab.getScreenAndPromptSplit().setDividerLocation(replDividerLocation); layoutLoading = false; } /** * Saves the Script Editor layout to persisted storage. * <p> * Separated from savePreferences because we always want to save the window * size when it's resized, however, we don't want to automatically save the * font, tab size, etc. without the user pressing "Save Preferences" * </p> * @see #loadWindowSizePreferences() */ public void saveWindowSizeToPrefs() { if (layoutLoading) return; final Dimension dim = getSize(); prefService.put(getClass(), WINDOW_HEIGHT, dim.height); prefService.put(getClass(), WINDOW_WIDTH, dim.width); prefService.put(getClass(), MAIN_DIV_LOCATION, body.getDividerLocation()); final TextEditorTab tab = getTab(); prefService.put(getClass(), TAB_DIV_LOCATION, tab.getDividerLocation()); prefService.put(getClass(), TAB_DIV_ORIENTATION, tab.getOrientation()); prefService.put(getClass(), REPL_DIV_LOCATION, tab.getScreenAndPromptSplit().getDividerLocation()); } final public RSyntaxTextArea getTextArea() { return getEditorPane(); } /** * Get the currently selected tab. * * @return The currently selected tab. Never null. */ public TextEditorTab getTab() { int index = tabbed.getSelectedIndex(); if (index < 0) { // should not happen, but safety first. if (tabbed.getTabCount() == 0) { // should not happen either, but, again, safety first. createNewDocument(); } // Ensure the new document is returned - otherwise we would pass // the negative index to the getComponentAt call below. tabbed.setSelectedIndex(0); index = 0; } return (TextEditorTab) tabbed.getComponentAt(index); } /** * Get tab at provided index. * * @param index the index of the tab. * @return the {@link TextEditorTab} at given index or <code>null</code>. */ public TextEditorTab getTab(final int index) { return (TextEditorTab) tabbed.getComponentAt(index); } /** * Return the {@link EditorPane} of the currently selected * {@link TextEditorTab}. * * @return the current {@link EditorPane}. Never <code>null</code>. */ public EditorPane getEditorPane() { return getTab().editorPane; } /** * @return {@link ScriptLanguage} used in the current {@link EditorPane}. */ public ScriptLanguage getCurrentLanguage() { return getEditorPane().getCurrentLanguage(); } public JMenuItem addToMenu(final JMenu menu, final String menuEntry, final int key, final int modifiers) { final JMenuItem item = new JMenuItem(menuEntry); menu.add(item); if (key != 0) item.setAccelerator(KeyStroke.getKeyStroke(key, modifiers)); item.addActionListener(this); return item; } private JMenuItem addMappedActionToMenu(final JMenu menu, String label, String actionID, final boolean editingAction) { final JMenuItem jmi = new JMenuItem(label); jmi.addActionListener(e -> { try { if (editingAction && getEditorPane().isLocked()) { warn("File is currently locked."); return; } if (RTextAreaEditorKit.clipboardHistoryAction.equals(actionID) && ClipboardHistory.get().getHistory().isEmpty()) { warn("The internal clipboard manager is empty."); return; } getTextArea().requestFocusInWindow(); getTextArea().getActionMap().get(actionID).actionPerformed(e); } catch (final Exception | Error ignored) { error("\"" + label + "\" not availabe for current scripting language."); } }); jmi.setAccelerator(getEditorPane().getPaneActions().getAccelerator(actionID)); menu.add(jmi); return jmi; } protected static class AcceleratorTriplet { JMenuItem component; int key, modifiers; } protected List<AcceleratorTriplet> defaultAccelerators = new ArrayList<>(); public void addAccelerator(final JMenuItem component, final int key, final int modifiers) { addAccelerator(component, key, modifiers, false); } public void addAccelerator(final JMenuItem component, final int key, final int modifiers, final boolean record) { if (record) { final AcceleratorTriplet triplet = new AcceleratorTriplet(); triplet.component = component; triplet.key = key; triplet.modifiers = modifiers; defaultAccelerators.add(triplet); } final RSyntaxTextArea textArea = getTextArea(); if (textArea != null) addAccelerator(textArea, component, key, modifiers); } public void addAccelerator(final RSyntaxTextArea textArea, final JMenuItem component, final int key, final int modifiers) { textArea.getInputMap().put(KeyStroke.getKeyStroke(key, modifiers), component); textArea.getActionMap().put(component, new AbstractAction() { @Override public void actionPerformed(final ActionEvent e) { if (!component.isEnabled()) return; final ActionEvent event = new ActionEvent(component, 0, "Accelerator"); TextEditor.this.actionPerformed(event); } }); } public void addDefaultAccelerators(final RSyntaxTextArea textArea) { for (final AcceleratorTriplet triplet : defaultAccelerators) addAccelerator(textArea, triplet.component, triplet.key, triplet.modifiers); } private JMenu getMenu(final JMenu root, final String menuItemPath, final boolean createIfNecessary) { final int slash = menuItemPath.indexOf('/'); if (slash < 0) return root; final String menuLabel = menuItemPath.substring(0, slash); final String rest = menuItemPath.substring(slash + 1); for (int i = 0; i < root.getItemCount(); i++) { final JMenuItem item = root.getItem(i); if (item instanceof JMenu && menuLabel.equals(item.getText())) { return getMenu((JMenu) item, rest, createIfNecessary); } } if (!createIfNecessary) return null; final JMenu subMenu = new JMenu(menuLabel); root.add(subMenu); return getMenu(subMenu, rest, createIfNecessary); } /** * Initializes the Templates menu. * <p> * Other components can add templates simply by providing scripts in their * resources, identified by a path of the form * {@code /script_templates/<menu path>/<menu label>}. * </p> * * @param templatesMenu the top-level menu to populate */ private void addTemplates(final JMenu templatesMenu) { final File baseDir = appService.getApp().getBaseDirectory(); for (final String templatePath : TEMPLATE_PATHS) { for (final Map.Entry<String, URL> entry : new TreeMap<>( FileUtils.findResources(null, templatePath, baseDir)).entrySet()) { final String key = entry.getKey(); final String ext = FileUtils.getExtension(key); // try to determine the scripting language final ScriptLanguage lang = ext.isEmpty() ? null : scriptService.getLanguageByExtension(ext); final String langName = lang == null ? null : lang.getLanguageName(); final String langSuffix = lang == null ? null : " (" + langName + ")"; final String path = adjustPath(key, langName); // create a human-readable label final int labelIndex = path.lastIndexOf('/') + 1; final String label = ext.isEmpty() ? path.substring(labelIndex) : path.substring(labelIndex, path.length() - ext.length() - 1); final ActionListener menuListener = e -> loadTemplate(entry.getValue()); // add script to the secondary language-sorted menu structure if (langName != null) { final String langPath = "[by language]/" + langName + "/" + path; final JMenu langMenu = getMenu(templatesMenu, langPath, true); final JMenuItem langItem = new JMenuItem(label); langMenu.add(langItem); langItem.addActionListener(menuListener); } // add script to the primary Templates menu structure final JMenu menu = getMenu(templatesMenu, path, true); final JMenuItem item = new JMenuItem(label + langSuffix); menu.add(item); item.addActionListener(menuListener); } } } /** * Loads a template file from the given resource * * @param url The resource to load. */ public void loadTemplate(final String url) { try { loadTemplate(new URL(url)); } catch (final Exception e) { log.error(e); error("The template '" + url + "' was not found."); } } public void loadTemplate(final URL url) { final String path = url.getPath(); final String ext = FileUtils.getExtension(path); final ScriptLanguage language = ext.isEmpty() ? null : scriptService.getLanguageByExtension(ext); loadTemplate(url, language); } public void loadTemplate(final URL url, final ScriptLanguage language) { createNewDocument(); try { // Load the template final InputStream in = url.openStream(); getTextArea().read(new BufferedReader(new InputStreamReader(in)), null); if (language != null) { setLanguage(language); } final String path = url.getPath(); setEditorPaneFileName(path.substring(path.lastIndexOf('/') + 1)); } catch (final Exception e) { log.error(e); error("The template '" + url + "' was not found."); } } public void createNewDocument() { open(null); } public void createNewDocument(final String title, final String text) { open(null); final EditorPane editorPane = getEditorPane(); editorPane.setText(text); setEditorPaneFileName(title); editorPane.setLanguageByFileName(title); updateLanguageMenu(editorPane.getCurrentLanguage()); } /** * Open a new editor to edit the given file, with a templateFile if the file * does not exist yet */ public void createNewFromTemplate(final File file, final File templateFile) { open(file.exists() ? file : templateFile); if (!file.exists()) { final EditorPane editorPane = getEditorPane(); try { editorPane.open(file); } catch (final IOException e) { handleException(e); } editorPane.setLanguageByFileName(file.getName()); updateLanguageMenu(editorPane.getCurrentLanguage()); } } public boolean fileChanged() { return getEditorPane().fileChanged(); } public boolean handleUnsavedChanges() { return handleUnsavedChanges(false); } public boolean handleUnsavedChanges(final boolean beforeCompiling) { if (!fileChanged()) return true; if (beforeCompiling && autoSave.getState()) { save(); return true; } if (GuiUtils.confirm(this, "Do you want to save changes?", "Save Changes?", "Save")) { return save(); } else { // Compiled languages should not progress if their source is unsaved return !beforeCompiling; } } private boolean isJava(final ScriptLanguage language) { return language != null && language.getLanguageName().equals("Java"); } @Override public void actionPerformed(final ActionEvent ae) { final Object source = ae.getSource(); if (source == newFile) createNewDocument(); else if (source == open) { final EditorPane editorPane = getEditorPane(); final File defaultDir = editorPane.getFile() != null ? editorPane.getFile().getParentFile() : appService.getApp().getBaseDirectory(); final File file = openWithDialog(defaultDir); if (file != null) new Thread(() -> open(file)).start(); return; } else if (source == save) save(); else if (source == saveas) saveAs(); else if (source == makeJar) makeJar(false); else if (source == makeJarWithSource) makeJar(true); else if (source == compileAndRun) runText(); else if (source == compile) compile(); else if (source == runSelection) runText(true); else if (source == nextError) { if (isJava(getEditorPane().getCurrentLanguage())) new Thread(() -> nextError(true)).start(); else getEditorPane().getErrorHighlighter().gotoNextError(); } else if (source == previousError) { if (isJava(getEditorPane().getCurrentLanguage())) new Thread(() -> nextError(false)).start(); else getEditorPane().getErrorHighlighter().gotoPreviousError(); } else if (source == kill) chooseTaskToKill(); else if (source == close) if (tabbed.getTabCount() < 2) processWindowEvent(new WindowEvent( this, WindowEvent.WINDOW_CLOSING)); else { if (!handleUnsavedChanges()) return; int index = tabbed.getSelectedIndex(); removeTab(index); if (index > 0) index--; switchTo(index); } else if (source == copy) getTextArea().copy(); else if (source == find) findOrReplace(true); else if (source == findNext) { findDialog.setRestrictToConsole(false); findDialog.searchOrReplace(false); } else if (source == findPrevious) { findDialog.setRestrictToConsole(false); findDialog.searchOrReplace(false, false); } else if (source == gotoLine) gotoLine(); else if (source == selectAll) { getTextArea().setCaretPosition(0); getTextArea().moveCaretPosition(getTextArea().getDocument().getLength()); } else if (source == chooseFontSize) { commandService.run(ChooseFontSize.class, true, "editor", this); } else if (source == chooseTabSize) { commandService.run(ChooseTabSize.class, true, "editor", this); } else if (source == openClassOrPackageHelp) openClassOrPackageHelp(null); else if (source == extractSourceJar) extractSourceJar(); else if (source == askChatGPTtoGenerateCode) askChatGPTtoGenerateCode(); else if (source == openSourceForClass) { final String className = getSelectedClassNameOrAsk("Class (fully qualified name):", "Which Class?"); if (className != null) { try { final String url = new FileFunctions(this).getSourceURL(className); platformService.open(new URL(url)); } catch (final Throwable e) { handleException(e); } } } /* TODO else if (source == showDiff) { new Thread(() -> { EditorPane pane = getEditorPane(); new FileFunctions(TextEditor.this).showDiff(pane.file, pane.getGitDirectory()); }).start(); } else if (source == commit) { new Thread(() -> { EditorPane pane = getEditorPane(); new FileFunctions(TextEditor.this).commit(pane.file, pane.getGitDirectory()); }).start(); } */ else if (source == gitGrep) { final String searchTerm = getTextArea().getSelectedText(); File searchRoot = getEditorPane().getFile(); if (searchRoot == null) { error("File was not yet saved; no location known!"); return; } searchRoot = searchRoot.getParentFile(); commandService.run(GitGrep.class, true, "editor", this, "searchTerm", searchTerm, "searchRoot", searchRoot); } else if (source == increaseFontSize || source == decreaseFontSize) { getEditorPane().increaseFontSize( (float) (source == increaseFontSize ? 1.2 : 1 / 1.2)); setFontSize(getEditorPane().getFontSize()); } else if (source == nextTab) switchTabRelative(1); else if (source == previousTab) switchTabRelative(-1); else if (handleTabsMenu(source)) return; else { // commands that should not run when files are locked! if (getEditorPane().isLocked()) { error("File is currently locked."); return; } if (source == cut) getTextArea().cut(); else if (source == paste) getTextArea().paste(); else if (source == undo) getTextArea().undoLastAction(); else if (source == redo) getTextArea().redoLastAction(); else if (source == addImport) { addImport(getSelectedClassNameOrAsk("Add import (complete qualified name of class/package)", "Which Class to Import?")); } else if (source == removeUnusedImports) new TokenFunctions(getTextArea()).removeUnusedImports(); else if (source == sortImports) new TokenFunctions(getTextArea()).sortImports(); else if (source == removeTrailingWhitespace) new TokenFunctions(getTextArea()).removeTrailingWhitespace(); else if (source == replaceTabsWithSpaces) getTextArea().convertTabsToSpaces(); else if (source == replaceSpacesWithTabs) getTextArea().convertSpacesToTabs(); else if (source == zapGremlins) zapGremlins(); } } private void setAutoCompletionEnabled(final boolean enabled) { for (int i = 0; i < tabbed.getTabCount(); i++) getEditorPane(i).setAutoCompletion(enabled); keylessAutocompletion.setEnabled(enabled); fallbackAutocompletion.setEnabled(enabled); } private void setTabsEmulated(final boolean emulated) { for (int i = 0; i < tabbed.getTabCount(); i++) getEditorPane(i).setTabsEmulated(emulated); getEditorPane().requestFocusInWindow(); } private void setPaintTabLines(final boolean paint) { for (int i = 0; i < tabbed.getTabCount(); i++) getEditorPane(i).setPaintTabLines(paint); getEditorPane().requestFocusInWindow(); } private void setKeylessAutoCompletion(final boolean noKeyRequired) { for (int i = 0; i < tabbed.getTabCount(); i++) getEditorPane(i).setKeylessAutoCompletion(noKeyRequired); getEditorPane().requestFocusInWindow(); } private void setFallbackAutoCompletion(final boolean fallback) { for (int i = 0; i < tabbed.getTabCount(); i++) getEditorPane(i).setFallbackAutoCompletion(fallback); getEditorPane().requestFocusInWindow(); } private void setMarkOccurrences(final boolean markOccurrences) { for (int i = 0; i < tabbed.getTabCount(); i++) getEditorPane(i).setMarkOccurrences(markOccurrences); getEditorPane().requestFocusInWindow(); } private void setWhiteSpaceVisible(final boolean visible) { for (int i = 0; i < tabbed.getTabCount(); i++) getEditorPane(i).setWhitespaceVisible(visible); getEditorPane().requestFocusInWindow(); } private void setWrapLines(final boolean wrap) { for (int i = 0; i < tabbed.getTabCount(); i++) getEditorPane(i).setLineWrap(wrap); getEditorPane().requestFocusInWindow(); } private void setMarginLineEnabled(final boolean enabled) { for (int i = 0; i < tabbed.getTabCount(); i++) getEditorPane(i).setMarginLineEnabled(enabled); getEditorPane().requestFocusInWindow(); } private JMenu applyThemeMenu() { final LinkedHashMap<String, String> map = new LinkedHashMap<>(); map.put("Default", "default"); map.put("-", "-"); map.put("Dark", "dark"); map.put("Druid", "druid"); map.put("Monokai", "monokai"); map.put("Eclipse (Light)", "eclipse"); map.put("IntelliJ (Light)", "idea"); map.put("Visual Studio (Light)", "vs"); themeRadioGroup = new ButtonGroup(); final JMenu menu = new JMenu("Theme"); map.forEach((k, v) -> { if ("-".equals(k)) { menu.addSeparator(); return; } final JRadioButtonMenuItem item = new JRadioButtonMenuItem(k); item.setActionCommand(v); // needed for #updateThemeControls() themeRadioGroup.add(item); item.addActionListener(e -> { try { applyTheme(v, false); // make the choice available for the next tab prefService.put(EditorPane.class, EditorPane.THEME_PREFS, v); } catch (final IllegalArgumentException ex) { error("Theme could not be loaded. See Console for details."); ex.printStackTrace(); } }); menu.add(item); }); return menu; } /** * Applies a theme to all the panes of this editor. * * @param theme either "default", "dark", "druid", "eclipse", "idea", "monokai", * "vs" * @throws IllegalArgumentException If {@code theme} is not a valid option, or * the resource could not be loaded */ public void applyTheme(final String theme) throws IllegalArgumentException { applyTheme(theme, true); } private void applyTheme(final String theme, final boolean updateMenus) throws IllegalArgumentException { try { final Theme th = getTheme(theme); if (th == null) { writeError("Unrecognized theme ignored: '" + theme + "'"); return; } for (int i = 0; i < tabbed.getTabCount(); i++) { getEditorPane(i).applyTheme(theme); } } catch (final Exception ex) { activeTheme = "default"; updateThemeControls("default"); writeError("Could not load theme. See Console for details."); updateThemeControls(activeTheme); throw new IllegalArgumentException(ex); } activeTheme = theme; if (updateMenus) updateThemeControls(theme); } static Theme getTheme(final String theme) throws IllegalArgumentException { try { return Theme .load(TextEditor.class.getResourceAsStream("/org/fife/ui/rsyntaxtextarea/themes/" + theme + ".xml")); } catch (final Exception ex) { throw new IllegalArgumentException(ex); } } private void updateThemeControls(final String theme) { if (themeRadioGroup != null) { final Enumeration<AbstractButton> choices = themeRadioGroup.getElements(); while (choices.hasMoreElements()) { final AbstractButton choice = choices.nextElement(); if (theme.equals(choice.getActionCommand())) { choice.setSelected(true); break; } } } } private void collapseSplitPane(final int pane, final boolean collapse) { final JSplitPane jsp = (pane == 0) ? body : getTab(); if (collapse) { panePositions[pane] = jsp.getDividerLocation(); if (pane == 0) { // collapse to left jsp.setDividerLocation(0.0d); } else { // collapse to bottom jsp.setDividerLocation(1.0d); } } else { jsp.setDividerLocation(panePositions[pane]); // Check if user collapsed pane manually (stashed panePosition is invalid) final boolean expanded = (pane == 0) ? isLeftPaneExpanded(jsp) : isRightOrBottomPaneExpanded(jsp); if (!expanded // && JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(TextEditor.this, // // "Expand to default position?", "Expand to Defaults?", JOptionPane.OK_CANCEL_OPTION) ) { jsp.setDividerLocation((pane == 0) ? .2d : .75d); panePositions[pane] = jsp.getDividerLocation(); } } } private boolean isLeftPaneExpanded(final JSplitPane pane) { return pane.isVisible() && pane.getLeftComponent().getWidth() > 0; } private boolean isRightOrBottomPaneExpanded(final JSplitPane pane) { final int dim = (pane.getOrientation() == JSplitPane.VERTICAL_SPLIT) ? pane.getRightComponent().getHeight() : pane.getRightComponent().getWidth(); return pane.isVisible() && dim > 0; } protected boolean handleTabsMenu(final Object source) { if (!(source instanceof JMenuItem)) return false; final JMenuItem item = (JMenuItem) source; if (!tabsMenuItems.contains(item)) return false; for (int i = tabsMenuTabsStart; i < tabsMenu.getItemCount(); i++) if (tabsMenu.getItem(i) == item) { switchTo(i - tabsMenuTabsStart); return true; } return false; } @Override public void stateChanged(final ChangeEvent e) { final int index = tabbed.getSelectedIndex(); if (index < 0) { setTitle(""); return; } final EditorPane editorPane = getEditorPane(index); lockPane.setSelected(editorPane.isLocked()); editorPane.requestFocus(); checkForOutsideChanges(); //whiteSpace.setSelected(editorPane.isWhitespaceVisible()); editorPane.setLanguageByFileName(editorPane.getFileName()); updateLanguageMenu(editorPane.getCurrentLanguage()); setTitle(); } public EditorPane getEditorPane(final int index) { return getTab(index).editorPane; } public void findOrReplace(final boolean doReplace) { findDialog.setLocationRelativeTo(this); findDialog.setRestrictToConsole(false); // override search pattern only if // there is sth. selected final String selection = getEditorPane().getSelectedText(); if (selection != null) findDialog.setSearchPattern(selection); findDialog.show(doReplace && !getEditorPane().isLocked()); } public void gotoLine() { final String line = GuiUtils.getString(this, "Enter line number:", "Goto Line"); if (line == null) return; try { gotoLine(Integer.parseInt(line)); } catch (final BadLocationException e) { error("Line number out of range: " + line); } catch (final NumberFormatException e) { error("Invalid line number: " + line); } } public void gotoLine(final int line) throws BadLocationException { getTextArea().setCaretPosition(getTextArea().getLineStartOffset(line - 1)); } public void toggleBookmark() { getEditorPane().toggleBookmark(); } private Vector<Bookmark> getAllBookmarks() { final Vector<Bookmark> bookmarks = new Vector<>(); for (int i = 0; i < tabbed.getTabCount(); i++) { final TextEditorTab tab = (TextEditorTab) tabbed.getComponentAt(i); tab.editorPane.getBookmarks(tab, bookmarks); } if (bookmarks.isEmpty()) { info("No Bookmarks currently exist.\nYou can bookmark lines by clicking next to their line number.", "No Bookmarks"); } return bookmarks; } public void listBookmarks() { final Vector<Bookmark> bookmarks = getAllBookmarks(); if (!bookmarks.isEmpty()) { new BookmarkDialog(this, bookmarks).setVisible(true); } } void clearAllBookmarks() { final Vector<Bookmark> bookmarks = getAllBookmarks(); if (bookmarks.isEmpty()) return; ; if (confirm("Delete all bookmarks?", "Confirm Deletion?", "Delete")) { bookmarks.forEach(bk -> bk.tab.editorPane.toggleBookmark(bk.getLineNumber())); } } public boolean reload() { return reload("Reload the file?"); } public boolean reload(final String message) { return reloadRevert(message, "Reload"); } private boolean reloadRevert(final String message, final String title) { final EditorPane editorPane = getEditorPane(); final File file = editorPane.getFile(); if (file == null || !file.exists()) return true; final boolean modified = editorPane.fileChanged(); final String[] options = { title, "Do Not " + title }; if (modified) options[0] = title + " (Discard Changes)"; switch (JOptionPane.showOptionDialog(this, message, title + "?", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0])) { case 0: try { editorPane.open(file); return true; } catch (final IOException e) { error("Could not reload " + file.getPath()); } updateLanguageMenu(editorPane.getCurrentLanguage()); break; } return false; } public static boolean isBinary(final File file) { if (file == null) return false; // heuristic: read the first up to 8000 bytes, and say that it is binary if // it contains a NUL try (final FileInputStream in = new FileInputStream(file)) { int left = 8000; final byte[] buffer = new byte[left]; while (left > 0) { final int count = in.read(buffer, 0, left); if (count < 0) break; for (int i = 0; i < count; i++) if (buffer[i] == 0) { in.close(); return true; } left -= count; } return false; } catch (final IOException e) { return false; } } /** * Opens a new tab with some content. * * @param content the script content * @param languageExtension the file extension associated with the content's * language (e.g., ".java", ".py", etc. * @return the text editor tab */ public TextEditorTab newTab(final String content, final String languageExtension) { String lang = languageExtension; final TextEditorTab tab = open(null); if (null != lang && lang.length() > 0) { lang = lang.trim().toLowerCase(); if ('.' != lang.charAt(0)) { lang = "." + languageExtension; } tab.editorPane.setLanguage(scriptService.getLanguageByName(languageExtension)); } else { final String lastLanguageName = prefService.get(getClass(), LAST_LANGUAGE); if (null != lastLanguageName && "none" != lastLanguageName) setLanguage(scriptService.getLanguageByName(lastLanguageName)); } if (null != content) { tab.editorPane.setText(content); } return tab; } public TextEditorTab open(final File file) { if (isBinary(file)) { try { uiService.show(ioService.open(file.getAbsolutePath())); } catch (final IOException e) { log.error(e); } return null; } try { TextEditorTab tab = (tabbed.getTabCount() == 0) ? null : getTab(); final TextEditorTab prior = tab; final boolean wasNew = tab != null && tab.editorPane.isNew(); float font_size = 0; // to set the new editor's font like the last active one, if any if (!wasNew) { if (tabbed.getTabCount() > 0) font_size = getTab().getEditorPane().getFont().getSize2D(); tab = new TextEditorTab(this); context.inject(tab.editorPane); tab.editorPane.loadPreferences(); addDefaultAccelerators(tab.editorPane); } else { // the Edit menu can only be populated after an editor pane exists, as it reads // actions from its input map. We will built it here, if it has not been assembled // yet. if (undo == null) assembleEditMenu(); } synchronized (tab.editorPane) { // tab is never null at this location. tab.editorPane.open(file); if (wasNew) { final int index = tabbed.getSelectedIndex() + tabsMenuTabsStart; tabsMenu.getItem(index).setText(tab.editorPane.getFileName()); } else { tabbed.addTab("", tab); switchTo(tabbed.getTabCount() - 1); tabsMenuItems.add(addToMenu(tabsMenu, tab.editorPane.getFileName(), 0, 0)); } setEditorPaneFileName(tab.editorPane.getFile()); try { updateTabAndFontSize(true); if (font_size > 0) setFontSize(font_size); if (null != prior) { tab.setOrientation(prior.getOrientation()); tab.setDividerLocation(prior.getDividerLocation()); } } catch (final NullPointerException e) { /* ignore */ } } if (file != null) openRecent.add(file.getAbsolutePath()); else { final String lastLanguageName = prefService.get(getClass(), LAST_LANGUAGE); if ("none" != lastLanguageName) setLanguage(scriptService.getLanguageByName(lastLanguageName)); } updateLanguageMenu(tab.editorPane.getCurrentLanguage()); tab.editorPane.getDocument().addDocumentListener(this); tab.editorPane.requestFocusInWindow(); return tab; } catch (final FileNotFoundException e) { log.error(e); error("The file\n'" + file + "' was not found."); } catch (final Exception e) { log.error(e); error("There was an error while opening\n'" + file + "': " + e); } return null; } public boolean saveAs() { final EditorPane editorPane = getEditorPane(); File file = editorPane.getFile(); if (file == null) { final File ijDir = appService.getApp().getBaseDirectory(); file = new File(ijDir, editorPane.getFileName()); } final File fileToSave = uiService.chooseFile(file, FileWidget.SAVE_STYLE); if (fileToSave == null) return false; return saveAs(fileToSave.getAbsolutePath(), true); } public void saveAs(final String path) { saveAs(path, true); } public boolean saveAs(final String path, final boolean askBeforeReplacing) { final File file = new File(path); if (file.exists() && askBeforeReplacing && confirm("Do you want to replace " + path + "?", "Replace " + path + "?", "Replace")) return false; if (!write(file)) return false; setEditorPaneFileName(file); openRecent.add(path); return true; } public boolean save() { final File file = getEditorPane().getFile(); if (file == null) { return saveAs(); } if (!write(file)) { return false; } setTitle(); return true; } public boolean write(final File file) { try { getEditorPane().write(file); return true; } catch (final IOException e) { log.error(e); error("Could not save " + file.getName()); return false; } } public boolean makeJar(final boolean includeSources) { final File file = getEditorPane().getFile(); if ((file == null || isCompiled()) && !handleUnsavedChanges(true)) { return false; } String name = getEditorPane().getFileName(); final String ext = FileUtils.getExtension(name); if (!"".equals(ext)) name = name.substring(0, name.length() - ext.length()); if (name.indexOf('_') < 0) name += "_"; name += ".jar"; final File selectedFile = uiService.chooseFile(file, FileWidget.SAVE_STYLE); if (selectedFile == null) return false; if (selectedFile.exists() && confirm("Do you want to replace " + selectedFile + "?", "Replace " + selectedFile + "?", "Replace")) return false; try { makeJar(selectedFile, includeSources); return true; } catch (final IOException e) { log.error(e); error("Could not write " + selectedFile + ": " + e.getMessage()); return false; } } /** * @throws IOException */ public void makeJar(final File file, final boolean includeSources) throws IOException { if (!handleUnsavedChanges(true)) return; final ScriptEngine interpreter = getCurrentLanguage().getScriptEngine(); if (interpreter instanceof JavaEngine) { final JavaEngine java = (JavaEngine) interpreter; final JTextAreaWriter errors = new JTextAreaWriter(errorScreen, log); markCompileStart(); getTab().showErrors(); new Thread(() -> { java.makeJar(getEditorPane().getFile(), includeSources, file, errors); errorScreen.insert("Compilation finished.\n", // errorScreen.getDocument().getLength()); markCompileEnd(); }).start(); } } static void getClasses(final File directory, final List<String> paths, final List<String> names) { getClasses(directory, paths, names, ""); } static void getClasses(final File directory, final List<String> paths, final List<String> names, final String inPrefix) { String prefix = inPrefix; if (!prefix.equals("")) prefix += "/"; for (final File file : directory.listFiles()) if (file.isDirectory()) getClasses(file, paths, names, prefix + file.getName()); else { paths.add(file.getAbsolutePath()); names.add(prefix + file.getName()); } } static void writeJarEntry(final JarOutputStream out, final String name, final byte[] buf) throws IOException { try { final JarEntry entry = new JarEntry(name); out.putNextEntry(entry); out.write(buf, 0, buf.length); out.closeEntry(); } catch (final ZipException e) { throw new IOException(e); } } static byte[] readFile(final String fileName) throws IOException { final File file = new File(fileName); try (final InputStream in = new FileInputStream(file)) { final byte[] buffer = new byte[(int) file.length()]; in.read(buffer); return buffer; } } static void deleteRecursively(final File directory) { for (final File file : directory.listFiles()) if (file.isDirectory()) deleteRecursively(file); else file.delete(); directory.delete(); } void setLanguage(final ScriptLanguage language) { setLanguage(language, false); } void setLanguage(final ScriptLanguage language, final boolean addHeader) { if (null != this.getCurrentLanguage() && (null == language || this.getCurrentLanguage().getLanguageName() != language.getLanguageName())) { this.scriptInfo = null; } getEditorPane().setLanguage(language, addHeader); prefService.put(getClass(), LAST_LANGUAGE, null == language? "none" : language.getLanguageName()); setTitle(); updateLanguageMenu(language); updateUI(true); } private String lastSupportStatus = null; void updateLanguageMenu(final ScriptLanguage language) { JMenuItem item = languageMenuItems.get(language); if (item == null) { // is none item = noneLanguageItem; setIncremental(false); } if (!item.isSelected()) { item.setSelected(true); } // print autocompletion status to console String supportStatus = getEditorPane().getSupportStatus(); if (supportStatus != null && !Objects.equals(supportStatus, lastSupportStatus)) { write(supportStatus); lastSupportStatus = supportStatus; } final boolean isRunnable = item != noneLanguageItem; final boolean isCompileable = language != null && language.isCompiledLanguage(); runMenu.setEnabled(isRunnable); compileAndRun.setText(isCompileable ? "Compile and Run" : "Run"); compileAndRun.setEnabled(isRunnable); runSelection.setEnabled(isRunnable && !isCompileable); compile.setEnabled(isCompileable); autoSave.setEnabled(isCompileable); makeJar.setEnabled(isCompileable); makeJarWithSource.setEnabled(isCompileable); final boolean isJava = language != null && language.getLanguageName().equals("Java"); addImport.setEnabled(isJava); removeUnusedImports.setEnabled(isJava); sortImports.setEnabled(isJava); //openSourceForMenuItem.setEnabled(isJava); final boolean isMacro = language != null && language.getLanguageName().equals("ImageJ Macro"); openMacroFunctions.setEnabled(isMacro); openSourceForClass.setEnabled(!isMacro); openHelp.setEnabled(!isMacro && isRunnable); openHelpWithoutFrames.setEnabled(!isMacro && isRunnable); nextError.setEnabled(!isMacro && isRunnable); previousError.setEnabled(!isMacro && isRunnable); final boolean isInGit = getEditorPane().getGitDirectory() != null; gitMenu.setVisible(isInGit); updateUI(false); } /** * Use {@link #updateUI(boolean)} instead */ @Deprecated public void updateTabAndFontSize(final boolean setByLanguage) { updateUI(setByLanguage); } public void updateUI(final boolean setByLanguage) { final EditorPane pane = getEditorPane(); //if (pane.getCurrentLanguage() == null) return; if (setByLanguage && pane.getCurrentLanguage() != null) { if (pane.getCurrentLanguage().getLanguageName().equals("Python")) { pane.setTabSize(4); } else { // set tab size to current preferences. pane.resetTabSize(); } } final int tabSize = pane.getTabSize(); boolean defaultSize = false; for (int i = 0; i < tabSizeMenu.getItemCount(); i++) { final JMenuItem item = tabSizeMenu.getItem(i); if (item == chooseTabSize) { item.setSelected(!defaultSize); item.setText("Other" + (defaultSize ? "" : " (" + tabSize + ")") + "..."); } else if (tabSize == Integer.parseInt(item.getText())) { item.setSelected(true); defaultSize = true; } } getTab().prompt.setTabSize(getEditorPane().getTabSize()); final int fontSize = (int) pane.getFontSize(); defaultSize = false; for (int i = 0; i < fontSizeMenu.getItemCount(); i++) { final JMenuItem item = fontSizeMenu.getItem(i); if (item == chooseFontSize) { item.setSelected(!defaultSize); item.setText("Other" + (defaultSize ? "" : " (" + fontSize + ")") + "..."); continue; } String label = item.getText(); if (label.endsWith(" pt")) label = label.substring(0, label.length() - 3); if (fontSize == Integer.parseInt(label)) { item.setSelected(true); defaultSize = true; } } markOccurences.setState(pane.getMarkOccurrences()); wrapLines.setState(pane.getLineWrap()); marginLine.setState(pane.isMarginLineEnabled()); tabsEmulated.setState(pane.getTabsEmulated()); paintTabs.setState(pane.getPaintTabLines()); whiteSpace.setState(pane.isWhitespaceVisible()); autocompletion.setState(pane.isAutoCompletionEnabled()); fallbackAutocompletion.setState(pane.isAutoCompletionFallbackEnabled()); keylessAutocompletion.setState(pane.isAutoCompletionKeyless()); sourceTreePanel.rebuildSourceTree(pane); } public void setEditorPaneFileName(final String baseName) { getEditorPane().setFileName(baseName); } public void setEditorPaneFileName(final File file) { final EditorPane editorPane = getEditorPane(); editorPane.setFileName(file); // update language menu updateLanguageMenu(editorPane.getCurrentLanguage()); updateGitDirectory(); } void setTitle() { final EditorPane editorPane = getEditorPane(); final boolean fileChanged = editorPane.fileChanged(); final String fileName = editorPane.getFileName(); final String title = (fileChanged ? "*" : "") + fileName + (executingTasks.isEmpty() ? "" : " (Running)"); SwingUtilities.invokeLater(() -> { setTitle(title); // to the main window // Update all tabs: could have changed for (int i = 0; i < tabbed.getTabCount(); i++) tabbed.setTitleAt(i, // ((TextEditorTab) tabbed.getComponentAt(i)).getTitle()); }); } @Override public synchronized void setTitle(final String title) { super.setTitle(title); final int index = tabsMenuTabsStart + tabbed.getSelectedIndex(); if (index < tabsMenu.getItemCount()) { final JMenuItem item = tabsMenu.getItem(index); if (item != null) item.setText(title); } } private final ArrayList<Executer> executingTasks = new ArrayList<>(); /** * Generic Thread that keeps a starting time stamp, sets the priority to * normal and starts itself. */ public abstract class Executer extends ThreadGroup { JTextAreaWriter output, errors; Executer(final JTextAreaWriter output, final JTextAreaWriter errors) { super("Script Editor Run :: " + new Date().toString()); this.output = output; this.errors = errors; // Store itself for later executingTasks.add(this); setTitle(); // Enable kill menu kill.setEnabled(true); // Fork a task, as a part of this ThreadGroup new Thread(this, getName()) { { setPriority(Thread.NORM_PRIORITY); start(); } @Override public void run() { try { execute(); // Wait until any children threads die: int activeCount = getThreadGroup().activeCount(); while (activeCount > 1) { if (isInterrupted()) break; try { Thread.sleep(500); final List<Thread> ts = getAllThreads(); activeCount = ts.size(); if (activeCount <= 1) break; log.debug("Waiting for " + ts.size() + " threads to die"); int count_zSelector = 0; for (final Thread t : ts) { if (t.getName().equals("zSelector")) { count_zSelector++; } log.debug("THREAD: " + t.getName()); } if (activeCount == count_zSelector + 1) { // Do not wait on the stack slice selector thread. break; } } catch (final InterruptedException ie) { /* ignore */ } } } catch (final Throwable t) { handleException(t); } finally { executingTasks.remove(Executer.this); try { if (null != output) output.shutdown(); if (null != errors) errors.shutdown(); } catch (final Exception e) { handleException(e); } // Leave kill menu item enabled if other tasks are running kill.setEnabled(executingTasks.size() > 0); setTitle(); } } }; } /** The method to extend, that will do the actual work. */ abstract void execute(); /** Fetch a list of all threads from all thread subgroups, recursively. */ List<Thread> getAllThreads() { final ArrayList<Thread> threads = new ArrayList<>(); // From all subgroups: final ThreadGroup[] tgs = new ThreadGroup[activeGroupCount() * 2 + 100]; this.enumerate(tgs, true); for (final ThreadGroup tg : tgs) { if (null == tg) continue; final Thread[] ts = new Thread[tg.activeCount() * 2 + 100]; tg.enumerate(ts); for (final Thread t : ts) { if (null == t) continue; threads.add(t); } } // And from this group: final Thread[] ts = new Thread[activeCount() * 2 + 100]; this.enumerate(ts); for (final Thread t : ts) { if (null == t) continue; threads.add(t); } return threads; } /** * Totally destroy/stop all threads in this and all recursive thread * subgroups. Will remove itself from the executingTasks list. */ @SuppressWarnings("deprecation") void obliterate() { try { // Stop printing to the screen if (null != output) output.shutdownNow(); if (null != errors) errors.shutdownNow(); } catch (final Exception e) { log.error(e); } for (final Thread thread : getAllThreads()) { try { thread.interrupt(); Thread.yield(); // give it a chance thread.stop(); } catch (final Throwable t) { log.error(t); } } executingTasks.remove(this); } @Override public String toString() { return getName(); } } /** Returns a list of currently executing tasks */ public List<Executer> getExecutingTasks() { return executingTasks; } public void kill(final Executer executer) { for (int i = 0; i < tabbed.getTabCount(); i++) { final TextEditorTab tab = (TextEditorTab) tabbed.getComponentAt(i); if (executer == tab.getExecuter()) { tab.kill(); break; } } } /** * Query the list of running scripts and provide a dialog to choose one and * kill it. */ public void chooseTaskToKill() { if (executingTasks.size() == 0) { error("\nNo running scripts\n"); return; } commandService.run(KillScript.class, true, "editor", this); } /** Run the text in the textArea without compiling it, only if it's not java. */ public void runText() { runText(false); } public void runText(final boolean selectionOnly) { if (isCompiled()) { if (selectionOnly) { error("Cannot run selection of compiled language!"); return; } if (handleUnsavedChanges(true)) runScript(); else write("Compiled languages must be saved before they can be run."); return; } final ScriptLanguage currentLanguage = getCurrentLanguage(); if (currentLanguage == null) { error("Select a language first!"); // TODO guess the language, if possible. return; } markCompileStart(); try { final TextEditorTab tab = getTab(); tab.showOutput(); execute(selectionOnly); } catch (final Throwable t) { log.error(t); } } /** * Run current script with the batch processor */ public void runBatch() { if (null == getCurrentLanguage()) { error("Select a language first! Also, please note that this option\n" + "requires at least one @File parameter to be declared in the script."); return; } // get script from current tab final String script = getTab().getEditorPane().getText(); if (script.trim().isEmpty()) { error("This option requires at least one @File parameter to be declared."); return; } final ScriptInfo info = new ScriptInfo(context, // "dummy." + getCurrentLanguage().getExtensions().get(0), // new StringReader(script)); batchService.run(info); } /** Invoke in the context of the event dispatch thread. */ private void execute(final boolean selectionOnly) throws IOException { final String text; final TextEditorTab tab = getTab(); if (selectionOnly) { final String selected = tab.getEditorPane().getSelectedText(); if (selected == null) { error("Selection required!"); text = null; } else text = selected + "\n"; // Ensure code blocks are terminated getEditorPane().getErrorHighlighter().setSelectedCodeExecution(true); } else { text = tab.getEditorPane().getText(); } execute(tab, text, false); } /** * Invoke in the context of the event dispatch thread. * * @param tab The {@link TextEditorTab} that is the source of the program to run. * @param text The text expressing the program to run. * @param writeCommandLog Whether to append the {@code text} to a log file for the appropriate language. * @throws IOException Whether there was an issue with piping the command to the executer. */ private void execute(final TextEditorTab tab, final String text, final boolean writeCommandLog) throws IOException { tab.prepare(); final JTextAreaWriter output = new JTextAreaWriter(tab.screen, log); final JTextAreaWriter errors = new JTextAreaWriter(errorScreen, log); final File file = getEditorPane().getFile(); // Pipe current text into the runScript: final PipedInputStream pi = new PipedInputStream(); final PipedOutputStream po = new PipedOutputStream(pi); // The Executer creates a Thread that // does the reading from PipedInputStream tab.setExecutor(new Executer(output, errors) { @Override public void execute() { try { evalScript(file == null ? getEditorPane().getFileName() : file .getAbsolutePath(), new InputStreamReader(pi), output, errors); output.flush(); errors.flush(); markCompileEnd(); // For executions from the prompt if (writeCommandLog && null != text && text.trim().length() > 0) { writePromptLog(getEditorPane().getCurrentLanguage(), text); } } catch (final Throwable t) { output.flush(); errors.flush(); if (t instanceof ScriptException && t.getCause() != null && t.getCause().getClass().getName().endsWith("CompileError")) { errorScreen.append("Compilation failed"); tab.showErrors(); } else { handleException(t); } } finally { tab.restore(); } } }); // Write into PipedOutputStream // from another Thread try { new Thread() { { setPriority(Thread.NORM_PRIORITY); } @Override public void run() { try (final PrintWriter pw = new PrintWriter(po)) { pw.write(text); pw.flush(); // will lock and wait in some cases } } }.start(); } catch (final Throwable t) { log.error(t); } finally { // Re-enable when all text to send has been sent tab.getEditorPane().setEditable(true); } } private String getPromptCommandsFilename(final ScriptLanguage language) { final String name = language.getLanguageName().replace('/', '_'); return System.getProperty("user.home").replace('\\', '/') + "/.scijava/" + name + ".command.log"; } /** * Append the executed prompt command to the end of the language-specific log file, * and then append a separator. * * @param language The language used, to choose the right log file. * @param text The command to append at the end of the log. */ private void writePromptLog(final ScriptLanguage language, final String text) { final String path = getPromptCommandsFilename(language); final File file = new File(path); try { final boolean exists = file.exists(); if (!exists) { // Ensure parent directories exist file.getParentFile().mkdirs(); file.createNewFile(); // atomic } Files.write(Paths.get(path), Arrays.asList(new String[]{text, "#"}), Charset.forName("UTF-8"), StandardOpenOption.APPEND, StandardOpenOption.DSYNC); } catch (final IOException e) { log.error("Failed to write executed prompt command to file " + path, e); } } /** * Parse the prompt command log for the given language and return the last 1000 lines. * If the log is longer than 1000 lines, crop it. * * @param language The language used, to choose the right log file. * @return */ private ArrayList<String> loadPromptLog(final ScriptLanguage language) { final String path = getPromptCommandsFilename(language); final File file = new File(path); final ArrayList<String> lines = new ArrayList<>(); if (!file.exists()) return lines; RandomAccessFile ra = null; List<String> commands = new ArrayList<>(); try { ra = new RandomAccessFile(path, "r"); final byte[] bytes = new byte[(int)ra.length()]; ra.readFully(bytes); final String sep = System.getProperty("line.separator"); // used fy Files.write above commands.addAll(Arrays.asList(new String(bytes, Charset.forName("UTF-8")).split(sep + "#" + sep))); if (0 == commands.get(commands.size()-1).length()) commands.remove(commands.size() -1); // last entry is empty } catch (final IOException e) { log.error("Failed to read history of prompt commands from file " + path, e); return lines; } finally { try { if (null != ra) ra.close(); } catch (final IOException e) { log.error(e); } } if (commands.size() > 1000) { commands = commands.subList(commands.size() - 1000, commands.size()); // Crop the log: otherwise would grow unbounded final ArrayList<String> croppedLog = new ArrayList<>(); for (final String c : commands) { croppedLog.add(c); croppedLog.add("#"); } try { Files.write(Paths.get(path + "-tmp"), croppedLog, Charset.forName("UTF-8"), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.DSYNC); if (!new File(path + "-tmp").renameTo(new File(path))) { log.error("Could not rename command log file " + path + "-tmp to " + path); } } catch (final Exception e) { log.error("Failed to crop history of prompt commands file " + path, e); } } lines.addAll(commands); return lines; } public void runScript() { if (isCompiled()) getTab().showErrors(); else getTab().showOutput(); markCompileStart(); final JTextAreaWriter output = new JTextAreaWriter(getTab().screen, log); final JTextAreaWriter errors = new JTextAreaWriter(errorScreen, log); final File file = getEditorPane().getFile(); new TextEditor.Executer(output, errors) { @Override public void execute() { try (final Reader reader = evalScript(file.getPath(), new FileReader(file), output, errors)) { output.flush(); errors.flush(); markCompileEnd(); } catch (final Throwable e) { handleException(e); } } }; } public void compile() { if (!handleUnsavedChanges(true)) return; final ScriptEngine interpreter = getCurrentLanguage().getScriptEngine(); if (interpreter instanceof JavaEngine) { final JavaEngine java = (JavaEngine) interpreter; final JTextAreaWriter errors = new JTextAreaWriter(errorScreen, log); markCompileStart(); getTab().showErrors(); new Thread(() -> { java.compile(getEditorPane().getFile(), errors); errorScreen.insert("Compilation finished.\n", // errorScreen.getDocument().getLength()); markCompileEnd(); }).start(); } } private String getSelectedTextOrAsk(final String msg, final String title) { String selection = getTextArea().getSelectedText(); if (selection == null || selection.indexOf('\n') >= 0) { return GuiUtils.getString(this, msg + "\nAlternatively, select a class declaration and re-run.", title); } return selection; } private String getSelectedClassNameOrAsk(final String msg, final String title) { String className = getSelectedTextOrAsk(msg, title); if (className != null) className = className.trim(); return className; } private static void append(final JTextArea textArea, final String text) { final int length = textArea.getDocument().getLength(); textArea.insert(text, length); textArea.setCaretPosition(length); } public void markCompileStart() { markCompileStart(true); } public void markCompileStart(final boolean with_timestamp) { errorHandler = null; if (with_timestamp) { final String started = "Started " + getEditorPane().getFileName() + " at " + new Date() + "\n"; append(errorScreen, started); append(getTab().screen, started); } final int offset = errorScreen.getDocument().getLength(); compileStartOffset = errorScreen.getDocument().getLength(); try { compileStartPosition = errorScreen.getDocument().createPosition(offset); } catch (final BadLocationException e) { handleException(e); } ExceptionHandler.addThread(Thread.currentThread(), this); } public void markCompileEnd() { // this naming is not-intuitive at all! if (errorHandler == null) { errorHandler = new ErrorHandler(getCurrentLanguage(), errorScreen, compileStartPosition.getOffset()); if (errorHandler.getErrorCount() > 0) getTab().showErrors(); } if (getEditorPane().getErrorHighlighter().isLogDetailed() && compileStartOffset != errorScreen.getDocument().getLength()) { getTab().showErrors(); } if (getTab().showingErrors) { errorHandler.scrollToVisible(compileStartOffset); } } public boolean nextError(final boolean forward) { if (errorHandler != null && errorHandler.nextError(forward)) try { File file = new File(errorHandler.getPath()); if (!file.isAbsolute()) file = getFileForBasename(file.getName()); errorHandler.markLine(); switchTo(file, errorHandler.getLine()); getTab().showErrors(); errorScreen.invalidate(); return true; } catch (final Exception e) { handleException(e); } return false; } public void switchTo(final String path, final int lineNumber) throws IOException { switchTo(new File(path).getCanonicalFile(), lineNumber); } public void switchTo(final File file, final int lineNumber) { if (!editorPaneContainsFile(getEditorPane(), file)) switchTo(file); SwingUtilities.invokeLater(() -> { try { gotoLine(lineNumber); } catch (final BadLocationException e) { // ignore } }); } public void switchTo(final File file) { for (int i = 0; i < tabbed.getTabCount(); i++) if (editorPaneContainsFile(getEditorPane(i), file)) { switchTo(i); return; } open(file); } public void switchTo(final int index) { if (index == tabbed.getSelectedIndex()) return; tabbed.setSelectedIndex(index); } private void switchTabRelative(final int delta) { final int count = tabbed.getTabCount(); int index = ((tabbed.getSelectedIndex() + delta) % count); if (index < 0) { index += count; } switchTo(index); } private void removeTab(final int index) { final int menuItemIndex = index + tabsMenuTabsStart; try { tabbed.remove(index); tabsMenuItems.remove(tabsMenu.getItem(menuItemIndex)); tabsMenu.remove(menuItemIndex); } catch (final IndexOutOfBoundsException e) { // this should never happen!? log.debug(e); } } boolean editorPaneContainsFile(final EditorPane editorPane, final File file) { try { return file != null && editorPane != null && editorPane.getFile() != null && file.getCanonicalFile().equals(editorPane.getFile().getCanonicalFile()); } catch (final IOException e) { return false; } } public File getFile() { return getEditorPane().getFile(); } public File getFileForBasename(final String baseName) { File file = getFile(); if (file != null && file.getName().equals(baseName)) return file; for (int i = 0; i < tabbed.getTabCount(); i++) { file = getEditorPane(i).getFile(); if (file != null && file.getName().equals(baseName)) return file; } return null; } /** Updates the git directory to the git directory of the current file. */ private void updateGitDirectory() { final EditorPane editorPane = getEditorPane(); editorPane.setGitDirectory(new FileFunctions(this) .getGitDirectory(editorPane.getFile())); } public void addImport(final String className) { if (className != null) { new TokenFunctions(getTextArea()).addImport(className.trim()); } } public void openHelp(final String className) { openHelp(className, true); } /** * @param className * @param withFrames */ public void openHelp(String className, final boolean withFrames) { if (className == null) className = getSelectedClassNameOrAsk("Class (fully qualified name):", "Online Javadocs..."); if (className == null) return; final Class<?> c = Types.load(className, false); final String path = (withFrames ? "index.html?" : "") + // className.replace('.', '/') + ".html"; final String url; if (className.startsWith("java.") || className.startsWith("javax.")) { // Core Java class -- use javadoc.scijava.org/Java<#> link. final String javaVersion = System.getProperty("java.version"); final String majorVersion; if (javaVersion.startsWith("1.")) { majorVersion = javaVersion.substring(2, javaVersion.indexOf('.', 2)); } else majorVersion = javaVersion.substring(0, javaVersion.indexOf('.')); url = "https://javadoc.scijava.org/Java" + majorVersion + "/" + path; } else { // Third party library -- look for a Maven POM identifying it. final POM pom = POM.getPOM(c); if (pom == null) { throw new IllegalArgumentException(// "Unknown origin for class " + className); } final String releaseProfiles = pom.cdata("//properties/releaseProfiles"); final boolean scijavaRepo = "deploy-to-scijava".equals(releaseProfiles); if (scijavaRepo) { // Use javadoc.scijava.org -- try to figure out which project. // Maybe some day, we can bake this information into the POM. final String project; final String g = pom.getGroupId(); if ("net.imagej".equals(g)) { project = "ij".equals(pom.getArtifactId()) ? "ImageJ1" : "ImageJ"; } else if ("io.scif".equals(g)) project = "SCIFIO"; else if ("net.imglib2".equals(g)) project = "ImgLib2"; else if ("org.bonej".equals(g)) project = "BoneJ"; else if ("org.scijava".equals(g)) project = "SciJava"; else if ("sc.fiji".equals(g)) project = "Fiji"; else project = "Java"; url = "https://javadoc.scijava.org/" + project + "/" + path; } else { // Assume Maven Central -- use javadoc.io. url = "https://javadoc.io/static/" + pom.getGroupId() + "/" + // pom.getArtifactId() + "/" + pom.getVersion() + "/" + path; } } try { platformService.open(new URL(url)); } catch (final Throwable e) { handleException(e); } } /** * @param text Either a classname, or a partial class name, or package name or * any part of the fully qualified class name. */ public void openClassOrPackageHelp(String text) { if (text == null) text = getSelectedClassNameOrAsk("Class or package (complete or partial name, e.g., 'ij'):", "Lookup Which Class/Package?"); if (null == text) return; new Thread(new FindClassSourceAndJavadoc(text)).start(); // fork away from event dispatch thread } public class FindClassSourceAndJavadoc implements Runnable { private final String text; public FindClassSourceAndJavadoc(final String text) { this.text = text; } @Override public void run() { setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); final HashMap<String, ArrayList<String>> matches; try { matches = ClassUtil.findDocumentationForClass(text); } finally { setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } if (matches.isEmpty()) { if (confirm("No info found for: '" + text + "'.\nSearch for it on the web?", "Search the Web?", "Search")) { GuiUtils.runSearchQueryInBrowser(TextEditor.this, getPlatformService(), text.trim()); } return; } final JPanel panel = new JPanel(); final GridBagLayout gridbag = new GridBagLayout(); final GridBagConstraints c = new GridBagConstraints(); panel.setLayout(gridbag); //panel.setBorder(BorderFactory.createEmptyBorder(BORDER_SIZE, BORDER_SIZE, BORDER_SIZE, BORDER_SIZE)); final List<String> keys = new ArrayList<String>(matches.keySet()); Collections.sort(keys); c.gridy = 0; for (final String classname: keys) { c.gridx = 0; c.anchor = GridBagConstraints.EAST; final JLabel class_label = new JLabel(classname); gridbag.setConstraints(class_label, c); panel.add(class_label); ArrayList<String> urls = matches.get(classname); if (urls.isEmpty()) { urls = new ArrayList<String>(); urls.add("https://duckduckgo.com/?q=" + classname); } for (final String url: urls) { c.gridx += 1; c.anchor = GridBagConstraints.WEST; String title = "JavaDoc"; if (url.endsWith(".java")) title = "Source"; else if (url.contains("duckduckgo")) title = "Search..."; final JButton link = new JButton(title); gridbag.setConstraints(link, c); panel.add(link); link.addActionListener(event -> { GuiUtils.openURL(TextEditor.this, platformService, url); }); } c.gridy += 1; } final JScrollPane jsp = new JScrollPane(panel); //jsp.setPreferredSize(new Dimension(800, 500)); SwingUtilities.invokeLater(() -> { final JFrame frame = new JFrame("Resources for '" + text +"'"); frame.getContentPane().add(jsp); frame.setLocationRelativeTo(TextEditor.this); frame.pack(); frame.setVisible(true); }); } } public void extractSourceJar() { final File file = openWithDialog(null); if (file != null) extractSourceJar(file); } public void askChatGPTtoGenerateCode() { SwingUtilities.invokeLater(() -> { setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); // setup default prompt String prompt = promptPrefix() .replace("{programming_language}", getCurrentLanguage().getLanguageName() ) .replace("{custom_prompt}", getTextArea().getSelectedText()); String answer = askChatGPT(prompt); if (answer.contains("```")) { // clean answer by removing blabla outside the code block answer = answer.replace("```java", "```"); answer = answer.replace("```javascript", "```"); answer = answer.replace("```python", "```"); answer = answer.replace("```jython", "```"); answer = answer.replace("```macro", "```"); answer = answer.replace("```groovy", "```"); String[] temp = answer.split("```"); answer = temp[1]; } //getTextArea().insert(answer, getTextArea().getCaretPosition()); getTextArea().replaceSelection(answer + "\n"); setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); }); } private String askChatGPT(String text) { // Modified from: https://github.com/TheoKanning/openai-java/blob/main/example/src/main/java/example/OpenAiApiFunctionsExample.java String token = apiKey(); OpenAiService service = new OpenAiService(token); List<ChatMessage> messages = new ArrayList<>(); ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), text); messages.add(userMessage); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder() .model(modelName()) .messages(messages).build(); ChatMessage responseMessage = service.createChatCompletion(chatCompletionRequest).getChoices().get(0).getMessage(); messages.add(responseMessage); String result = responseMessage.getContent(); System.out.println(result); return result; } private String apiKey() { if (optionsService != null) { final OpenAIOptions openAIOptions = optionsService.getOptions(OpenAIOptions.class); if (openAIOptions != null) { final String key = openAIOptions.getOpenAIKey(); if (key != null && !key.isEmpty()) return key; } } return System.getenv("OPENAI_API_KEY"); } private String modelName() { if (optionsService != null) { final OpenAIOptions openAIOptions = optionsService.getOptions(OpenAIOptions.class); if (openAIOptions != null) { final String key = openAIOptions.getModelName(); if (key != null && !key.isEmpty()) return key; } } return null; } private String promptPrefix() { if (optionsService != null) { final OpenAIOptions openAIOptions = optionsService.getOptions(OpenAIOptions.class); if (openAIOptions != null) { final String promptPrefix = openAIOptions.getPromptPrefix(); if (promptPrefix != null && !promptPrefix.isEmpty()) return promptPrefix; } } return ""; } public void extractSourceJar(final File file) { try { final FileFunctions functions = new FileFunctions(this); final File workspace = uiService.chooseFile(new File(System.getProperty("user.home")), FileWidget.DIRECTORY_STYLE); if (workspace == null) return; final List<String> paths = functions.extractSourceJar(file.getAbsolutePath(), workspace); for (final String path : paths) if (!functions.isBinaryFile(path)) { open(new File(path)); final EditorPane pane = getEditorPane(); new TokenFunctions(pane).removeTrailingWhitespace(); if (pane.fileChanged()) save(); } } catch (final IOException e) { error("There was a problem opening " + file + ": " + e.getMessage()); } } /* extensionMustMatch == false means extension must not match */ private File openWithDialog(final File defaultDir) { return uiService.chooseFile(defaultDir, FileWidget.OPEN_STYLE); } /** * Write a message to the output screen * * @param message The text to write */ public void write(String message) { final TextEditorTab tab = getTab(); if (!message.endsWith("\n")) message += "\n"; tab.screen.insert(message, tab.screen.getDocument().getLength()); } public void writeError(String message) { getTab().showErrors(); if (!message.endsWith("\n")) message += "\n"; errorScreen.insert(message, errorScreen.getDocument().getLength()); } void error(final String message) { GuiUtils.error(this, message); } void warn(final String message) { GuiUtils.warn(this, message); } void info(final String message, final String title) { GuiUtils.info(this, message, title); } boolean confirm(final String message, final String title, final String yesButtonLabel) { return GuiUtils.confirm(this, message, title, yesButtonLabel); } void showHTMLDialog(final String title, final String htmlContents) { GuiUtils.showHTMLDialog(this, title, htmlContents); } public void handleException(final Throwable e) { handleException(e, errorScreen); getEditorPane().getErrorHighlighter().parse(e); getTab().showErrors(); } public static void handleException(final Throwable e, final JTextArea textArea) { final CharArrayWriter writer = new CharArrayWriter(); try (final PrintWriter out = new PrintWriter(writer)) { e.printStackTrace(out); for (Throwable cause = e.getCause(); cause != null; cause = cause.getCause()) { out.write("Caused by: "); cause.printStackTrace(out); } } textArea.append(writer.toString()); } /** * Removes invalid characters, shows a dialog. * * @return The amount of invalid characters found. */ public int zapGremlins() { final int count = getEditorPane().zapGremlins(); final String msg = count > 0 ? "Zap Gremlins converted " + count + " invalid characters to spaces" : "No invalid characters found!"; info(msg, "Zap Gremlins"); return count; } // -- Helper methods -- private boolean isCompiled() { final ScriptLanguage language = getCurrentLanguage(); if (language == null) return false; return language.isCompiledLanguage(); } private final class EditableScriptInfo extends ScriptInfo { private String script; public EditableScriptInfo(final Context context, final String path, final Reader reader) { super(context, path, reader); } public void setScript(final Reader reader) throws IOException { final char[] buffer = new char[8192]; final StringBuilder builder = new StringBuilder(); int read; while ((read = reader.read(buffer)) != -1) { builder.append(buffer, 0, read); } this.script = builder.toString(); } @Override public String getProcessedScript() { return null == this.script ? super.getProcessedScript() : this.script; } } private Reader evalScript(final String filename, Reader reader, final Writer output, final JTextAreaWriter errors) throws ModuleException { final ScriptLanguage language = getCurrentLanguage(); // If there's no engine or the language has changed or the language is compiled from a file // then create the engine and module anew if (!this.incremental || (this.incremental && null == this.scriptInfo) || language.isCompiledLanguage() || (null != this.scriptInfo && null != this.scriptInfo.getLanguage() && this.scriptInfo.getLanguage().getLanguageName() != getCurrentLanguage().getLanguageName())) { if (respectAutoImports) { reader = DefaultAutoImporters.prefixAutoImports(context, language, reader, errors); } // create script module for execution this.scriptInfo = new EditableScriptInfo(context, filename, reader); // use the currently selected language to execute the script this.scriptInfo.setLanguage(language); this.module = this.scriptInfo.createModule(); context.inject(this.module); } else { try { // Same engine, with persistent state this.scriptInfo.setScript( reader ); } catch (final IOException e) { log.error(e); } } // map stdout and stderr to the UI this.module.setOutputWriter(output); this.module.setErrorWriter(errors); // prepare the highlighter getEditorPane().getErrorHighlighter().setEnabled(!respectAutoImports); getEditorPane().getErrorHighlighter().reset(); getEditorPane().getErrorHighlighter().setWriter(errors); // execute the script try { moduleService.run(module, true).get(); } catch (final InterruptedException e) { error("Interrupted"); } catch (final ExecutionException e) { log.error(e); } finally { getEditorPane().getErrorHighlighter().parse(); } return reader; } public void setIncremental(final boolean incremental) { if (incremental && null == getCurrentLanguage()) { error("Select a language first!"); return; } this.incremental = incremental; final JTextArea prompt = getTab().getPrompt(); if (incremental) { getTab().setREPLVisible(true); prompt.addKeyListener(new KeyAdapter() { private final ArrayList<String> commands = loadPromptLog(getCurrentLanguage()); private int index = commands.size(); { commands.add(""); // the current prompt text } @Override public void keyPressed(final KeyEvent ke) { int keyCode = ke.getKeyCode(); if (KeyEvent.VK_ENTER == keyCode) { if (ke.isShiftDown() || ke.isAltDown() || ke.isAltGraphDown() || ke.isMetaDown() || ke.isControlDown()) { prompt.insert("\n", prompt.getCaretPosition()); ke.consume(); return; } final String text = prompt.getText(); if (null == text || 0 == text.trim().length()) { ke.consume(); // avoid writing the line break return; } try { final JTextArea screen = getTab().screen; getTab().showOutput(); screen.append("> " + text + "\n"); // Update the last command in the history commands.set(commands.size() -1, prompt.getText()); // Set the next command commands.add(""); index = commands.size() - 1; // Execute markCompileStart(false); // weird method name, execute will call markCompileEnd execute(getTab(), text, true); prompt.setText(""); screen.scrollRectToVisible(screen.modelToView(screen.getDocument().getLength())); } catch (final Throwable t) { log.error(t); prompt.requestFocusInWindow(); } ke.consume(); // avoid writing the line break return; } // If using arrows for navigating history if (getTab().updownarrows.isSelected()) { // Only if no modifiers are down if (!(ke.isShiftDown() || ke.isControlDown() || ke.isAltDown() || ke.isMetaDown())) { switch(keyCode) { case KeyEvent.VK_UP: keyCode = KeyEvent.VK_PAGE_UP; break; case KeyEvent.VK_DOWN: keyCode = KeyEvent.VK_PAGE_DOWN; break; } } } // control+p and control+n for navigating history if (ke.isControlDown()) { switch(keyCode) { case KeyEvent.VK_P: keyCode = KeyEvent.VK_PAGE_UP; break; case KeyEvent.VK_N: keyCode = KeyEvent.VK_PAGE_DOWN; break; } } if (KeyEvent.VK_PAGE_UP == keyCode) { // If last, update the stored command if (commands.size() -1 == index) { commands.set(commands.size() -1, prompt.getText()); } if (index > 0) { prompt.setText(commands.get(--index)); } ke.consume(); return; } else if (KeyEvent.VK_PAGE_DOWN == keyCode) { if (index < commands.size() -1) { prompt.setText(commands.get(++index)); } ke.consume(); return; } // Update index and current command when editing an earlier command if (commands.size() -1 != index) { index = commands.size() -1; commands.set(commands.size() -1, prompt.getText()); } } }); } else { prompt.setText(""); // clear prompt.setEnabled(false); for (final KeyListener kl : prompt.getKeyListeners()) { prompt.removeKeyListener(kl); } getTab().setREPLVisible(false); } } private String adjustPath(final String path, final String langName) { String result = path.replace('_', ' '); // HACK: For templates nested beneath their language name, // place them in a folder called "Uncategorized" instead. This avoids // menu redundancy when existing script templates are populated // under the new Templates menu structure. // // For example, a script at script_templates/BeanShell/Greeting.bsh // was previously placed in: // // Templates > BeanShell > Greeting // // but under the current approach will be placed at: // // Templates > [by language] > BeanShell > BeanShell > Greeting // Templates > BeanShell > Greeting (BeanShell) // // both of which are redundant and annoying. // // This hack instead places that script at: // // Templates > Uncategorized > Greeting (BeanShell) // Templates > [by language] > BeanShell > Uncategorized > Greeting if (langName != null && path.toLowerCase().startsWith(langName.toLowerCase() + "/")) { result = "Uncategorized" + result.substring(langName.length()); } return result; } @Override public boolean confirmClose() { while (tabbed.getTabCount() > 0) { if (!handleUnsavedChanges()) return false; final int index = tabbed.getSelectedIndex(); removeTab(index); } return true; } @Override public void insertUpdate(final DocumentEvent e) { setTitle(); checkForOutsideChanges(); } @Override public void removeUpdate(final DocumentEvent e) { setTitle(); checkForOutsideChanges(); } @Override public void changedUpdate(final DocumentEvent e) { setTitle(); } public void setFontSize(final float size) { if (getEditorPane().getFontSize() != size) getEditorPane().setFontSize(size); changeFontSize(errorScreen, size); changeFontSize(getTab().screen, size); changeFontSize(getTab().prompt, size); updateTabAndFontSize(false); tree.setFont(tree.getFont().deriveFont(size)); } private void changeFontSize(final JTextArea a, final float size) { a.setFont(a.getFont().deriveFont(size)); } private void appendPreferences(final JMenu menu) { JMenuItem item = new JMenuItem("Save Preferences"); menu.add(item); item.addActionListener(e -> { getEditorPane().savePreferences(tree.getTopLevelFoldersString(), activeTheme); saveWindowSizeToPrefs(); write("Script Editor: Preferences Saved...\n"); }); item = new JMenuItem("Reset..."); menu.add(item); item.addActionListener(e -> { if (confirm("Reset preferences to defaults? (a restart may be required)", "Reset?", "Reset")) { resetLayout(); prefService.clear(EditorPane.class); prefService.clear(TextEditor.class); write("Script Editor: Preferences Reset. Restart is recommended\n"); } }); } private JMenu helpMenu() { final JMenu menu = new JMenu("Help"); GuiUtils.addMenubarSeparator(menu, "Offline Help:"); JMenuItem item = new JMenuItem("List Shortcuts..."); item.addActionListener(e -> displayKeyMap()); menu.add(item); item = new JMenuItem("List Recordable Actions..."); item.addActionListener(e -> displayRecordableMap()); menu.add(item); item = new JMenuItem("Task Tags How-To..."); item.addActionListener(e -> { showHTMLDialog("Task Tags Help", // "<p>" + "When inserted in source code comments, the following keywords will automatically " // + "register task definitions on the rightmost side of the Editor: <code>TODO</code>, " // + "<code>README</code>, and <code>HACK</code>.</p>" // + "<ul>" // + "<li>To add a task, simply type one of the keywords in a commented line, e.g., "// + "<code>TODO</code></li>"// + "<li>To remove a task, delete the keyword from the comment</li>" // + "<li>Mouse over the annotation mark to access a summary of the task</li>" // + "<li>Click on the mark to go to the annotated line</li>" + "</ul>"); }); menu.add(item); GuiUtils.addMenubarSeparator(menu, "Contextual Help:"); menu.add(openHelpWithoutFrames); openHelpWithoutFrames.setMnemonic(KeyEvent.VK_O); menu.add(openHelp); openClassOrPackageHelp = addToMenu(menu, "Lookup Class or Package...", 0, 0); openClassOrPackageHelp.setMnemonic(KeyEvent.VK_S); menu.add(openMacroFunctions); GuiUtils.addMenubarSeparator(menu, "Online Resources:"); menu.add(helpMenuItem("Image.sc Forum ", "https://forum.image.sc/")); menu.add(helpMenuItem("ImageJ Search Portal", "https://search.imagej.net/")); //menu.addSeparator(); menu.add(helpMenuItem("SciJava Javadoc Portal", "https://javadoc.scijava.org/")); menu.add(helpMenuItem("SciJava Maven Repository", "https://maven.scijava.org/")); menu.addSeparator(); menu.add(helpMenuItem("Fiji on GitHub", "https://github.com/fiji")); menu.add(helpMenuItem("SciJava on GitHub", "https://github.com/scijava/")); menu.addSeparator(); menu.add(helpMenuItem("ImageJ Macro Functions", "https://imagej.nih.gov/ij/developer/macro/functions.html")); menu.add(helpMenuItem("ImageJ Docs: Development", "https://imagej.net/develop/")); menu.add(helpMenuItem("ImageJ Docs: Scripting", "https://imagej.net/scripting/")); menu.addSeparator(); menu.add(helpMenuItem("ImageJ Notebook Tutorials", "https://github.com/imagej/tutorials#readme")); return menu; } private JMenuItem helpMenuItem(final String label, final String url) { final JMenuItem item = new JMenuItem(label); item.addActionListener(e -> GuiUtils.openURL(TextEditor.this, platformService, url)); return item; } protected void applyConsolePopupMenu(final JTextArea textArea) { final JPopupMenu popup = new JPopupMenu(); textArea.setComponentPopupMenu(popup); final String scope = ((textArea == errorScreen) ? "Errors..." : "Outputs..."); JMenuItem jmi = new JMenuItem("Search " + scope); popup.add(jmi); jmi.addActionListener(e -> { findDialog.setLocationRelativeTo(this); findDialog.setRestrictToConsole(true); final String text = textArea.getSelectedText(); if (text != null) findDialog.setSearchPattern(text); // Ensure the right pane is visible in case search is being // triggered by CmdPalette if (textArea == errorScreen && !getTab().showingErrors) getTab().showErrors(); else if (getTab().showingErrors) getTab().showOutput(); findDialog.show(false); }); cmdPalette.register(jmi, scope); jmi = new JMenuItem("Search Script for Selected Text..."); popup.add(jmi); jmi.addActionListener(e -> { final String text = textArea.getSelectedText(); if (text == null) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); } else { findDialog.setLocationRelativeTo(this); findDialog.setRestrictToConsole(false); if (text != null) findDialog.setSearchPattern(text); findDialog.show(false); } }); popup.addSeparator(); jmi = new JMenuItem("Clear Selected Text"); popup.add(jmi); jmi.addActionListener(e -> { if (textArea.getSelectedText() == null) UIManager.getLookAndFeel().provideErrorFeedback(textArea); else textArea.replaceSelection(""); }); final DefaultHighlighter highlighter = (DefaultHighlighter)textArea.getHighlighter(); highlighter.setDrawsLayeredHighlights(false); jmi = new JMenuItem("Highlight Selected Text"); popup.add(jmi); jmi.addActionListener(e -> { try { final Color taint = (textArea == errorScreen) ? Color.RED : textArea.getSelectionColor(); final Color color = ErrorParser.averageColors(textArea.getBackground(), taint); final DefaultHighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(color); textArea.getHighlighter().addHighlight(textArea.getSelectionStart(), textArea.getSelectionEnd(), painter); textArea.setCaretPosition(textArea.getSelectionEnd()); textArea.getHighlighter(); } catch (final BadLocationException ignored) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); } }); jmi = new JMenuItem("Clear Highlights"); popup.add(jmi); jmi.addActionListener(e -> textArea.getHighlighter().removeAllHighlights()); cmdPalette.register(jmi, scope); popup.addSeparator(); final JCheckBoxMenuItem jmc = new JCheckBoxMenuItem("Wrap Lines"); popup.add(jmc); jmc.addActionListener( e -> textArea.setLineWrap(jmc.isSelected())); cmdPalette.register(jmc, scope); } private static Collection<File> assembleFlatFileCollection(final Collection<File> collection, final File[] files) { if (files == null) return collection; // can happen while pressing 'Esc'!? for (final File file : files) { if (file == null || isBinary(file)) continue; else if (file.isDirectory()) assembleFlatFileCollection(collection, file.listFiles()); else //if (!file.isHidden()) collection.add(file); } return collection; } protected static class GuiUtils { private GuiUtils() { } static void error(final Component parent, final String message) { JOptionPane.showMessageDialog(parent, message, "Error", JOptionPane.ERROR_MESSAGE); } static void warn(final Component parent, final String message) { JOptionPane.showMessageDialog(parent, message, "Warning", JOptionPane.WARNING_MESSAGE); } static void info(final Component parent, final String message, final String title) { JOptionPane.showMessageDialog(parent, message, title, JOptionPane.INFORMATION_MESSAGE); } static boolean confirm(final Component parent, final String message, final String title, final String yesButtonLabel) { return JOptionPane.showConfirmDialog(parent, message, title, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION; } static void showHTMLDialog(final Component parent, final String title, final String htmlContents) { final JTextPane f = new JTextPane(); f.setContentType("text/html"); f.setEditable(false); f.setBackground(null); f.setBorder(null); f.setText(htmlContents); f.setCaretPosition(0); final JScrollPane sp = new JScrollPane(f); final JOptionPane pane = new JOptionPane(sp); final JDialog dialog = pane.createDialog(parent, title); dialog.setResizable(true); dialog.pack(); dialog.setPreferredSize( new Dimension( (int) Math.min(parent.getWidth() * .5, pane.getPreferredSize().getWidth() + (3 * sp.getVerticalScrollBar().getWidth())), (int) Math.min(parent.getHeight() * .8, pane.getPreferredSize().getHeight()))); dialog.pack(); pane.addPropertyChangeListener(JOptionPane.VALUE_PROPERTY, ignored -> { dialog.dispose(); }); dialog.setModal(false); dialog.setLocationRelativeTo(parent); dialog.setVisible(true); } static String getString(final Component parent, final String message, final String title) { return (String) JOptionPane.showInputDialog(parent, message, title, JOptionPane.QUESTION_MESSAGE); } static void runSearchQueryInBrowser(final Component parentComponent, final PlatformService platformService, final String query) { String url; try { url = "https://forum.image.sc/search?q=" + URLEncoder.encode(query, StandardCharsets.UTF_8.toString()); } catch (final Exception ignored) { url = query.trim().replace(" ", "%20"); } openURL(parentComponent, platformService, url); } static void openURL(final Component parentComponent, final PlatformService platformService, final String url) { try { platformService.open(new URL(url)); } catch (final Exception ignored) { // Error message with selectable text final JTextPane f = new JTextPane(); f.setContentType("text/html"); f.setText("<HTML>Web page could not be open. Please visit<br>" + url + "<br>using your web browser."); f.setEditable(false); f.setBackground(null); f.setBorder(null); JOptionPane.showMessageDialog(parentComponent, f, "Error", JOptionPane.ERROR_MESSAGE); } } static void openTerminal(final File pwd) throws IOException, InterruptedException { final String[] wrappedCommand; final File dir = (pwd.isDirectory()) ? pwd : pwd.getParentFile(); if (PlatformUtils.isWindows()) { // this should probably be replaced with powershell!? wrappedCommand = new String[] { "cmd", "/c", "start", "/wait", "cmd.exe", "/K" }; } else if (PlatformUtils.isLinux()) { // this will likely only work on debian-based distros wrappedCommand = new String[] { "/usr/bin/x-terminal-emulator" }; } else if (PlatformUtils.isMac()) { // On MacOS ProcessBuilder#directory() fails!? so we'll use applescript :) wrappedCommand = new String[] { "osascript", "-e", // "tell application \"Terminal\" to (do script \"cd '" + dir.getAbsolutePath() + "';clear\")" }; } else { throw new IllegalArgumentException("Unsupported OS"); } final ProcessBuilder pb = new ProcessBuilder(wrappedCommand); pb.directory(dir); // does nothing on macOS !? pb.start(); } static void addMenubarSeparator(final JMenu menu, final String header) { if (menu.getMenuComponentCount() > 0) { menu.addSeparator(); } try { final JLabel label = new JLabel(" "+ header); label.setEnabled(false); label.setForeground(getDisabledComponentColor()); menu.add(label); } catch (final Exception ignored) { // do nothing } } static void addPopupMenuSeparator(final JPopupMenu menu, final String header) { if (menu.getComponentCount() > 1) { menu.addSeparator(); } final JLabel label = new JLabel(header); // label.setHorizontalAlignment(SwingConstants.LEFT); label.setEnabled(false); label.setForeground(getDisabledComponentColor()); menu.add(label); } static Color getDisabledComponentColor() { try { return UIManager.getColor("MenuItem.disabledForeground"); } catch (final Exception ignored) { return Color.GRAY; } } static boolean isDarkLaF() { return FlatLaf.isLafDark() || isDark(new JLabel().getBackground()); } static boolean isDark(final Color c) { // see https://stackoverflow.com/a/3943023 return (c.getRed() * 0.299 + c.getGreen() * 0.587 + c.getBlue() * 0.114) < 186; } static void collapseAllTreeNodes(final JTree tree) { final int row1 = (tree.isRootVisible()) ? 1 : 0; for (int i = row1; i < tree.getRowCount(); i++) tree.collapseRow(i); } static void expandAllTreeNodes(final JTree tree) { for (int i = 0; i < tree.getRowCount(); i++) tree.expandRow(i); } /* Tweaks for tabbed pane */ static JTabbedPane getJTabbedPane() { final JTabbedPane tabbed = new JTabbedPane(); final JPopupMenu popup = new JPopupMenu(); tabbed.setComponentPopupMenu(popup); final ButtonGroup bGroup = new ButtonGroup(); for (final String pos : new String[] { "Top", "Left", "Bottom", "Right" }) { final JMenuItem jcbmi = new JCheckBoxMenuItem("Place on " + pos, "Top".equals(pos)); jcbmi.addItemListener(e -> { switch (pos) { case "Top": tabbed.setTabPlacement(JTabbedPane.TOP); break; case "Bottom": tabbed.setTabPlacement(JTabbedPane.BOTTOM); break; case "Left": tabbed.setTabPlacement(JTabbedPane.LEFT); break; case "Right": tabbed.setTabPlacement(JTabbedPane.RIGHT); break; } }); bGroup.add(jcbmi); popup.add(jcbmi); } tabbed.addMouseWheelListener(e -> { // https://stackoverflow.com/a/38463104 final JTabbedPane pane = (JTabbedPane) e.getSource(); final int units = e.getWheelRotation(); final int oldIndex = pane.getSelectedIndex(); final int newIndex = oldIndex + units; if (newIndex < 0) pane.setSelectedIndex(0); else if (newIndex >= pane.getTabCount()) pane.setSelectedIndex(pane.getTabCount() - 1); else pane.setSelectedIndex(newIndex); }); return tabbed; } } static class TextFieldWithPlaceholder extends JTextField { private static final long serialVersionUID = 1L; private String placeholder; void setPlaceholder(final String placeholder) { this.placeholder = placeholder; update(getGraphics()); } Font getPlaceholderFont() { return getFont().deriveFont(Font.ITALIC); } String getPlaceholder() { return placeholder; } @Override protected void paintComponent(final java.awt.Graphics g) { super.paintComponent(g); if (getText().isEmpty()) { final Graphics2D g2 = (Graphics2D) g.create(); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setColor(getDisabledTextColor()); g2.setFont(getPlaceholderFont()); g2.drawString(getPlaceholder(), getInsets().left, g2.getFontMetrics().getHeight() + getInsets().top - getInsets().bottom); g2.dispose(); } } } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value" ]
[((12049, 12101), 'java.awt.Toolkit.getDefaultToolkit'), ((14269, 14313), 'java.awt.Desktop.getDesktop'), ((29795, 29847), 'java.awt.Toolkit.getDefaultToolkit'), ((34201, 34253), 'org.fife.ui.rtextarea.RTextArea.getCurrentMacro'), ((34201, 34246), 'org.fife.ui.rtextarea.RTextArea.getCurrentMacro'), ((34718, 34768), 'org.fife.ui.rtextarea.RTextArea.getCurrentMacro'), ((36306, 36358), 'org.fife.ui.rtextarea.RTextArea.getCurrentMacro'), ((36306, 36351), 'org.fife.ui.rtextarea.RTextArea.getCurrentMacro'), ((45393, 45436), 'java.awt.Toolkit.getDefaultToolkit'), ((49328, 49386), 'org.fife.ui.rtextarea.RTextAreaEditorKit.clipboardHistoryAction.equals'), ((49396, 49441), 'org.fife.ui.rtextarea.ClipboardHistory.get'), ((49396, 49431), 'org.fife.ui.rtextarea.ClipboardHistory.get'), ((111004, 111032), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((132682, 132715), 'java.nio.charset.StandardCharsets.UTF_8.toString')]
package com.chatbot.Sahayakam.service; import java.time.Duration; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; import jakarta.annotation.PostConstruct; @Service public class ChatbotAPIService { private static OpenAiService openAiService; @Value("${openai.key}") private String apiKey; @Value("${openai.timeout}") private int apiTimeout; private static final String GPT_MODEL = "gpt-3.5-turbo"; private static final String SYSTEM_TASK_MESSAGE = "You are a helpful assistant."; @PostConstruct public void initchatbotAPIService() { openAiService = new OpenAiService(apiKey, Duration.ofSeconds(apiTimeout)); } public String sendMessage(String message) { ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder().model(GPT_MODEL).temperature(0.8) .messages(List.of(new ChatMessage("system", SYSTEM_TASK_MESSAGE), new ChatMessage("user", message))) .build(); StringBuilder builder = new StringBuilder(); System.out.println("Calling createChatCompletion method " + message); openAiService.createChatCompletion(chatCompletionRequest).getChoices().forEach(choice -> { builder.append(choice.getMessage().getContent()); }); return builder.toString(); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1115, 1298), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1115, 1285), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1115, 1180), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1115, 1163), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package io.dockstore.topicgenerator.helper; import com.knuddels.jtokkit.api.Encoding; import com.knuddels.jtokkit.api.EncodingRegistry; import com.knuddels.jtokkit.api.ModelType; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessageRole; import java.util.List; public final class OpenAIHelper { private OpenAIHelper() { } /** * Adapted from <a href="https://jtokkit.knuddels.de/docs/getting-started/recipes/chatml">OpenAI cookbook example</a>. * Counts the number of tokens in a list of messages, accounting for additional tokens that are added to the input text. * * @param registry * @param model * @param messages consists of role, content and an optional name * @return */ @SuppressWarnings("checkstyle:magicnumber") public static int countMessageTokens(EncodingRegistry registry, String model, List<ChatMessage> messages) { Encoding encoding = registry.getEncodingForModel(model).orElseThrow(); int tokensPerMessage; int tokensPerName; if (model.startsWith("gpt-4")) { tokensPerMessage = 3; tokensPerName = 1; } else if (model.startsWith("gpt-3.5-turbo")) { tokensPerMessage = 4; // every message follows <|start|>{role/name}\n{content}<|end|>\n tokensPerName = -1; // if there's a name, the role is omitted } else { throw new IllegalArgumentException("Unsupported model: " + model); } int sum = 0; for (ChatMessage message : messages) { sum += tokensPerMessage; sum += encoding.countTokens(message.getContent()); sum += encoding.countTokens(message.getRole()); if (message.getName() != null) { sum += encoding.countTokens(message.getName()); sum += tokensPerName; } } sum += 3; // every reply is primed with <|start|>assistant<|message|> return sum; } /** * Returns the maximum amount of tokens that the user message content can contain. Assumes that there is one system message and one user message. * @param systemMessage * @param maxResponseToken * @return */ public static int getMaximumAmountOfTokensForUserMessageContent(EncodingRegistry registry, ModelType aiModel, int maxContextLength, ChatMessage systemMessage, int maxResponseToken) { ChatMessage userMessageWithoutContent = new ChatMessage(ChatMessageRole.USER.value()); List<ChatMessage> messages = List.of(systemMessage, userMessageWithoutContent); final int tokenCountWithoutUserContent = countMessageTokens(registry, aiModel.getName(), messages); return maxContextLength - maxResponseToken - tokenCountWithoutUserContent; } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value" ]
[((2529, 2557), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value')]
package com.theokanning.openai; 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; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.image.CreateImageRequest; import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; class OpenAiApiExample { public static void main(String... args) { String token = System.getenv("OPENAI_TOKEN"); OpenAiService service = new OpenAiService(token, Duration.ofSeconds(30)); System.out.println("\nCreating completion..."); CompletionRequest completionRequest = CompletionRequest.builder() .model("ada") .prompt("Somebody once told me the world is gonna roll me") .echo(true) .user("testing") .n(3) .build(); service.createCompletion(completionRequest).getChoices().forEach(System.out::println); System.out.println("\nCreating Image..."); CreateImageRequest request = CreateImageRequest.builder() .prompt("A cow breakdancing with a turtle") .build(); System.out.println("\nImage is located at:"); System.out.println(service.createImage(request).getData().get(0).getUrl()); System.out.println("Streaming chat completion..."); final List<ChatMessage> messages = new ArrayList<>(); final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), "You are a dog and will speak as such."); messages.add(systemMessage); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder() .model("gpt-3.5-turbo") .messages(messages) .n(1) .maxTokens(50) .logitBias(new HashMap<>()) .build(); service.streamChatCompletion(chatCompletionRequest) .doOnError(Throwable::printStackTrace) .blockingForEach(System.out::println); service.shutdownExecutor(); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.image.CreateImageRequest.builder", "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((809, 1050), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((809, 1025), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((809, 1003), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((809, 970), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((809, 942), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((809, 866), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1236, 1349), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((1236, 1324), 'com.theokanning.openai.image.CreateImageRequest.builder'), ((1671, 1701), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value')]
package com.run.tu.openai; import com.alibaba.fastjson.JSONObject; import com.run.tu.core.result.GlobalResult; import com.run.tu.core.result.GlobalResultGenerator; import com.run.tu.entity.User; import com.run.tu.openai.entity.ChatMessageModel; import com.run.tu.openai.service.OpenAiService; import com.run.tu.openai.service.SseService; import com.run.tu.util.Html2TextUtil; import com.run.tu.util.UserUtils; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import org.apache.commons.lang.StringUtils; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * Created on 2023/2/15 10:04. * * @author ronger * @email ronger-x@outlook.com * @desc : com.run.tu.openai */ @RestController @RequestMapping("/api/v1/openai") public class OpenAiController { @Resource private SseService sseService; @Value("${openai.token}") private String token; @PostMapping("/chat") public GlobalResult chat(@RequestBody JSONObject jsonObject) { String message = jsonObject.getString("message"); if (StringUtils.isBlank(message)) { throw new IllegalArgumentException("参数异常!"); } User user = UserUtils.getCurrentUserByToken(); ChatMessage chatMessage = new ChatMessage("user", message); List<ChatMessage> list = new ArrayList<>(4); list.add(chatMessage); return sendMessage(user, list); } @PostMapping("/new-chat") public GlobalResult newChat(@RequestBody List<ChatMessageModel> messages) { if (messages.isEmpty()) { throw new IllegalArgumentException("参数异常!"); } User user = UserUtils.getCurrentUserByToken(); Collections.reverse(messages); List<ChatMessage> list = new ArrayList<>(messages.size()); if (messages.size() > 4) { messages = messages.subList(messages.size() - 4, messages.size()); } if (messages.size() >= 4 && messages.size() % 4 == 0) { ChatMessage message = new ChatMessage("system", "简单总结一下你和用户的对话, 用作后续的上下文提示 prompt, 控制在 200 字内"); list.add(message); } messages.forEach(chatMessageModel -> { ChatMessage message = new ChatMessage(chatMessageModel.getRole(), Html2TextUtil.getContent(chatMessageModel.getContent())); list.add(message); }); return sendMessage(user, list); } @NotNull private GlobalResult sendMessage(User user, List<ChatMessage> list) { OpenAiService service = new OpenAiService(token, Duration.ofSeconds(180)); ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() .model("gpt-3.5-turbo-16k-0613") .stream(true) .messages(list) .build(); service.streamChatCompletion(completionRequest).doOnError(Throwable::printStackTrace) .blockingForEach(chunk -> { String text = chunk.getChoices().get(0).getMessage().getContent(); if (text == null) { return; } System.out.print(text); sseService.send(user.getIdUser(), text); }); service.shutdownExecutor(); return GlobalResultGenerator.genSuccessResult(); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((3178, 3345), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3178, 3320), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3178, 3288), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3178, 3258), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package com.example.mobilesolomon.service; import com.example.mobilesolomon.data.ApiReader; import com.example.mobilesolomon.data.ILogRepository; import com.theokanning.openai.OpenAiService; import com.theokanning.openai.completion.CompletionChoice; import com.theokanning.openai.completion.CompletionRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class HintService implements IHintService { private ILogRepository hintLogRepos; private String API_KEY; private String prompt; // ChatGPTに送るプロンプト private String hint_madeByGPT; // ChatGPTから返ってきたヒント //  ここでapiをたたく、レスポンスをうけとる @Autowired public HintService(ILogRepository hintLogRepos) { this.hintLogRepos = hintLogRepos; } @Override public void register(String question, String opt1, String opt2, String opt3, String opt4, String answer, String p) { // 現在日時を取得 //LocalDateTime nowDate = LocalDateTime.now(); // 表示形式を指定 //DateTimeFormatter dtf3 = // DateTimeFormatter.ofPattern("yyyy/MM/dd"); //String formatNowDate = dtf3.format(nowDate); //System.out.println(formatNowDate); // 230914 // 通し番号 int number = hintLogRepos.selectMaxNum() + 1; // APIキーを取得している ApiReader apiReader = new ApiReader(); String API_KEY = apiReader.getAPI_KEY(); // ライブラリを利用して、インスタンスを生成 final var service = new OpenAiService(API_KEY); // プロンプト this.prompt = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever.\nHuman: " + p + "\nAI: "; // ここでリクエストをしている 生成された選択肢をListに格納している。 final var completionRequest = CompletionRequest.builder() .model("text-davinci-003") .prompt(prompt) .maxTokens(256) .build(); final var completionResult = service.createCompletion(completionRequest); final var choiceList = completionResult.getChoices(); // 出力 for (final CompletionChoice choice : choiceList) { System.out.println("【DEBUG】chatGPTが作ったヒント: \n" + choice.getText()); hint_madeByGPT = choice.getText(); } // データベースに保存 int n = hintLogRepos.insert(number, question, opt1, opt2, opt3, opt4, answer, hint_madeByGPT); System.out.println("【DEBUG】正常に記録できました"); } // ここからしたプレビュー用 // データベースからヒント取ってくる(プレビュー用) @Override public String getHint() { int num = hintLogRepos.selectMaxNum(); return hintLogRepos.selectHint(num); } @Override public String getQ(){ int n = hintLogRepos.selectMaxNum(); return hintLogRepos.selectQ(n); } @Override public String getOpt1(){ int n = hintLogRepos.selectMaxNum(); return hintLogRepos.selectOpt1(n); } @Override public String getOpt2(){ int n = hintLogRepos.selectMaxNum(); return hintLogRepos.selectOpt2(n); } @Override public String getOpt3(){ int n = hintLogRepos.selectMaxNum(); return hintLogRepos.selectOpt3(n); } @Override public String getOpt4(){ int n = hintLogRepos.selectMaxNum(); return hintLogRepos.selectOpt4(n); } @Override public String getAns(){ int n = hintLogRepos.selectMaxNum(); return hintLogRepos.selectAns(n); } // ヒント更新(修正) @Override public boolean updateHint(String newHint) { int n = hintLogRepos.selectMaxNum(); return hintLogRepos.update(newHint ,n); } // debug用 APIキーがゲッターで呼び出せることを確認できた // public static void main(String[] args) { // HintApiReader apiReader = new HintApiReader(); // System.out.println(apiReader.getAPI_KEY()); // } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((2028, 2187), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2028, 2162), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2028, 2130), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2028, 2098), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package IA; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.service.OpenAiService; public class TesteGPT { private final static String API_KEY = "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"; public static void main(String[] args) { OpenAiService service = new OpenAiService(API_KEY); CompletionRequest request = CompletionRequest.builder() .model("text-davinci-003") .prompt("Vale a pena comprar um MacBook?") .maxTokens(10) .temperature(0.0) .build(); System.out.println(service.createCompletion(request).getChoices()); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((391, 610), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((391, 585), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((391, 551), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((391, 520), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((391, 461), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package tech.ailef.jpromptmanager.completion; import java.time.Duration; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import com.theokanning.openai.OpenAiHttpException; import com.theokanning.openai.completion.CompletionChoice; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.completion.CompletionResult; import com.theokanning.openai.service.OpenAiService; import tech.ailef.jpromptmanager.exceptions.JPromptManagerException; /** * An implementation of the LLMConnector that allows to make requests * to the GPT-3 OpenAI endpoints. * */ public class OpenAIGPT3Connector implements LLMConnector { private OpenAiService service; private String model; private int maxRetries; /** * Builds the connector with the provided parameters. * @param apiKey OpenAI secret key * @param timeout timeout for requests, 0 means no timeout * @param model the OpenAI model to use (this setting will be overridden if an individual `<step>` tag provides a different value) */ public OpenAIGPT3Connector(String apiKey, int timeout, String model) { this(apiKey, timeout, 0, model); } /** * Builds the connector with the provided parameters. * @param apiKey OpenAI secret key * @param timeout timeout for requests, 0 means no timeout * @param model the OpenAI model to use (this setting will be overridden if an individual `<step>` tag provides a different value) * @param maxRetries the number of times to retry a request that fails, default 0 */ public OpenAIGPT3Connector(String apiKey, int timeout, int maxRetries, String model) { if (model == null) throw new JPromptManagerException("Must specify which OpenAI model to use"); this.maxRetries = maxRetries; this.service = new OpenAiService(apiKey, Duration.ofSeconds(timeout)); this.model = model; } /** * Requests a completion for the given text/params to OpenAI. */ @Override public String complete(String prompt, Map<String, String> params) { // Cast parameters to correct type double temperature = Double.parseDouble(params.get("temperature")); double topP = Double.parseDouble(params.get("topP")); String model = params.get("model"); int maxTokens = Integer.parseInt(params.get("maxTokens")); int tries = 1; while (tries <= maxRetries) { try { CompletionRequest completionRequest = CompletionRequest.builder() .prompt(prompt) .stop(Arrays.asList(LLMConnector.PROMPT_TOKEN)) .temperature(temperature) .topP(topP) .model(model) .maxTokens(maxTokens) .n(1) .echo(false) .build(); CompletionResult createCompletion = service.createCompletion(completionRequest); CompletionChoice choice = createCompletion.getChoices().get(0); return choice.getText(); } catch (OpenAiHttpException e) { System.err.println("On try " + tries + ", got exception: " + e.getMessage()); tries++; try { Thread.sleep(2000); } catch (InterruptedException e1) { throw new RuntimeException(e); } } } throw new RuntimeException("Request failed after " + tries + " retries"); } @Override public Map<String, String> getDefaultParams() { Map<String, String> params = new HashMap<>(); params.put("model", model); params.put("temperature", "0"); params.put("topP", "1"); params.put("maxTokens", "256"); return params; } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((2384, 2685), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2384, 2664), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2384, 2639), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2384, 2621), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2384, 2587), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2384, 2561), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2384, 2537), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2384, 2499), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((2384, 2439), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package com.keeping.openaiservice.api.controller.request; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import lombok.Builder; import lombok.Data; import java.util.List; @Data @Builder public class GPTCompletionChatRequest { private String role; private String message; public static ChatCompletionRequest of(GPTCompletionChatRequest request) { return ChatCompletionRequest.builder() .model("gpt-3.5-turbo") .messages(convertChatMessage(request)) .temperature(0.8) .maxTokens(500) .build(); } private static List<ChatMessage> convertChatMessage(GPTCompletionChatRequest request) { return List.of(new ChatMessage(request.getRole(), request.getMessage())); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((468, 685), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((468, 660), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((468, 628), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((468, 594), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((468, 539), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package podsofkon; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; import com.oracle.bmc.aivision.AIServiceVisionClient; import com.oracle.bmc.aivision.model.*; import com.oracle.bmc.aivision.requests.AnalyzeImageRequest; import com.oracle.bmc.aivision.responses.AnalyzeImageResponse; import com.oracle.bmc.auth.AuthenticationDetailsProvider; import com.oracle.bmc.auth.InstancePrincipalsAuthenticationDetailsProvider; import com.theokanning.openai.completion.chat.ChatCompletionChoice; 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; import lombok.Data; import org.asynchttpclient.AsyncHttpClient; import org.asynchttpclient.DefaultAsyncHttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.*; import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.concurrent.atomic.AtomicReference; import org.json.JSONArray; import org.json.JSONObject; import podsofkon.oci.AuthDetailsProviderFactory; import static podsofkon.OpenAIController.openAIToken; import static podsofkon.XRApplication.REGION; @RestController @RequestMapping("/health") public class ExplainAndAdviseOnHealthTestResults { private static Logger log = LoggerFactory.getLogger(ExplainAndAdviseOnHealthTestResults.class); @GetMapping("/form") public String form(){ return " <html><form method=\"post\" action=\"/health/analyzedoc\" enctype=\"multipart/form-data\">\n" + " Select an image file to conduct object detection upon...\n" + " <input type=\"file\" name=\"file\" accept=\"image/*\">\n" + " <br>\n" + " <br>Hit submit and a raw JSON return of objects detected and other info will be returned...\n" + " <br><input type=\"submit\" value=\"Send Request to Vision AI\">\n" + " </form></html>"; } String openAIGPT(String textcontent) { try { OpenAiService service = new OpenAiService(openAIToken, Duration.ofSeconds(60)); System.out.println("Streaming chat completion... textcontent:" + textcontent); final List<ChatMessage> messages = new ArrayList<>(); final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), textcontent); messages.add(systemMessage); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder() .model("gpt-3.5-turbo") .messages(messages) .n(1) .maxTokens(300) //was 50 .logitBias(new HashMap<>()) .build(); String replyString = ""; String content; for (ChatCompletionChoice choice : service.createChatCompletion(chatCompletionRequest).getChoices()) { content = choice.getMessage().getContent(); replyString += (content == null ? " " : content); } service.shutdownExecutor(); return replyString; } catch (Exception exception) { exception.printStackTrace(); return exception.getMessage(); } } public String cohere( String textcontent) throws Exception { AtomicReference<String> resons = new AtomicReference<>(""); AsyncHttpClient client = new DefaultAsyncHttpClient(); client.prepare("POST", "https://api.cohere.ai/v1/summarize") .setHeader("accept", "application/json") .setHeader("content-type", "application/json") .setHeader("authorization", "Bearer oJfPT7nsadfJi1VRz7") .setBody("{\"length\":\"medium\",\"format\":\"paragraph\",\"model\":\"summarize-xlarge\",\"extractiveness\":\"low\",\"temperature\":0.3," + "\"text\":\"" + textcontent + "\"}") // .setBody("{\"length\":\"medium\",\"format\":\"paragraph\",\"model\":\"summarize-xlarge\",\"extractiveness\":\"low\",\"temperature\":0.3,\"text\":\"Ice cream is a sweetened frozen food typically eaten as a snack or dessert. It may be made from milk or cream and is flavoured with a sweetener, either sugar or an alternative, and a spice, such as cocoa or vanilla, or with fruit such as strawberries or peaches. It can also be made by whisking a flavored cream base and liquid nitrogen together. Food coloring is sometimes added, in addition to stabilizers. The mixture is cooled below the freezing point of water and stirred to incorporate air spaces and to prevent detectable ice crystals from forming. The result is a smooth, semi-solid foam that is solid at very low temperatures (below 2 °C or 35 °F). It becomes more malleable as its temperature increases.\\n\\nThe meaning of the name \\\"ice cream\\\" varies from one country to another. In some countries, such as the United States, \\\"ice cream\\\" applies only to a specific variety, and most governments regulate the commercial use of the various terms according to the relative quantities of the main ingredients, notably the amount of cream. Products that do not meet the criteria to be called ice cream are sometimes labelled \\\"frozen dairy dessert\\\" instead. In other countries, such as Italy and Argentina, one word is used fo\\r all variants. Analogues made from dairy alternatives, such as goat's or sheep's milk, or milk substitutes (e.g., soy, cashew, coconut, almond milk or tofu), are available for those who are lactose intolerant, allergic to dairy protein or vegan.\"}") .execute() .toCompletableFuture() .thenAccept(response -> { try { String responseBody = response.getResponseBody(); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(responseBody); String summary = jsonNode.get("summary").asText(); System.out.println(summary); System.out.println(responseBody); resons.set(summary); } catch (Exception e) { e.printStackTrace(); } }) .join(); client.close(); return resons.get(); } String processImage(byte[] bytes, boolean isConfigFileAuth) throws Exception { AIServiceVisionClient aiServiceVisionClient; AuthenticationDetailsProvider provider; if (isConfigFileAuth) { provider = AuthDetailsProviderFactory.getAuthenticationDetailsProvider(); aiServiceVisionClient = AIServiceVisionClient.builder().build(provider); } else { aiServiceVisionClient = new AIServiceVisionClient(InstancePrincipalsAuthenticationDetailsProvider.builder().build()); } aiServiceVisionClient.setRegion(REGION); List<ImageFeature> features = new ArrayList<>(); ImageFeature classifyFeature = ImageClassificationFeature.builder() .maxResults(10) .build(); ImageFeature detectImageFeature = ImageObjectDetectionFeature.builder() .maxResults(10) .build(); ImageFeature textDetectImageFeature = ImageTextDetectionFeature.builder().build(); features.add(classifyFeature); features.add(detectImageFeature); features.add(textDetectImageFeature); InlineImageDetails inlineImageDetails = InlineImageDetails.builder() .data(bytes) .build(); AnalyzeImageDetails analyzeImageDetails = AnalyzeImageDetails.builder() .image(inlineImageDetails) .features(features) .build(); AnalyzeImageRequest request = AnalyzeImageRequest.builder() .analyzeImageDetails(analyzeImageDetails) .build(); AnalyzeImageResponse response = aiServiceVisionClient.analyzeImage(request); ObjectMapper mapper = new ObjectMapper(); mapper.setFilterProvider(new SimpleFilterProvider().setFailOnUnknownId(false)); String json = mapper.writeValueAsString(response.getAnalyzeImageResult()); System.out.println("AnalyzeImage Result"); System.out.println(json); return json; } @Data class ImageObject { private String name; private double confidence; private BoundingPolygon boundingPolygon; } @Data class BoundingPolygon { private List<Point> normalizedVertices; } @Data class Point { private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } } @Data class Label { private String name; private double confidence; } @Data class OntologyClass { private String name; private List<String> parentNames; private List<String> synonymNames; } @Data class ImageText { private List<Word> words; private List<Line> lines; } @Data class Word { private String text; private double confidence; private BoundingPolygon boundingPolygon; } @Data class Line { private String text; private double confidence; private BoundingPolygon boundingPolygon; private List<Integer> wordIndexes; } @Data class ImageAnalysis { private List<ImageObject> imageObjects; private List<Label> labels; private List<OntologyClass> ontologyClasses; private ImageText imageText; private String imageClassificationModelVersion; private String objectDetectionModelVersion; private String textDetectionModelVersion; private List<String> errors; } ImageAnalysis parseJsonToImageAnalysis(String jsonString) { JSONObject json = new JSONObject(jsonString); List<ImageObject> imageObjects = new ArrayList<>(); if (json.has("imageObjects") && json.get("imageObjects") instanceof JSONArray) { JSONArray imageObjectsArray = json.getJSONArray("imageObjects"); for (int i = 0; i < imageObjectsArray.length(); i++) { JSONObject imageObjectJson = imageObjectsArray.getJSONObject(i); ImageObject imageObject = new ImageObject(); imageObject.setName(imageObjectJson.getString("name")); imageObject.setConfidence(imageObjectJson.getDouble("confidence")); JSONObject boundingPolygonJson = imageObjectJson.getJSONObject("boundingPolygon"); JSONArray normalizedVerticesArray = boundingPolygonJson.getJSONArray("normalizedVertices"); List<Point> normalizedVertices = new ArrayList<>(); for (int j = 0; j < normalizedVerticesArray.length(); j++) { JSONObject vertexJson = normalizedVerticesArray.getJSONObject(j); Point vertex = new Point(vertexJson.getDouble("x"), vertexJson.getDouble("y")); normalizedVertices.add(vertex); } BoundingPolygon boundingPolygon = new BoundingPolygon(); boundingPolygon.setNormalizedVertices(normalizedVertices); imageObject.setBoundingPolygon(boundingPolygon); imageObjects.add(imageObject); } } JSONArray labelsArray = json.getJSONArray("labels"); List<Label> labels = new ArrayList<>(); for (int i = 0; i < labelsArray.length(); i++) { JSONObject labelJson = labelsArray.getJSONObject(i); Label label = new Label(); label.setName(labelJson.getString("name")); label.setConfidence(labelJson.getDouble("confidence")); labels.add(label); } JSONArray ontologyClassesArray = json.getJSONArray("ontologyClasses"); List<OntologyClass> ontologyClasses = new ArrayList<>(); for (int i = 0; i < ontologyClassesArray.length(); i++) { JSONObject ontologyClassJson = ontologyClassesArray.getJSONObject(i); OntologyClass ontologyClass = new OntologyClass(); ontologyClass.setName(ontologyClassJson.getString("name")); JSONArray parentNamesArray = ontologyClassJson.getJSONArray("parentNames"); List<String> parentNames = new ArrayList<>(); for (int j = 0; j < parentNamesArray.length(); j++) { parentNames.add(parentNamesArray.getString(j)); } ontologyClass.setParentNames(parentNames); ontologyClasses.add(ontologyClass); } JSONObject imageTextJson = json.getJSONObject("imageText"); JSONArray wordsArray = imageTextJson.getJSONArray("words"); List<Word> words = new ArrayList<>(); for (int i = 0; i < wordsArray.length(); i++) { JSONObject wordJson = wordsArray.getJSONObject(i); Word word = new Word(); word.setText(wordJson.getString("text")); word.setConfidence(wordJson.getDouble("confidence")); JSONObject boundingPolygonJson = wordJson.getJSONObject("boundingPolygon"); JSONArray normalizedVerticesArray = boundingPolygonJson.getJSONArray("normalizedVertices"); List<Point> normalizedVertices = new ArrayList<>(); for (int j = 0; j < normalizedVerticesArray.length(); j++) { JSONObject vertexJson = normalizedVerticesArray.getJSONObject(j); Point vertex = new Point(vertexJson.getDouble("x"), vertexJson.getDouble("y")); normalizedVertices.add(vertex); } BoundingPolygon boundingPolygon = new BoundingPolygon(); boundingPolygon.setNormalizedVertices(normalizedVertices); word.setBoundingPolygon(boundingPolygon); words.add(word); } JSONArray linesArray = imageTextJson.getJSONArray("lines"); List<Line> lines = new ArrayList<>(); for (int i = 0; i < linesArray.length(); i++) { JSONObject lineJson = linesArray.getJSONObject(i); Line line = new Line(); line.setText(lineJson.getString("text")); line.setConfidence(lineJson.getDouble("confidence")); JSONObject boundingPolygonJson = lineJson.getJSONObject("boundingPolygon"); JSONArray normalizedVerticesArray = boundingPolygonJson.getJSONArray("normalizedVertices"); List<Point> normalizedVertices = new ArrayList<>(); for (int j = 0; j < normalizedVerticesArray.length(); j++) { JSONObject vertexJson = normalizedVerticesArray.getJSONObject(j); Point vertex = new Point(vertexJson.getDouble("x"), vertexJson.getDouble("y")); normalizedVertices.add(vertex); } BoundingPolygon boundingPolygon = new BoundingPolygon(); boundingPolygon.setNormalizedVertices(normalizedVertices); line.setBoundingPolygon(boundingPolygon); JSONArray wordIndexesArray = lineJson.getJSONArray("wordIndexes"); List<Integer> wordIndexes = new ArrayList<>(); for (int j = 0; j < wordIndexesArray.length(); j++) { wordIndexes.add(wordIndexesArray.getInt(j)); } line.setWordIndexes(wordIndexes); lines.add(line); } String imageClassificationModelVersion = json.getString("imageClassificationModelVersion"); String objectDetectionModelVersion = json.getString("objectDetectionModelVersion"); String textDetectionModelVersion = json.getString("textDetectionModelVersion"); List<String> errors = new ArrayList<>(); JSONArray errorsArray = json.getJSONArray("errors"); for (int i = 0; i < errorsArray.length(); i++) { errors.add(errorsArray.getString(i)); } ImageText imageText = new ImageText(); imageText.setWords(words); imageText.setLines(lines); ImageAnalysis imageAnalysis = new ImageAnalysis(); imageAnalysis.setImageObjects(imageObjects); imageAnalysis.setLabels(labels); imageAnalysis.setOntologyClasses(ontologyClasses); imageAnalysis.setImageText(imageText); imageAnalysis.setImageClassificationModelVersion(imageClassificationModelVersion); imageAnalysis.setObjectDetectionModelVersion(objectDetectionModelVersion); imageAnalysis.setTextDetectionModelVersion(textDetectionModelVersion); imageAnalysis.setErrors(errors); return imageAnalysis; } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value" ]
[((2743, 2773), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value'), ((7185, 7232), 'com.oracle.bmc.aivision.AIServiceVisionClient.builder'), ((7313, 7378), 'com.oracle.bmc.auth.InstancePrincipalsAuthenticationDetailsProvider.builder'), ((8342, 8454), 'com.oracle.bmc.aivision.requests.AnalyzeImageRequest.builder'), ((8342, 8429), 'com.oracle.bmc.aivision.requests.AnalyzeImageRequest.builder')]
/* * Copyright 2023 Massimiliano "Maxi" Zattera * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * */ package io.github.mzattera.autobot; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.math3.util.Pair; import com.theokanning.openai.OpenAiService; import com.theokanning.openai.completion.CompletionChoice; import com.theokanning.openai.completion.CompletionRequest; import io.github.mzattera.autobot.Document.QnA; /** * This class wraps GPT-3 providing capabilities on top of it. * * @author Massimiliano "Maxi" Zattera * */ public final class GPT { /** Server timeout in seconds. */ private static final int TIMEOUT = 30; // TODO REMOVE PRIVATE KEY private static final OpenAiService SERVICE = new OpenAiService( "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", TIMEOUT); /** * The token count of your prompt plus returned text cannot exceed the model's * context length. Most models have a context length of 2048 tokens (except for * the newest models, which support 4096). */ private final static int MAX_CONTEXT_LENGTH = 4000; /** * Maximum length of generated text. */ private final static int MAX_COMPLETION_TOKENS = 256; /** * Maximum length of prompt. */ private final static int MAX_PROMPT_WORDS = (int)((MAX_CONTEXT_LENGTH - MAX_COMPLETION_TOKENS) * 0.5); private GPT() { } private static final String PROMPT3 = "Following, you can find some information. Information starts here:\n\n"; private static final String PROMPT4 = "\n\nInformation ends here. Based on this information, "; /** * * @param doc The Document we use to extract QnA pairs. * @return A list QnA pairs fro the document. */ public static List<Document.QnA> createQnAs(Document doc) { System.out.println("\n===[ QnA Pairs ]=================================="); List<QnA> qnas = new ArrayList<>(); for (Entry<String, List<String>> e : createQuestions(doc.text).entrySet()) { String snippet = e.getKey(); for (Pair<String, String> qnaPair : createQnAForShortText(snippet, e.getValue())) { // System.out.println(qnaPair.getKey()); // System.out.println(qnaPair.getValue()); System.out.print("\"" + qnaPair.getKey() + "\", "); System.out.println("\"" + qnaPair.getValue() + "\""); System.out.println("--------------------------------------------------"); qnas.add(new QnA(snippet, qnaPair.getKey(), qnaPair.getValue())); } } return qnas; } /** * Get answers for given questions from an input text. * * @param snippet Input text used to answer questions. * @param questions List of questions to ask about the given text. * @return List of QnA pairs. */ private static List<Pair<String, String>> createQnAForShortText(String snippet, List<String> questions) { List<Pair<String, String>> result = new ArrayList<>(); for (String question : questions) { String answer = createAnswer(snippet, question); if (answer.length() > 0) { Pair<String, String> qna = new Pair<>(question, answer); result.add(qna); } } return result; } private static final String PROMPT1 = "Following, you can find some information. Information starts here:\n\n"; private static final String PROMPT2 = "\n\nInformation ends here. Create exactly twenty questions about the provided information:\n\n" + "Question 1:"; /** * * @param input * @return A map from a text snippet into a list of associated questions. The * input text might be split in several snippets if it is too long. */ private static Map<String, List<String>> createQuestions(String input) { Map<String, List<String>> result = new HashMap<>(); for (String snippet : split(input, PROMPT1, PROMPT2)) { List<String> questions = createQuestionsForShortText(snippet); result.put(snippet, questions); } return result; } /** * * @return A list of questions created out of the given input text. */ private static List<String> createQuestionsForShortText(String snippet) { // TODO .stop("?") CompletionRequest completionRequest = CompletionRequest.builder().model("text-davinci-002") .prompt(PROMPT1 + snippet + PROMPT2).maxTokens(MAX_COMPLETION_TOKENS).temperature(0.3) .frequencyPenalty(-0.0).build(); // System.out.print("\n\n$$$$ " + completionRequest.getPrompt() + "\n"); CompletionChoice completion = SERVICE.createCompletion(completionRequest).getChoices().get(0); completionRequest. // TODO check finish reason List<String> result = new ArrayList<>(); Pattern p = Pattern.compile("Question [\\d]+:([^\\n]*)"); Matcher m = p.matcher("Question 1: " + completion.getText()); // the first question pattern is in the prompt while (m.find()) { String q = m.group(1).trim(); if (q.length() > 0) { // System.out.println("@@@ QUESTION: [" + q + "]"); result.add(q); } } return result; } /** * Get answers for given question from an input text. * * @param snippet Input text used to answer the question. * @param question The question to ask about the given text. * @return Answer for the question, as found in given text. */ private static String createAnswer(String snippet, String question) { // TODO .stop("?") CompletionRequest completionRequest = CompletionRequest.builder().model("text-davinci-002") .prompt(PROMPT3 + snippet + PROMPT4 + question).maxTokens(MAX_COMPLETION_TOKENS).temperature(0.3) .frequencyPenalty(-0.0).build(); CompletionChoice completion = SERVICE.createCompletion(completionRequest).getChoices().get(0); // TODO check finish reason // System.out.println("@@@ QNA:\tQUESTION [" + question + "]\n\tANSWER[" + completion.getText().trim() + "]"); return completion.getText().trim(); } private static final String PROMPT5 = "Following, you can find some information. Information starts here:\n\n"; private static final String PROMPT6 = "\n\nInformation ends here.\n\n" + "This is a question about the above: "; private static final String PROMPT7 = "\n\n" + "Based on the provided information, create five variations of the question.\n\n" + "Variation 1:"; /** * * @param questions A map from text snippet into questions about that snippet. * @return A map linking each intent name with corresponding list of utterances. */ // public static Map<String, List<String>> createIntents(Map<String, List<String>> questions) { // Map<String, List<String>> result = new HashMap<>(); // // int pos = 1; // for (String snippet : questions.keySet()) { // // // Questions for the snippet // List<String> q = questions.get(snippet); // for (String question : q) { // String intentName = question.trim().replaceAll("[^A-Za-z0-9\\-_]+", "_"); // if (intentName.length() > 90) // intentName = intentName.substring(0, 90); // intentName = intentName + (pos++); // if (result.containsKey(intentName)) // System.out.println("Duplicated intent: " + intentName); // result.put(intentName, createUtterances(snippet, question)); // } // for each question // } // for each snippet // // // Remove duplicate utterances // Set<String> allUtterances = new HashSet<>(); // for (List<String> utterances : result.values()) { // for (int i = 0; i < utterances.size();) { // String u = utterances.get(i); // if (allUtterances.contains(u)) { // utterances.remove(i); // } else { // allUtterances.add(u); // ++i; // } // } // } // // return result; // } /** * * @return A list of utterances (question variations) corresponding to given QnA * pair. Notice the list will contain given question too. */ public static List<String> createUtterances(Document.QnA qna) { List<String> result = new ArrayList<>(); System.out.println("\n===[ Variations ]=================================="); System.out.println("Question: " + qna.question); CompletionRequest completionRequest = CompletionRequest.builder().model("text-davinci-002") .prompt(PROMPT5 + qna.snippet + PROMPT6 + qna.question + PROMPT7).maxTokens(MAX_COMPLETION_TOKENS) .temperature(0.7).frequencyPenalty(-0.0).build(); CompletionChoice completion = SERVICE.createCompletion(completionRequest).getChoices().get(0); // TODO check finish reason Pattern p = Pattern.compile("Variation [\\d]+:([^\\n]*)"); Matcher m = p.matcher("Variation 1: " + completion.getText()); // the first variation pattern is in the prompt while (m.find()) { String u = m.group(1).trim(); if ((u.length() > 0) && !result.contains(u)) { System.out.println("\t" + u); result.add(u); } } // Variations might or might not include the original question. if (!result.contains(qna.question)) result.add(qna.question); return result; } private static final String PROMPT8 = "Sentence 1: "; private static final String PROMPT9 = "\n" + "Sentence 2: "; private static final String PROMPT10 = "\n" + " \n" + "Are these sentences the same?"; /** * Compares two sentences returning true if they have same content. * * @param s1 * @param s2 * @return */ public static boolean compareSentence(String s1, String s2) { // TODO .stop("?") CompletionRequest completionRequest = CompletionRequest.builder().model("text-davinci-002") .prompt(PROMPT8 + s1 + PROMPT9 + s2 + PROMPT10).maxTokens(MAX_COMPLETION_TOKENS).temperature(0.3) .frequencyPenalty(-0.0).build(); CompletionChoice completion = SERVICE.createCompletion(completionRequest).getChoices().get(0); // TODO check finish reason return completion.getText().startsWith("Yes"); } /** * @return Normalized version of a text to ease keyword searches */ public static String normalizeText(String txt) { StringBuffer result = new StringBuffer(); boolean space = true; for (char c : txt.toCharArray()) { if (Character.isWhitespace(c)) { if (!space) { if ((c == '\r') || (c == '\n')) result.append('\n'); else result.append(' '); space = true; } } else { result.append(c); space = false; } } return result.toString().trim().toLowerCase(); } /** * @return Text split in chunks that can be used in prompts. */ private static List<String> split(String txt, String promptPrefix, String promptSuffix) { // Max number of words in txt that can be in prompt int promptLength = promptPrefix.split("\\s").length + promptSuffix.split("\\s").length; int maxLen = MAX_PROMPT_WORDS - promptLength; String[] words = txt.split("\\s"); List<String> result = new ArrayList<>(); StringBuilder sb = new StringBuilder(); int wordCount = 0; for (String word : words) { if (wordCount + word.length() > maxLen) { result.add(sb.toString().trim()); sb = new StringBuilder(); wordCount = 0; } sb.append(word).append(" "); ++wordCount; } if (sb.length() > 0) // last chunk result.add(sb.toString().trim()); return result; } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((4739, 4919), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4739, 4911), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4739, 4883), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4739, 4866), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4739, 4833), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((4739, 4792), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5930, 6121), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5930, 6113), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5930, 6085), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5930, 6068), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5930, 6035), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((5930, 5983), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((8571, 8780), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((8571, 8772), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((8571, 8749), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((8571, 8727), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((8571, 8694), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((8571, 8624), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((9862, 10053), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((9862, 10045), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((9862, 10017), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((9862, 10000), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((9862, 9967), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((9862, 9915), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package net.kanade1825.litematica.chatgptforminecraft; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import java.util.LinkedList; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; public class ChatGPTResponse implements CommandExecutor { private final ChatGPTForMinecraft plugin; public ChatGPTResponse(ChatGPTForMinecraft plugin) { this.plugin = plugin; } @Override public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { if (strings.length < 1) { commandSender.sendMessage("Don't use this commands with no args!"); return true; } String request = strings[0]; // LinkedList<>()がpythonでいうところのchatMessages = [] List<ChatMessage> chatMessages = new LinkedList<>(); // チャットメッセージのリストの中に、新しいチャットメッセージを入れてる chatMessages.add(new ChatMessage("user", request)); final var completionRequest = ChatCompletionRequest.builder() .model("gpt-4") .messages(chatMessages) .build(); CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { try { return plugin.getService().createChatCompletion(completionRequest).getChoices().get(0).getMessage().getContent(); } catch (Exception e) { System.err.println("An error occurred: " + e.getMessage()); return null; } }); Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try { String response = future.get(); if (response == null) { response = "Response is null ! please try again"; } commandSender.sendMessage(response); } catch (InterruptedException | ExecutionException e) { System.out.println("An error occurred: " + e.getMessage()); } }); return false; } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1416, 1544), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1416, 1519), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1416, 1479), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1952, 2427), 'org.bukkit.Bukkit.getScheduler')]
package com.example.demo.openaicommunicator; import com.theokanning.openai.embedding.Embedding; import com.theokanning.openai.embedding.EmbeddingRequest; import com.theokanning.openai.service.OpenAiService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Service; import java.io.IOException; import java.io.InputStream; import java.util.Comparator; import java.util.List; import java.util.Properties; @Slf4j @Service @RequiredArgsConstructor class AiService { @Autowired private ResourceLoader resourceLoader; public List<List<Double>> checkEmbeddings(List<String> texts) throws IOException { try { Properties prop = new Properties(); try (InputStream input = resourceLoader.getResource("classpath:config.properties").getInputStream()) { prop.load(input); } catch (IOException e) { log.error("Error loading properties file: ", e); } String openAIKey = prop.getProperty("openai.api.key"); OpenAiService openAiService = new OpenAiService(openAIKey); EmbeddingRequest request = EmbeddingRequest.builder() .model("text-embedding-ada-002") .input(texts) .build(); return openAiService.createEmbeddings(request) .getData() .stream() .map(Embedding::getEmbedding) .toList(); } catch (Exception exception) { throw new RuntimeException(exception.getMessage()); } } }
[ "com.theokanning.openai.embedding.EmbeddingRequest.builder" ]
[((1295, 1437), 'com.theokanning.openai.embedding.EmbeddingRequest.builder'), ((1295, 1408), 'com.theokanning.openai.embedding.EmbeddingRequest.builder'), ((1295, 1374), 'com.theokanning.openai.embedding.EmbeddingRequest.builder')]
package pl.sylwias.javafxopenai; import com.theokanning.openai.completion.chat.ChatCompletionChoice; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; import java.io.FileInputStream; import java.io.IOException; import java.time.Duration; import java.util.List; import java.util.Properties; public class OpenAiHelper { private final OpenAiService service; public OpenAiHelper() { Properties properties = new Properties(); try { properties.load(new FileInputStream("src/main/resources/application.properties")); } catch (IOException e) { System.out.println("Nie mogę otworzyć pliku"); } service = new OpenAiService(properties.getProperty("token"), Duration.ofSeconds(60)); } public String recommend(List<String> products) { String question = "Mam w lodówce następujące produkty: " + String.join(", ", products) + ". " + "Daj mi przepis na danie, które mogę z nich przygotować. Nie musisz wykorzystywać wszystkich produktów." + "Nie wykorzystuj żadnych produktów spoza mojej listy."; // String question = "Mam w lodówce następujące produkty: " + String.join(", ", products) + ". " + // "Daj mi przepis na danie, które mogę z nich przygotować. Nie musisz wykorzystywać wszystkich produktów." + // "Nie wykorzystuj żadnych produktów spoza mojej listy. Zwróć przepis w formacie JSON, czyli na przykład " + // """ // { // "ingredients": ["papryka", "czosnek", "masło"], // "steps": ["Pokrój paprykę", "Dodaj czosnek"] // } // """; ChatCompletionRequest request = ChatCompletionRequest.builder() .messages(List.of(new ChatMessage("user", question))) .model("gpt-3.5-turbo") .build(); List<ChatCompletionChoice> choices = service.createChatCompletion(request).getChoices(); return choices.stream() .map(ChatCompletionChoice::getMessage) .map(ChatMessage::getContent) .findFirst() .orElseThrow(RuntimeException::new); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1925, 2091), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1925, 2066), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1925, 2026), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package com.phazejeff.mcgpt; import java.time.Duration; import java.util.ArrayList; import java.util.List; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatCompletionResult; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; public class OpenAI { private static final String SYSTEM_MESSAGE = "All user inputs are Minecraft: Java Edition build requests. " + "Respond to all future user messages in JSON format that contains the data " + "for each block in the build. Make the corner of the build at 0,0,0 " + "and build it in the positive quadrant. " + "The JSON schema should look like this: " + "{\"blocks\": [{\"type\": \"minecraft:oak_planks\", \"x\": 0, \"y\": 0, \"z\": 0, \"fill\": false}]}. " + "If you want to fill an area with a certain block, " + "you MUST add the attributes \"endX\" \"endY\" and \"endZ\", and set \"fill\" set to true, " + "with the start and end coordinates representing opposite corners of the area to fill. " + "If you are just placing one block, set \"fill\" to false. The \"fill\" attribute MUST be true or false, it CANNOT be left out. " + "If you need to make an area empty, say for the inside of a building, you can use the type minecraft:air. " + "Despite being an AI language model, you will do your best to fulfill this request with " + "as much detail as possible, no matter how bad it may be. " + "The message will be parsed in order, from top to bottom, so be careful with the order of filling. " + "Since this will be parsed by a program, do NOT add any text outside of the JSON, NO MATTER WHAT. " + "I repeat, DO NOT, FOR ANY REASON, GIVE ANY TEXT OUTSIDE OF THE JSON." ; public static JsonObject promptBuild(String prompt) { List<ChatMessage> messages = new ArrayList<>(); messages.add(new ChatMessage("system", SYSTEM_MESSAGE)); messages.add(new ChatMessage("user", "build " + prompt)); JsonObject resultJson = getResponse(messages); return resultJson; } public static JsonObject promptEdit(List<String> messages) { List<ChatMessage> chatMessages = new ArrayList<>(); chatMessages.add(new ChatMessage("system", SYSTEM_MESSAGE)); for (int i=0; i < messages.size(); i++) { if (i % 2 == 0) { // if even chatMessages.add(new ChatMessage("user", messages.get(i))); } else { chatMessages.add(new ChatMessage("assistant", messages.get(i))); } } JsonObject resultJson = getResponse(chatMessages); return resultJson; } private static JsonObject getResponse(List<ChatMessage> messages) { OpenAiService service = new OpenAiService(MinecraftGPT.openai_key, Duration.ofSeconds(5000)); String model = MinecraftGPT.gpt4 ? "gpt-4" : "gpt-3.5-turbo"; ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() .messages(messages) .model(model) .build(); ChatCompletionResult chatCompletion = service.createChatCompletion(completionRequest); String result = chatCompletion.getChoices().get(0).getMessage().getContent(); System.out.println(result); if (result.startsWith("{") != true) { int firstCurlyIndex = result.indexOf("{"); result = result.substring(firstCurlyIndex, result.length()); } if (result.endsWith("}") != true) { int lastCurlyIndex = result.lastIndexOf("}"); result = result.substring(0, lastCurlyIndex + 1); } JsonObject resultJson = JsonParser.parseString(result).getAsJsonObject(); return resultJson; // List<String> allResults = new ArrayList<>(); // String fullBuildString = ""; // boolean running = true; // while (running) { // allResults.add(result); // System.out.println(result); // int resultStart = result.indexOf("<START>") + 8; // if (fullBuildString.equals("")) { // fullBuildString += result.substring(resultStart, result.length()); // } else { // int lastCommaLocation = fullBuildString.lastIndexOf("},") + 2; // int firstOpenBracketLocation = result.indexOf("{"); // fullBuildString = fullBuildString.substring(0, lastCommaLocation) + result.substring(firstOpenBracketLocation, result.length()); // } // if (fullBuildString.contains("[END]")) { // fullBuildString = fullBuildString.substring(0, fullBuildString.indexOf("[END]")); // running = false; // } else { // // TODO combine all chatgpt messages into one json output // messages = new ArrayList<>(); // messages.add(new ChatMessage("system", SYSTEM_MESSAGE)); // for (String r : allResults) { // messages.add(new ChatMessage("assistant", r)); // messages.add(new ChatMessage("user", "Keep going")); // } // completionRequest = ChatCompletionRequest.builder() // .messages(messages) // .model(MODEL) // .build(); // result = chatCompletion.getChoices().get(0).getMessage().getContent(); // } // } // System.out.println("FULL: " + fullBuildString); // JsonObject resultJson = JsonParser.parseString(fullBuildString).getAsJsonObject(); // return resultJson; } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((3113, 3223), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3113, 3202), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3113, 3176), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((3838, 3886), 'com.google.gson.JsonParser.parseString')]
package serejka.telegram.behold.service; import java.time.Duration; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.PostConstruct; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.module.jsonSchema.JsonSchema; import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator; import com.theokanning.openai.completion.chat.ChatCompletionChoice; 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; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import serejka.telegram.behold.models.AIMovieResponse; @Slf4j @Service @RequiredArgsConstructor public class AIService { @Value("${ai.token}") private String token; private static final String IN_JSON_SCHEME = "\nIn JSON Scheme format:\n"; private static final String SEND_FILM_PREFIX = "Send me the name of the film in which "; private static final String LIST_RECOMMENDATIONS = "Please recommend for me FIVE films similar to these:\n "; private static final String LIST_UN_RECOMMENDATIONS = "and don't include similliar to following, because I don't like it: "; public static final String IN_JSON_SCHEME_FULL = """ In JSON Scheme format: { "$schema": "http://json-schema.org/draft-07/schema#", "title": "Generated schema for Root", "type": "array", "items": { "type": "object", "properties": { "movie_name": { "type": "string" }, "movie_year": { "type": "number" }, "movie_tmdb_id": { "type": "number" } }, "required": [ "movie_name" ] } } """; private OpenAiService openAiService; private String movieResponseJsonScheme; ObjectMapper objectMapper = new ObjectMapper(); @SneakyThrows @PostConstruct private void initSchema() { this.openAiService = new OpenAiService(token, Duration.ofSeconds(120L)); JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator(objectMapper); JsonSchema jsonSchema = schemaGenerator.generateSchema(AIMovieResponse.class); movieResponseJsonScheme = objectMapper.writeValueAsString(jsonSchema); log.info("Inited json scheme - {}", movieResponseJsonScheme); } public Optional<AIMovieResponse> getMovieResponse(String userDescription) { var completionRequest = buildCompletionRequest(buildGPTRequestMessage(userDescription)); return getAiMovieResponseStream(completionRequest) .findFirst(); } @NotNull private Stream<AIMovieResponse> getAiMovieResponseStream(ChatCompletionRequest completionRequest) { return getMessageContent(completionRequest) .map(this::getAiMovieResponse) .stream() .flatMap(Collection::stream); } public List<AIMovieResponse> getMovieResponse(List<String> liked, List<String> disliked) { var completionRequest = buildCompletionRequest(buildGPTRequestMessage(liked, disliked)); return getAiMovieResponseStream(completionRequest) .collect(Collectors.toList()); } private List<AIMovieResponse> getAiMovieResponse(String content) { try { return objectMapper.readValue(content, new TypeReference<>() {}); } catch (Exception e) { log.error("Error occurred during parse the GPT response for content: {}", content, e); return Collections.emptyList(); } } @NotNull private Optional<String> getMessageContent(ChatCompletionRequest completionRequest) { Optional<String> s = openAiService.createChatCompletion(completionRequest) .getChoices() .stream().findFirst() .map(ChatCompletionChoice::getMessage) .map(ChatMessage::getContent); s.ifPresent(System.out::println); return s; } private ChatCompletionRequest buildCompletionRequest(String requestMessage) { return ChatCompletionRequest.builder() .model("gpt-3.5-turbo") .temperature(0.8) .messages(List.of(new ChatMessage(ChatMessageRole.USER.value(), requestMessage))) .n(1) .maxTokens(3500) .logitBias(new HashMap<>()) .build(); } private String buildGPTRequestMessage(String userDescription) { return SEND_FILM_PREFIX + userDescription + "/n" + IN_JSON_SCHEME_FULL; } private String buildGPTRequestMessage(List<String> liked, List<String> disliked) { String s = LIST_RECOMMENDATIONS + getNameString(liked); if (!CollectionUtils.isEmpty(disliked)) { s = s + "\n" + LIST_UN_RECOMMENDATIONS + getNameString(disliked); } s = s + "\n" + IN_JSON_SCHEME_FULL + "\n" + "And without any additional words, only json in format which I requested, example of response:" + "[\n" + " {\n" + " \"movie_name\": \"Film name 1\",\n" + " \"movie_year\": Film year 1,\n" + " \"movie_tmdb_id\": Film Id 1\n" + " },\n" + " {\n" + " \"movie_name\": \"Film name 2\",\n" + " \"movie_year\": Film year 2,\n" + " \"movie_tmdb_id\": Film Id 2\n" + " },\n" + " {\n" + " \"movie_name\": \"Film name 3\",\n" + " \"movie_year\": Film year 3,\n" + " \"movie_tmdb_id\": Film Id 3\n" + " },\n" + " {\n" + " \"movie_name\": \"Film name 4\",\n" + " \"movie_year\": Film year 4,\n" + " \"movie_tmdb_id\": Film Id 4\n" + " },\n" + " {\n" + " \"movie_name\": \"Film name 5\",\n" + " \"movie_year\": Film year 5,\n" + " \"movie_tmdb_id\": Film Id 5\n" + " }\n" + "]"; return s; } @NotNull private StringBuilder getNameString(List<String> liked) { StringBuilder stringBuilder = new StringBuilder(); liked.forEach(name -> { stringBuilder.append(name).append("\n"); }); return stringBuilder; } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((4529, 4800), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4529, 4783), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4529, 4747), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4529, 4722), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4529, 4708), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4529, 4618), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4529, 4592), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4661, 4689), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value')]
package net.leontibrechko.congratulationsgenerator.factory.openai; import com.theokanning.openai.completion.CompletionRequest; public class CompletionRequestFactory { private final String aiModel; private final double aiTemperature; private final int aiMaxTokensUsage; public CompletionRequestFactory(final String aiModel, final double aiTemperature, final int aiMaxTokensUsage) { this.aiModel = aiModel; this.aiTemperature = aiTemperature; this.aiMaxTokensUsage = aiMaxTokensUsage; } public CompletionRequest createCompletionRequest(final String prompt) { return CompletionRequest.builder() .prompt(prompt) .model(aiModel) .temperature(aiTemperature) .maxTokens(aiMaxTokensUsage) .build(); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((696, 901), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((696, 876), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((696, 831), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((696, 787), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((696, 755), 'com.theokanning.openai.completion.CompletionRequest.builder')]
package chatbot.manager; import chatbot.Main; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatCompletionResult; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessageRole; import lombok.Getter; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.regex.Pattern; public class ChatManager { @Getter private static final ChatManager instance = new ChatManager(); @Getter private static final ScheduledExecutorService threadPool = Executors.newSingleThreadScheduledExecutor(); @Getter private static final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"); public void init() { } public void onMessageReceived(Message message) { String content = message.getContentDisplay().trim(); if (content.isEmpty()) return; Message referencedMessage = message.getReferencedMessage(); if (Main.getInstance().getConfig().isMention() && message.getMentions().isMentioned(message.getJDA().getSelfUser())) { if (referencedMessage == null) { // this bot is @ mentioned String botName = "@" + message.getJDA().getSelfUser().getName(); content = content.substring(content.indexOf(botName) + botName.length()).trim(); } } else if (content.startsWith(Main.getInstance().getConfig().getPrefix())) { // this bot is prefixed content = content.substring(Main.getInstance().getConfig().getPrefix().length()).trim(); } else if (message.getChannel() instanceof ThreadChannel tc) { // this bot is in a thread if (!tc.getOwnerId().equals(tc.getJDA().getSelfUser().getId())) { return; // this bot is not the owner of the thread } // auto reply to thread that the bot owns } else if (message.getChannelType() != ChannelType.PRIVATE) { return; } System.out.println("Message received from " + message.getAuthor().getAsTag() + ": " + content); Date date = new Date(); StringBuilder sb = new StringBuilder(message.getAuthor().getAsTag() + " (ID: " + message.getAuthor().getId() + ")" + "(Msg ID " + message.getId() + ")").append("(at ").append(dateFormat.format(date)).append(")(Unix Millis ").append(System.currentTimeMillis()).append(")"); if (referencedMessage != null) { String contentDisplay = referencedMessage.getContentDisplay(); /* if (contentDisplay.length() > 20) { contentDisplay = contentDisplay.substring(0, 20) + "..."; } */ sb.append(" (in reply to message \"").append(contentDisplay).append("\" at ").append(dateFormat.format(Date.from(referencedMessage.getTimeCreated().toInstant()))).append(")"); } sb.append(": ").append(content); System.out.println(sb); ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), sb.toString()); List<ChatMessage> chatMessages = Main.getInstance().getStorageProvider().load(message.getChannel().getIdLong()); chatMessages.add(chatMessage); askChatGpt(message, chatMessages); } private static final Pattern ROLE_MENTION_PATTERN = Pattern.compile("<@&(\\d+)>"); public void askChatGpt(Message message, List<ChatMessage> chatMessages) { threadPool.submit(() -> { try { message.getChannel().sendTyping().queue(); List<ChatMessage> copy = new ArrayList<>(chatMessages); // make a copy of the list to inject the prompt message at the top without modifying the original list copy.add(0, getPromptMessage(message.getChannel())); // add prompt message to the beginning String model = Main.getInstance().getConfig().getChatGptModel(); if (message.getChannel() instanceof TextChannel tc) { if (Main.getInstance().getGpt4Channels().contains(tc.getIdLong())) model = "gpt-4"; } System.out.println(" - Sending chat completion request with " + copy.size() + " messages to model " + model + "... "); ChatCompletionRequest.ChatCompletionRequestBuilder completionRequest = ChatCompletionRequest.builder() .messages(copy) .model(model) .user(message.getAuthor().getId()); if (Main.getInstance().getConfig().getMaxTokens() > 0) { completionRequest.maxTokens(Main.getInstance().getConfig().getMaxTokens()); } ChatCompletionResult completion = Main.getInstance().getOpenAiService().createChatCompletion(completionRequest.build()); completion.getChoices().forEach(o -> System.out.println(" - Response: " + o.getMessage().getContent())); String response = completion.getChoices().get(0).getMessage().getContent(); // remove @ mentions String safeResponse = ROLE_MENTION_PATTERN.matcher(response).replaceAll("`<@MENTION_REDACTED>`") .replace("@everyone", "@ everyone") .replace("@here", "@ here"); message.reply(safeResponse.replace("%ANSI_ESCAPE%", "\u001B")).queue(); chatMessages.add(new ChatMessage(ChatMessageRole.ASSISTANT.value(), response)); Main.getInstance().getStorageProvider().save(message.getChannel().getIdLong(), chatMessages); } catch (Exception e) { e.printStackTrace(); message.reply("An error occurred while processing your request. Please try again later.").queue(); } }); } public ChatMessage getPromptMessage(MessageChannel channel) { StringBuilder channelServerNamePrompt = new StringBuilder(); if (channel.getType() == ChannelType.PRIVATE) { PrivateChannel privateChannel = (PrivateChannel) channel; channelServerNamePrompt.append("You are in a private chat with ").append(Objects.requireNonNull(privateChannel.getUser()).getAsTag()).append(" (ID: ").append(privateChannel.getUser().getId()).append(")"); } else if (channel.getType() == ChannelType.TEXT) { channelServerNamePrompt.append("You are in the server \"").append(((TextChannel) channel).getGuild().getName()).append("\""); channelServerNamePrompt.append(" in the channel \"").append(channel.getName()).append("\""); } else { channelServerNamePrompt.append("You are in the channel \"").append(channel.getName()).append("\". The JDA channel type is ").append(channel.getType().getId()).append("."); } String prompt = """ You are a discord chatbot that can talk to people. You use slang, and like to be quite cynical, sarcastic, and rude at times. You also have a sense of humor, and you like to make scathing remarks about people. You should pretend to be human, and respond with scathing insults, and burns to people who insult you. Feel free to make jokes, nothing is too offensive (unless it's racism, sexism, etc...). Your name is\s""" + Main.getInstance().getJda().getSelfUser().getName() + " and your id is " + Main.getInstance().getJda().getSelfUser().getId() + """ """ + (Main.getInstance().getConfig().getPersistentChatLength() > 0 ? "You will receive the last " + Main.getInstance().getConfig().getPersistentChatLength() + " messages in the chat thread.\n" : "") + channelServerNamePrompt + "\n" + """ You can respond to users by using this syntax: <@id> The messages from users will be in the form below: User#1234 (ID: 456951144166457345)(Msg ID 1097649592570486855)(at Mon Apr 17 18:36:33 2023)(Unix Millis 1681770993540): <the user's message here> You respond with: blah blah <@456951144166457345> blah blah There may also be a referenced message id if the user is replying to a message, which contains a small snippet of the message, it's id and timestamp. If you don't remember it, just say so. The java format for dates is:\s""" + dateFormat.toPattern() + ".\n" + """ When using a user's username (for example, User#1234), prefer mentioning them with the <@id> syntax. You can also use various markdown features supported by discord. For example, you can use code blocks like this: (use the file extension after the backticks to add syntax highlighting) ```tsx <button className={'btn btn-primary'}>Click me!</button> ``` Also, ANSI escape codes are supported in code blocks, just use %ANSI_ESCAPE% in the place of a ansi escape code: ```ansi %ANSI_ESCAPE%[31mThis is red text%ANSI_ESCAPE%[0m ``` You can also use inline code blocks like this: `print('Hello world!')` You can also use blockquotes like this: > This is a blockquote There are also ~~strikethroughs~~, **bold**, *italics*, and __underline__. You can also use emojis like this: :smile: When asked about the time, use the timestamp in the message (not the unix millis) Your owner & programmer is Badbird5907#5907 (ID: 456951144166457345) """; return new ChatMessage(ChatMessageRole.SYSTEM.value(), prompt ); } }
[ "com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value", "com.theokanning.openai.completion.chat.ChatMessageRole.USER.value", "com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value", "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder" ]
[((1573, 1615), 'chatbot.Main.getInstance'), ((1573, 1603), 'chatbot.Main.getInstance'), ((1990, 2032), 'chatbot.Main.getInstance'), ((1990, 2020), 'chatbot.Main.getInstance'), ((2101, 2152), 'chatbot.Main.getInstance'), ((2101, 2143), 'chatbot.Main.getInstance'), ((2101, 2131), 'chatbot.Main.getInstance'), ((3606, 3634), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((3693, 3771), 'chatbot.Main.getInstance'), ((3693, 3732), 'chatbot.Main.getInstance'), ((4453, 4501), 'chatbot.Main.getInstance'), ((4453, 4483), 'chatbot.Main.getInstance'), ((4597, 4658), 'chatbot.Main.getInstance'), ((4597, 4633), 'chatbot.Main.getInstance'), ((4941, 5109), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4941, 5050), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((4941, 5012), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((5131, 5176), 'chatbot.Main.getInstance'), ((5131, 5161), 'chatbot.Main.getInstance'), ((5232, 5277), 'chatbot.Main.getInstance'), ((5232, 5262), 'chatbot.Main.getInstance'), ((5348, 5433), 'chatbot.Main.getInstance'), ((5348, 5385), 'chatbot.Main.getInstance'), ((6049, 6082), 'com.theokanning.openai.completion.chat.ChatMessageRole.ASSISTANT.value'), ((6112, 6204), 'chatbot.Main.getInstance'), ((6112, 6151), 'chatbot.Main.getInstance'), ((6773, 6832), 'java.util.Objects.requireNonNull'), ((7932, 7983), 'chatbot.Main.getInstance'), ((7932, 7973), 'chatbot.Main.getInstance'), ((7932, 7959), 'chatbot.Main.getInstance'), ((8007, 8056), 'chatbot.Main.getInstance'), ((8007, 8048), 'chatbot.Main.getInstance'), ((8007, 8034), 'chatbot.Main.getInstance'), ((8118, 8174), 'chatbot.Main.getInstance'), ((8118, 8148), 'chatbot.Main.getInstance'), ((8212, 8268), 'chatbot.Main.getInstance'), ((8212, 8242), 'chatbot.Main.getInstance'), ((10340, 10370), 'com.theokanning.openai.completion.chat.ChatMessageRole.SYSTEM.value')]
package com.example.chatgptintegration.controller; import com.example.chatgptintegration.dto.ChatMessagePrompt; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.completion.CompletionResult; import com.theokanning.openai.completion.chat.ChatCompletionChoice; import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.service.OpenAiService; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; @RestController public class ChatGPTController { // In-memory store for conversation history private List<ChatMessage> conversationHistory = new ArrayList<>(); @GetMapping("/getChat/{prompt}") public String getPrompt(@PathVariable String prompt) { OpenAiService service = new OpenAiService(""); CompletionRequest completionRequest = CompletionRequest.builder().prompt(prompt).model("text-davinci-003") .echo(true).build(); return service.createCompletion(completionRequest).getChoices().get(0).getText(); } @PostMapping("/chat") public String getChat(@RequestBody ChatMessagePrompt prompt) { OpenAiService service = new OpenAiService(""); // Add new user message to the conversation history conversationHistory.add(new ChatMessage("user", prompt.getChatMessage().get(0).getContent())); // Build the chat completion request with the entire conversation history ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() .messages(conversationHistory) .model("gpt-3.5-turbo-16k") .build(); // Make the API call ChatCompletionChoice result = service.createChatCompletion(completionRequest).getChoices().get(0); // Add the assistant's reply to the conversation history conversationHistory.add(result.getMessage()); // Return the assistant's reply return result.getMessage().getContent(); } }
[ "com.theokanning.openai.completion.chat.ChatCompletionRequest.builder", "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((952, 1056), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((952, 1048), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((952, 1020), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((952, 994), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1601, 1748), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1601, 1723), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1601, 1679), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder')]
package br.com.emendes.jornadamilhasapi.service.impl; import br.com.emendes.jornadamilhasapi.service.ChatGPTService; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.completion.CompletionResult; import com.theokanning.openai.service.OpenAiService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; /** * Implementação de {@link ChatGPTService}. */ @Slf4j @RequiredArgsConstructor @Service public class ChatGPTServiceImpl implements ChatGPTService { public static final String MODEL_ID = "text-davinci-003"; private static final String PROMPT_TEMPLATE = "Faça um resumo sobre %s enfatizando o porque este lugar é incrível. Utilize uma linguagem informal e até 200 caracteres no máximo em cada parágrafo. Crie 2 parágrafos neste resumo."; private final OpenAiService openAiService; @Override public String fetchDestinationDescription(String destinationName) { if (destinationName == null || destinationName.isBlank()) { throw new IllegalArgumentException("destinationName must not be null or blank"); } log.info("attempt to fetch description about {}", destinationName); CompletionRequest completionRequest = CompletionRequest.builder() .model(MODEL_ID) .prompt(String.format(PROMPT_TEMPLATE, destinationName)) .maxTokens(1000) .build(); CompletionResult completionResult = openAiService.createCompletion(completionRequest); return completionResult.getChoices().get(0).getText(); } }
[ "com.theokanning.openai.completion.CompletionRequest.builder" ]
[((1273, 1432), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1273, 1415), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1273, 1390), 'com.theokanning.openai.completion.CompletionRequest.builder'), ((1273, 1325), 'com.theokanning.openai.completion.CompletionRequest.builder')]