|
| 1 | +package io.kestra.plugin.deepseek; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 4 | +import io.kestra.core.http.HttpRequest; |
| 5 | +import io.kestra.core.http.client.HttpClient; |
| 6 | +import io.kestra.core.http.client.configurations.HttpConfiguration; |
| 7 | +import io.kestra.core.models.annotations.Example; |
| 8 | +import io.kestra.core.models.annotations.Plugin; |
| 9 | +import io.kestra.core.models.property.Property; |
| 10 | +import io.kestra.core.models.tasks.RunnableTask; |
| 11 | +import io.kestra.core.models.tasks.Task; |
| 12 | +import io.kestra.core.runners.RunContext; |
| 13 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 14 | +import jakarta.validation.constraints.NotNull; |
| 15 | +import lombok.*; |
| 16 | +import lombok.experimental.SuperBuilder; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.net.URI; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Objects; |
| 23 | + |
| 24 | +@SuperBuilder |
| 25 | +@Getter |
| 26 | +@NoArgsConstructor |
| 27 | +@ToString |
| 28 | +@EqualsAndHashCode |
| 29 | +@Plugin( |
| 30 | + examples = { |
| 31 | + @Example( |
| 32 | + title = "Chat completion with DeepSeek", |
| 33 | + full = true, |
| 34 | + code = """ |
| 35 | + id: deepseek-chat |
| 36 | + namespace: company.name |
| 37 | +
|
| 38 | + tasks: |
| 39 | + - id: chat_completion |
| 40 | + type: io.kestra.plugin.deepseek.ChatCompletion |
| 41 | + apiKey: '{{ secret("DEEPSEEK_API_KEY") }}' |
| 42 | + modelName: deepseek-chat |
| 43 | + messages: |
| 44 | + - type: SYSTEM |
| 45 | + content: You are a helpful assistant. |
| 46 | + - type: USER |
| 47 | + content: What is the capital of Germany? Return only the name. |
| 48 | + """ |
| 49 | + ) |
| 50 | + } |
| 51 | +) |
| 52 | +public class ChatCompletion extends Task implements RunnableTask<ChatCompletion.Output> { |
| 53 | + |
| 54 | + @Schema(title = "API Key", description = "The DeepSeek API key used for authentication") |
| 55 | + @NotNull |
| 56 | + private Property<String> apiKey; |
| 57 | + |
| 58 | + @Schema(title = "Model name", description = "The name of the DeepSeek model to use, e.g. `deepseek-chat` or `deepseek-coder`") |
| 59 | + @NotNull |
| 60 | + private Property<String> modelName; |
| 61 | + |
| 62 | + @Schema(title = "Base URL", description = "The base URL of the DeepSeek API. Using the /v1 URL allows to be compatible with OpenAI.") |
| 63 | + @Builder.Default |
| 64 | + private Property<String> baseUrl = Property.ofValue("https://api.deepseek.com/v1"); |
| 65 | + |
| 66 | + @Schema(title = "Messages", description = "The list of messages in the conversation history") |
| 67 | + @NotNull |
| 68 | + private Property<List<ChatMessage>> messages; |
| 69 | + |
| 70 | + @Override |
| 71 | + public Output run(RunContext runContext) throws Exception { |
| 72 | + var resolvedApiKey = runContext.render(apiKey).as(String.class).orElseThrow(); |
| 73 | + var resolvedModelName = runContext.render(modelName).as(String.class).orElseThrow(); |
| 74 | + var resolvedBaseUrl = runContext.render(baseUrl).as(String.class).orElse("https://api.deepseek.com/v1"); |
| 75 | + var resolvedMessages = runContext.render(messages).asList(ChatMessage.class); |
| 76 | + |
| 77 | + var formattedMessages = resolvedMessages.stream() |
| 78 | + .map(msg -> Map.of( |
| 79 | + "role", msg.type().role(), |
| 80 | + "content", Objects.toString(msg.content(), "") |
| 81 | + )) |
| 82 | + .toList(); |
| 83 | + |
| 84 | + var requestBody = Map.of( |
| 85 | + "model", resolvedModelName, |
| 86 | + "messages", formattedMessages |
| 87 | + ); |
| 88 | + |
| 89 | + try (var client = new HttpClient(runContext, HttpConfiguration.builder().build())) { |
| 90 | + var request = HttpRequest.builder() |
| 91 | + .uri(URI.create(resolvedBaseUrl + "/chat/completions")) |
| 92 | + .addHeader("Authorization", "Bearer " + resolvedApiKey) |
| 93 | + .addHeader("Content-Type", "application/json") |
| 94 | + .method("POST") |
| 95 | + .body(HttpRequest.JsonRequestBody.builder().content(requestBody).build()) |
| 96 | + .build(); |
| 97 | + |
| 98 | + var response = client.request(request, ObjectNode.class); |
| 99 | + |
| 100 | + if (response.getStatus().getCode() >= 400) { |
| 101 | + throw new IOException("DeepSeek API error: " + response.getBody()); |
| 102 | + } |
| 103 | + |
| 104 | + var content = response.getBody() |
| 105 | + .get("choices") |
| 106 | + .get(0) |
| 107 | + .get("message") |
| 108 | + .get("content") |
| 109 | + .asText(); |
| 110 | + |
| 111 | + return Output.builder() |
| 112 | + .response(content) |
| 113 | + .raw(response.getBody().toString()) |
| 114 | + .build(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Builder |
| 119 | + @Getter |
| 120 | + public static class Output implements io.kestra.core.models.tasks.Output { |
| 121 | + private final String response; |
| 122 | + private final String raw; |
| 123 | + } |
| 124 | + |
| 125 | + @Builder |
| 126 | + public record ChatMessage(ChatMessageType type, String content) { |
| 127 | + } |
| 128 | + |
| 129 | + public enum ChatMessageType { |
| 130 | + SYSTEM("system"), |
| 131 | + ASSISTANT("assistant"), |
| 132 | + USER("user"); |
| 133 | + |
| 134 | + private final String role; |
| 135 | + |
| 136 | + ChatMessageType(String role) { |
| 137 | + this.role = role; |
| 138 | + } |
| 139 | + |
| 140 | + public String role() { |
| 141 | + return role; |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments