Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GitHub Modules #43

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions langchain4j-github-models-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring</artifactId>
<version>0.35.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>langchain4j-github-models-spring-boot-starter</artifactId>
<name>LangChain4j Spring Boot starter for GitHub Models</name>
<packaging>jar</packaging>

<dependencies>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-github-models</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>

<!-- needed to generate automatic metadata about available config properties -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-core</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<type>test-jar</type>
<scope>test</scope>
</dependency>

</dependencies>

<licenses>
<license>
<name>Apache-2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package dev.langchain4j.model.github.spring;

import com.azure.core.http.ProxyOptions;
import com.azure.core.util.Configuration;
import dev.langchain4j.model.github.GitHubModelsChatModel;
import dev.langchain4j.model.github.GitHubModelsEmbeddingModel;
import dev.langchain4j.model.github.GitHubModelsStreamingChatModel;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.lang.Nullable;

import java.time.Duration;

@AutoConfiguration
@EnableConfigurationProperties(Properties.class)
public class AutoConfig {

@Bean
@ConditionalOnProperty(Properties.PREFIX + ".chat-model.github-token")
GitHubModelsChatModel gitHubModelsChatModel(Properties properties) {
ChatModelProperties chatModelProperties = properties.getChatModel();
GitHubModelsChatModel.Builder builder = GitHubModelsChatModel.builder()
.endpoint(chatModelProperties.getEndpoint())
.gitHubToken(chatModelProperties.getGitHubToken())
.modelName(chatModelProperties.getModelName())
.temperature(chatModelProperties.getTemperature())
.topP(chatModelProperties.getTopP())
.maxTokens(chatModelProperties.getMaxTokens())
.presencePenalty(chatModelProperties.getPresencePenalty())
.frequencyPenalty(chatModelProperties.getFrequencyPenalty())
.timeout(Duration.ofSeconds(chatModelProperties.getTimeout() == null ? 0 : chatModelProperties.getTimeout()))
.maxRetries(chatModelProperties.getMaxRetries())
.proxyOptions(ProxyOptions.fromConfiguration(Configuration.getGlobalConfiguration()))
.logRequestsAndResponses(chatModelProperties.getLogRequestsAndResponses() != null && chatModelProperties.getLogRequestsAndResponses());

return builder.build();
}

@Bean
@ConditionalOnProperty(Properties.PREFIX + ".streaming-chat-model.github-token")
GitHubModelsStreamingChatModel gitHubModelsStreamingChatModel(Properties properties) {
ChatModelProperties chatModelProperties = properties.getStreamingChatModel();
GitHubModelsStreamingChatModel.Builder builder = GitHubModelsStreamingChatModel.builder()
.endpoint(chatModelProperties.getEndpoint())
.gitHubToken(chatModelProperties.getGitHubToken())
.modelName(chatModelProperties.getModelName())
.temperature(chatModelProperties.getTemperature())
.topP(chatModelProperties.getTopP())
.stop(chatModelProperties.getStop())
.maxTokens(chatModelProperties.getMaxTokens())
.presencePenalty(chatModelProperties.getPresencePenalty())
.frequencyPenalty(chatModelProperties.getFrequencyPenalty())
.timeout(Duration.ofSeconds(chatModelProperties.getTimeout() == null ? 0 : chatModelProperties.getTimeout()))
.proxyOptions(ProxyOptions.fromConfiguration(Configuration.getGlobalConfiguration()))
.logRequestsAndResponses(chatModelProperties.getLogRequestsAndResponses() != null && chatModelProperties.getLogRequestsAndResponses());

return builder.build();
}

@Bean
@ConditionalOnProperty({Properties.PREFIX + ".embedding-model.github-token"})
GitHubModelsEmbeddingModel openAiEmbeddingModelByApiKey(Properties properties) {
EmbeddingModelProperties embeddingModelProperties = properties.getEmbeddingModel();
GitHubModelsEmbeddingModel.Builder builder = GitHubModelsEmbeddingModel.builder()
.endpoint(embeddingModelProperties.getEndpoint())
.gitHubToken(embeddingModelProperties.getGitHubToken())
.modelName(embeddingModelProperties.getModelName())
.maxRetries(embeddingModelProperties.getMaxRetries())
.timeout(Duration.ofSeconds(embeddingModelProperties.getTimeout() == null ? 0 : embeddingModelProperties.getTimeout()))
.proxyOptions(ProxyOptions.fromConfiguration(Configuration.getGlobalConfiguration()))
.logRequestsAndResponses(embeddingModelProperties.getLogRequestsAndResponses() != null && embeddingModelProperties.getLogRequestsAndResponses());

return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package dev.langchain4j.model.github.spring;

import java.util.List;

class ChatModelProperties {

private String endpoint;
private String gitHubToken;
private String modelName;
private Double temperature;
private Double topP;
private Integer maxTokens;
private Double presencePenalty;
private Double frequencyPenalty;
private String responseFormat;
private Integer seed;
private List<String> stop;
private Integer timeout;
private Integer maxRetries;
private Boolean logRequestsAndResponses;

public String getEndpoint() {
return endpoint;
}

public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

public String getGitHubToken() {
return gitHubToken;
}

public void setGitHubToken(String gitHubToken) {
this.gitHubToken = gitHubToken;
}

public String getModelName() {
return modelName;
}

public void setModelName(String modelName) {
this.modelName = modelName;
}

public Double getTemperature() {
return temperature;
}

public void setTemperature(Double temperature) {
this.temperature = temperature;
}

public Double getTopP() {
return topP;
}

public void setTopP(Double topP) {
this.topP = topP;
}

public Integer getMaxTokens() {
return maxTokens;
}

public void setMaxTokens(Integer maxTokens) {
this.maxTokens = maxTokens;
}

public Double getPresencePenalty() {
return presencePenalty;
}

public void setPresencePenalty(Double presencePenalty) {
this.presencePenalty = presencePenalty;
}

public Double getFrequencyPenalty() {
return frequencyPenalty;
}

public void setFrequencyPenalty(Double frequencyPenalty) {
this.frequencyPenalty = frequencyPenalty;
}

public String getResponseFormat() {
return responseFormat;
}

public void setResponseFormat(String responseFormat) {
this.responseFormat = responseFormat;
}

public Integer getSeed() {
return seed;
}

public void setSeed(Integer seed) {
this.seed = seed;
}

public List<String> getStop() {
return stop;
}

public void setStop(List<String> stop) {
this.stop = stop;
}

public Integer getTimeout() {
return timeout;
}

public void setTimeout(Integer timeout) {
this.timeout = timeout;
}

public Integer getMaxRetries() {
return maxRetries;
}

public void setMaxRetries(Integer maxRetries) {
this.maxRetries = maxRetries;
}

public Boolean getLogRequestsAndResponses() {
return logRequestsAndResponses;
}

public void setLogRequestsAndResponses(Boolean logRequestsAndResponses) {
this.logRequestsAndResponses = logRequestsAndResponses;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dev.langchain4j.model.github.spring;

class EmbeddingModelProperties {

private String endpoint;
private String gitHubToken;
private String modelName;
private Integer dimensions;
private Integer timeout;
private Integer maxRetries;
private Boolean logRequestsAndResponses;

public String getEndpoint() {
return endpoint;
}

public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

public String getGitHubToken() {
return gitHubToken;
}

public void setGitHubToken(String gitHubToken) {
this.gitHubToken = gitHubToken;
}

public String getModelName() {
return modelName;
}

public void setModelName(String modelName) {
this.modelName = modelName;
}

public Integer getDimensions() {
return dimensions;
}

public void setDimensions(Integer dimensions) {
this.dimensions = dimensions;
}

public Integer getTimeout() {
return timeout;
}

public void setTimeout(Integer timeout) {
this.timeout = timeout;
}

public Integer getMaxRetries() {
return maxRetries;
}

public void setMaxRetries(Integer maxRetries) {
this.maxRetries = maxRetries;
}

public Boolean getLogRequestsAndResponses() {
return logRequestsAndResponses;
}

public void setLogRequestsAndResponses(Boolean logRequestsAndResponses) {
this.logRequestsAndResponses = logRequestsAndResponses;
}
}
Loading