From 89fa967e2056c104ed8317a3b743d7b247d2e8cb Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 14 Nov 2024 02:23:18 +0530 Subject: [PATCH 01/41] Base Commit for Google Gemini AI Model --- langchain4j-google-ai-gemini/.gitattributes | 2 + langchain4j-google-ai-gemini/.gitignore | 33 +++ .../.mvn/wrapper/maven-wrapper.properties | 19 ++ langchain4j-google-ai-gemini/mvnw | 259 ++++++++++++++++++ langchain4j-google-ai-gemini/mvnw.cmd | 149 ++++++++++ langchain4j-google-ai-gemini/pom.xml | 62 +++++ .../Langchain4jGoogleAiGeminiApplication.java | 13 + .../src/main/resources/application.properties | 1 + ...chain4jGoogleAiGeminiApplicationTests.java | 13 + 9 files changed, 551 insertions(+) create mode 100644 langchain4j-google-ai-gemini/.gitattributes create mode 100644 langchain4j-google-ai-gemini/.gitignore create mode 100644 langchain4j-google-ai-gemini/.mvn/wrapper/maven-wrapper.properties create mode 100755 langchain4j-google-ai-gemini/mvnw create mode 100644 langchain4j-google-ai-gemini/mvnw.cmd create mode 100644 langchain4j-google-ai-gemini/pom.xml create mode 100644 langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplication.java create mode 100644 langchain4j-google-ai-gemini/src/main/resources/application.properties create mode 100644 langchain4j-google-ai-gemini/src/test/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplicationTests.java diff --git a/langchain4j-google-ai-gemini/.gitattributes b/langchain4j-google-ai-gemini/.gitattributes new file mode 100644 index 00000000..3b41682a --- /dev/null +++ b/langchain4j-google-ai-gemini/.gitattributes @@ -0,0 +1,2 @@ +/mvnw text eol=lf +*.cmd text eol=crlf diff --git a/langchain4j-google-ai-gemini/.gitignore b/langchain4j-google-ai-gemini/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/langchain4j-google-ai-gemini/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/langchain4j-google-ai-gemini/.mvn/wrapper/maven-wrapper.properties b/langchain4j-google-ai-gemini/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..d58dfb70 --- /dev/null +++ b/langchain4j-google-ai-gemini/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +wrapperVersion=3.3.2 +distributionType=only-script +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip diff --git a/langchain4j-google-ai-gemini/mvnw b/langchain4j-google-ai-gemini/mvnw new file mode 100755 index 00000000..19529ddf --- /dev/null +++ b/langchain4j-google-ai-gemini/mvnw @@ -0,0 +1,259 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Apache Maven Wrapper startup batch script, version 3.3.2 +# +# Optional ENV vars +# ----------------- +# JAVA_HOME - location of a JDK home dir, required when download maven via java source +# MVNW_REPOURL - repo url base for downloading maven distribution +# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output +# ---------------------------------------------------------------------------- + +set -euf +[ "${MVNW_VERBOSE-}" != debug ] || set -x + +# OS specific support. +native_path() { printf %s\\n "$1"; } +case "$(uname)" in +CYGWIN* | MINGW*) + [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" + native_path() { cygpath --path --windows "$1"; } + ;; +esac + +# set JAVACMD and JAVACCMD +set_java_home() { + # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched + if [ -n "${JAVA_HOME-}" ]; then + if [ -x "$JAVA_HOME/jre/sh/java" ]; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACCMD="$JAVA_HOME/jre/sh/javac" + else + JAVACMD="$JAVA_HOME/bin/java" + JAVACCMD="$JAVA_HOME/bin/javac" + + if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then + echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 + echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 + return 1 + fi + fi + else + JAVACMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v java + )" || : + JAVACCMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v javac + )" || : + + if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then + echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 + return 1 + fi + fi +} + +# hash string like Java String::hashCode +hash_string() { + str="${1:-}" h=0 + while [ -n "$str" ]; do + char="${str%"${str#?}"}" + h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) + str="${str#?}" + done + printf %x\\n $h +} + +verbose() { :; } +[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } + +die() { + printf %s\\n "$1" >&2 + exit 1 +} + +trim() { + # MWRAPPER-139: + # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. + # Needed for removing poorly interpreted newline sequences when running in more + # exotic environments such as mingw bash on Windows. + printf "%s" "${1}" | tr -d '[:space:]' +} + +# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties +while IFS="=" read -r key value; do + case "${key-}" in + distributionUrl) distributionUrl=$(trim "${value-}") ;; + distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; + esac +done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties" +[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties" + +case "${distributionUrl##*/}" in +maven-mvnd-*bin.*) + MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ + case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in + *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; + :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; + :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; + :Linux*x86_64*) distributionPlatform=linux-amd64 ;; + *) + echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 + distributionPlatform=linux-amd64 + ;; + esac + distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" + ;; +maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; +*) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; +esac + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" +distributionUrlName="${distributionUrl##*/}" +distributionUrlNameMain="${distributionUrlName%.*}" +distributionUrlNameMain="${distributionUrlNameMain%-bin}" +MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" +MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" + +exec_maven() { + unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : + exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" +} + +if [ -d "$MAVEN_HOME" ]; then + verbose "found existing MAVEN_HOME at $MAVEN_HOME" + exec_maven "$@" +fi + +case "${distributionUrl-}" in +*?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; +*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; +esac + +# prepare tmp dir +if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then + clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } + trap clean HUP INT TERM EXIT +else + die "cannot create temp dir" +fi + +mkdir -p -- "${MAVEN_HOME%/*}" + +# Download and Install Apache Maven +verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +verbose "Downloading from: $distributionUrl" +verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +# select .zip or .tar.gz +if ! command -v unzip >/dev/null; then + distributionUrl="${distributionUrl%.zip}.tar.gz" + distributionUrlName="${distributionUrl##*/}" +fi + +# verbose opt +__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' +[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v + +# normalize http auth +case "${MVNW_PASSWORD:+has-password}" in +'') MVNW_USERNAME='' MVNW_PASSWORD='' ;; +has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; +esac + +if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then + verbose "Found wget ... using wget" + wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" +elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then + verbose "Found curl ... using curl" + curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" +elif set_java_home; then + verbose "Falling back to use Java to download" + javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" + targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" + cat >"$javaSource" <<-END + public class Downloader extends java.net.Authenticator + { + protected java.net.PasswordAuthentication getPasswordAuthentication() + { + return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); + } + public static void main( String[] args ) throws Exception + { + setDefault( new Downloader() ); + java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); + } + } + END + # For Cygwin/MinGW, switch paths to Windows format before running javac and java + verbose " - Compiling Downloader.java ..." + "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" + verbose " - Running Downloader.java ..." + "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" +fi + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +if [ -n "${distributionSha256Sum-}" ]; then + distributionSha256Result=false + if [ "$MVN_CMD" = mvnd.sh ]; then + echo "Checksum validation is not supported for maven-mvnd." >&2 + echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + elif command -v sha256sum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + elif command -v shasum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + else + echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 + echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + fi + if [ $distributionSha256Result = false ]; then + echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 + echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 + exit 1 + fi +fi + +# unzip and move +if command -v unzip >/dev/null; then + unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" +else + tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" +fi +printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url" +mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" + +clean || : +exec_maven "$@" diff --git a/langchain4j-google-ai-gemini/mvnw.cmd b/langchain4j-google-ai-gemini/mvnw.cmd new file mode 100644 index 00000000..249bdf38 --- /dev/null +++ b/langchain4j-google-ai-gemini/mvnw.cmd @@ -0,0 +1,149 @@ +<# : batch portion +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper startup batch script, version 3.3.2 +@REM +@REM Optional ENV vars +@REM MVNW_REPOURL - repo url base for downloading maven distribution +@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output +@REM ---------------------------------------------------------------------------- + +@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) +@SET __MVNW_CMD__= +@SET __MVNW_ERROR__= +@SET __MVNW_PSMODULEP_SAVE=%PSModulePath% +@SET PSModulePath= +@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( + IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) +) +@SET PSModulePath=%__MVNW_PSMODULEP_SAVE% +@SET __MVNW_PSMODULEP_SAVE= +@SET __MVNW_ARG0_NAME__= +@SET MVNW_USERNAME= +@SET MVNW_PASSWORD= +@IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*) +@echo Cannot start maven from wrapper >&2 && exit /b 1 +@GOTO :EOF +: end batch / begin powershell #> + +$ErrorActionPreference = "Stop" +if ($env:MVNW_VERBOSE -eq "true") { + $VerbosePreference = "Continue" +} + +# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties +$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl +if (!$distributionUrl) { + Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" +} + +switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { + "maven-mvnd-*" { + $USE_MVND = $true + $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" + $MVN_CMD = "mvnd.cmd" + break + } + default { + $USE_MVND = $false + $MVN_CMD = $script -replace '^mvnw','mvn' + break + } +} + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +if ($env:MVNW_REPOURL) { + $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" } + $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')" +} +$distributionUrlName = $distributionUrl -replace '^.*/','' +$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' +$MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain" +if ($env:MAVEN_USER_HOME) { + $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain" +} +$MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' +$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" + +if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { + Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" + Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" + exit $? +} + +if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { + Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" +} + +# prepare tmp dir +$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile +$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" +$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null +trap { + if ($TMP_DOWNLOAD_DIR.Exists) { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } + } +} + +New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null + +# Download and Install Apache Maven +Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +Write-Verbose "Downloading from: $distributionUrl" +Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +$webclient = New-Object System.Net.WebClient +if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { + $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) +} +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 +$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum +if ($distributionSha256Sum) { + if ($USE_MVND) { + Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." + } + Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash + if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { + Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." + } +} + +# unzip and move +Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null +Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null +try { + Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null +} catch { + if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { + Write-Error "fail to move MAVEN_HOME" + } +} finally { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } +} + +Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" diff --git a/langchain4j-google-ai-gemini/pom.xml b/langchain4j-google-ai-gemini/pom.xml new file mode 100644 index 00000000..4fc8627a --- /dev/null +++ b/langchain4j-google-ai-gemini/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.3.5 + + + dev.langchain4j + langchain4j-google-ai-gemini + 0.0.1-SNAPSHOT + langchain4j-google-ai-gemini + langchain4j-google-ai-gemini + + + + + + + + + + + + + + + 21 + + + + + + dev.langchain4j + langchain4j-google-ai-gemini + 0.34.0 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplication.java b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplication.java new file mode 100644 index 00000000..58378423 --- /dev/null +++ b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplication.java @@ -0,0 +1,13 @@ +package dev.langchain4j.googleaigemini; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Langchain4jGoogleAiGeminiApplication { + + public static void main(String[] args) { + SpringApplication.run(Langchain4jGoogleAiGeminiApplication.class, args); + } + +} diff --git a/langchain4j-google-ai-gemini/src/main/resources/application.properties b/langchain4j-google-ai-gemini/src/main/resources/application.properties new file mode 100644 index 00000000..20d1a22e --- /dev/null +++ b/langchain4j-google-ai-gemini/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=langchain4j-google-ai-gemini diff --git a/langchain4j-google-ai-gemini/src/test/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplicationTests.java b/langchain4j-google-ai-gemini/src/test/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplicationTests.java new file mode 100644 index 00000000..9ec5d6aa --- /dev/null +++ b/langchain4j-google-ai-gemini/src/test/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplicationTests.java @@ -0,0 +1,13 @@ +package dev.langchain4j.googleaigemini; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class Langchain4jGoogleAiGeminiApplicationTests { + + @Test + void contextLoads() { + } + +} From cb8544ff1d01c9318d1299fd4a861a5c93c4f32f Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 14 Nov 2024 03:14:33 +0530 Subject: [PATCH 02/41] Added Configuration files for Google Gemini AI - ChatModel --- langchain4j-google-ai-gemini/pom.xml | 10 ++++-- .../googleaigemini/AutoConfig.java | 34 +++++++++++++++++++ .../googleaigemini/ChatModelProperties.java | 20 +++++++++++ .../googleaigemini/Properties.java | 20 +++++++++++ .../src/main/resources/application.properties | 13 +++++++ 5 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java create mode 100644 langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java create mode 100644 langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Properties.java diff --git a/langchain4j-google-ai-gemini/pom.xml b/langchain4j-google-ai-gemini/pom.xml index 4fc8627a..bda8fa8b 100644 --- a/langchain4j-google-ai-gemini/pom.xml +++ b/langchain4j-google-ai-gemini/pom.xml @@ -37,12 +37,18 @@ langchain4j-google-ai-gemini 0.34.0 - + + org.springframework.boot + spring-boot-starter-web + org.springframework.boot spring-boot-starter - + + org.projectlombok + lombok + org.springframework.boot spring-boot-starter-test diff --git a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java new file mode 100644 index 00000000..526d0214 --- /dev/null +++ b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java @@ -0,0 +1,34 @@ +package dev.langchain4j.googleaigemini; + + +import dev.langchain4j.model.chat.ChatLanguageModel; +import dev.langchain4j.model.chat.request.ResponseFormat; +import dev.langchain4j.model.chat.request.ResponseFormatType; +import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; + + +import static dev.langchain4j.googleaigemini.Properties.PREFIX; + +@AutoConfiguration +@EnableConfigurationProperties(Properties.class) +public class AutoConfig { + + @Bean + @ConditionalOnProperty(name = PREFIX + ".chatModel.enabled", havingValue = "true") + ChatLanguageModel googleAiGeminiChatModel(Properties properties) { + ChatModelProperties chatModelProperties = properties.getChatModel(); + return GoogleAiGeminiChatModel.builder() + .apiKey(chatModelProperties.getAPI_KEY()) + .modelName(chatModelProperties.getModelName()) + .temperature(chatModelProperties.getTemperature()) + .topP(chatModelProperties.getTopP()) + .topK(chatModelProperties.getTopK()) + .maxOutputTokens(chatModelProperties.getMaxOutputTokens()) + .responseFormat(ResponseFormat.builder().type(ResponseFormatType.JSON).build()) + .build(); + } +} \ No newline at end of file diff --git a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java new file mode 100644 index 00000000..7ddd040e --- /dev/null +++ b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java @@ -0,0 +1,20 @@ +package dev.langchain4j.googleaigemini; + + +import lombok.Getter; +import lombok.Setter; + +import java.time.Duration; + +@Getter +@Setter +public class ChatModelProperties { + + private String API_KEY; + private String modelName; + private double temperature; + private double topP; + private Integer topK; + private Integer maxOutputTokens; + +} diff --git a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Properties.java b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Properties.java new file mode 100644 index 00000000..1035184c --- /dev/null +++ b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Properties.java @@ -0,0 +1,20 @@ +package dev.langchain4j.googleaigemini; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; + +@Getter +@Setter +@ConfigurationProperties(prefix = Properties.PREFIX) +public class Properties { + + static final String PREFIX = "langchain4j.googleaigemini"; + + @NestedConfigurationProperty + ChatModelProperties chatModel; + + @NestedConfigurationProperty + ChatModelProperties streamingChatModel; +} diff --git a/langchain4j-google-ai-gemini/src/main/resources/application.properties b/langchain4j-google-ai-gemini/src/main/resources/application.properties index 20d1a22e..d4d1d5d3 100644 --- a/langchain4j-google-ai-gemini/src/main/resources/application.properties +++ b/langchain4j-google-ai-gemini/src/main/resources/application.properties @@ -1 +1,14 @@ spring.application.name=langchain4j-google-ai-gemini + +# Enable Google AI Gemini chat model +langchain4j.googleaigemini.chatModel.enabled=true + +# API Key for Google AI Gemini +langchain4j.googleaigemini.chatModel.apiKey=YOUR_API_KEY_HERE + +# Model configuration +langchain4j.googleaigemini.chatModel.modelName=YOUR_MODEL_NAME_HERE +langchain4j.googleaigemini.chatModel.temperature=0.7 +langchain4j.googleaigemini.chatModel.topP=0.9 +langchain4j.googleaigemini.chatModel.topK=40 +langchain4j.googleaigemini.chatModel.maxOutputTokens=200 From 3a8765c787cdee9334cc3bdeb552bb36fac5c08b Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 14 Nov 2024 03:15:26 +0530 Subject: [PATCH 03/41] Added Configuration files for Google Gemini AI - ChatModel --- langchain4j-google-ai-gemini/.gitattributes | 2 - langchain4j-google-ai-gemini/.gitignore | 33 --- langchain4j-google-ai-gemini/.idea/.gitignore | 8 + .../.idea/compiler.xml | 19 ++ .../.idea/encodings.xml | 6 + .../inspectionProfiles/Project_Default.xml | 23 ++ .../.idea/jarRepositories.xml | 20 ++ langchain4j-google-ai-gemini/.idea/misc.xml | 12 + langchain4j-google-ai-gemini/.idea/vcs.xml | 6 + .../.mvn/wrapper/maven-wrapper.properties | 19 -- langchain4j-google-ai-gemini/mvnw | 259 ------------------ langchain4j-google-ai-gemini/mvnw.cmd | 149 ---------- 12 files changed, 94 insertions(+), 462 deletions(-) delete mode 100644 langchain4j-google-ai-gemini/.gitattributes delete mode 100644 langchain4j-google-ai-gemini/.gitignore create mode 100644 langchain4j-google-ai-gemini/.idea/.gitignore create mode 100644 langchain4j-google-ai-gemini/.idea/compiler.xml create mode 100644 langchain4j-google-ai-gemini/.idea/encodings.xml create mode 100644 langchain4j-google-ai-gemini/.idea/inspectionProfiles/Project_Default.xml create mode 100644 langchain4j-google-ai-gemini/.idea/jarRepositories.xml create mode 100644 langchain4j-google-ai-gemini/.idea/misc.xml create mode 100644 langchain4j-google-ai-gemini/.idea/vcs.xml delete mode 100644 langchain4j-google-ai-gemini/.mvn/wrapper/maven-wrapper.properties delete mode 100755 langchain4j-google-ai-gemini/mvnw delete mode 100644 langchain4j-google-ai-gemini/mvnw.cmd diff --git a/langchain4j-google-ai-gemini/.gitattributes b/langchain4j-google-ai-gemini/.gitattributes deleted file mode 100644 index 3b41682a..00000000 --- a/langchain4j-google-ai-gemini/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -/mvnw text eol=lf -*.cmd text eol=crlf diff --git a/langchain4j-google-ai-gemini/.gitignore b/langchain4j-google-ai-gemini/.gitignore deleted file mode 100644 index 549e00a2..00000000 --- a/langchain4j-google-ai-gemini/.gitignore +++ /dev/null @@ -1,33 +0,0 @@ -HELP.md -target/ -!.mvn/wrapper/maven-wrapper.jar -!**/src/main/**/target/ -!**/src/test/**/target/ - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ -build/ -!**/src/main/**/build/ -!**/src/test/**/build/ - -### VS Code ### -.vscode/ diff --git a/langchain4j-google-ai-gemini/.idea/.gitignore b/langchain4j-google-ai-gemini/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/langchain4j-google-ai-gemini/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/langchain4j-google-ai-gemini/.idea/compiler.xml b/langchain4j-google-ai-gemini/.idea/compiler.xml new file mode 100644 index 00000000..ed7b6364 --- /dev/null +++ b/langchain4j-google-ai-gemini/.idea/compiler.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/langchain4j-google-ai-gemini/.idea/encodings.xml b/langchain4j-google-ai-gemini/.idea/encodings.xml new file mode 100644 index 00000000..63e90019 --- /dev/null +++ b/langchain4j-google-ai-gemini/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/langchain4j-google-ai-gemini/.idea/inspectionProfiles/Project_Default.xml b/langchain4j-google-ai-gemini/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..67e28fb5 --- /dev/null +++ b/langchain4j-google-ai-gemini/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,23 @@ + + + + \ No newline at end of file diff --git a/langchain4j-google-ai-gemini/.idea/jarRepositories.xml b/langchain4j-google-ai-gemini/.idea/jarRepositories.xml new file mode 100644 index 00000000..712ab9d9 --- /dev/null +++ b/langchain4j-google-ai-gemini/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/langchain4j-google-ai-gemini/.idea/misc.xml b/langchain4j-google-ai-gemini/.idea/misc.xml new file mode 100644 index 00000000..6546f14f --- /dev/null +++ b/langchain4j-google-ai-gemini/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/langchain4j-google-ai-gemini/.idea/vcs.xml b/langchain4j-google-ai-gemini/.idea/vcs.xml new file mode 100644 index 00000000..6c0b8635 --- /dev/null +++ b/langchain4j-google-ai-gemini/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/langchain4j-google-ai-gemini/.mvn/wrapper/maven-wrapper.properties b/langchain4j-google-ai-gemini/.mvn/wrapper/maven-wrapper.properties deleted file mode 100644 index d58dfb70..00000000 --- a/langchain4j-google-ai-gemini/.mvn/wrapper/maven-wrapper.properties +++ /dev/null @@ -1,19 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you 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. -wrapperVersion=3.3.2 -distributionType=only-script -distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip diff --git a/langchain4j-google-ai-gemini/mvnw b/langchain4j-google-ai-gemini/mvnw deleted file mode 100755 index 19529ddf..00000000 --- a/langchain4j-google-ai-gemini/mvnw +++ /dev/null @@ -1,259 +0,0 @@ -#!/bin/sh -# ---------------------------------------------------------------------------- -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you 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. -# ---------------------------------------------------------------------------- - -# ---------------------------------------------------------------------------- -# Apache Maven Wrapper startup batch script, version 3.3.2 -# -# Optional ENV vars -# ----------------- -# JAVA_HOME - location of a JDK home dir, required when download maven via java source -# MVNW_REPOURL - repo url base for downloading maven distribution -# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven -# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output -# ---------------------------------------------------------------------------- - -set -euf -[ "${MVNW_VERBOSE-}" != debug ] || set -x - -# OS specific support. -native_path() { printf %s\\n "$1"; } -case "$(uname)" in -CYGWIN* | MINGW*) - [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" - native_path() { cygpath --path --windows "$1"; } - ;; -esac - -# set JAVACMD and JAVACCMD -set_java_home() { - # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched - if [ -n "${JAVA_HOME-}" ]; then - if [ -x "$JAVA_HOME/jre/sh/java" ]; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" - JAVACCMD="$JAVA_HOME/jre/sh/javac" - else - JAVACMD="$JAVA_HOME/bin/java" - JAVACCMD="$JAVA_HOME/bin/javac" - - if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then - echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 - echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 - return 1 - fi - fi - else - JAVACMD="$( - 'set' +e - 'unset' -f command 2>/dev/null - 'command' -v java - )" || : - JAVACCMD="$( - 'set' +e - 'unset' -f command 2>/dev/null - 'command' -v javac - )" || : - - if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then - echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 - return 1 - fi - fi -} - -# hash string like Java String::hashCode -hash_string() { - str="${1:-}" h=0 - while [ -n "$str" ]; do - char="${str%"${str#?}"}" - h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) - str="${str#?}" - done - printf %x\\n $h -} - -verbose() { :; } -[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } - -die() { - printf %s\\n "$1" >&2 - exit 1 -} - -trim() { - # MWRAPPER-139: - # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. - # Needed for removing poorly interpreted newline sequences when running in more - # exotic environments such as mingw bash on Windows. - printf "%s" "${1}" | tr -d '[:space:]' -} - -# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties -while IFS="=" read -r key value; do - case "${key-}" in - distributionUrl) distributionUrl=$(trim "${value-}") ;; - distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; - esac -done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties" -[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties" - -case "${distributionUrl##*/}" in -maven-mvnd-*bin.*) - MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ - case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in - *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; - :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; - :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; - :Linux*x86_64*) distributionPlatform=linux-amd64 ;; - *) - echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 - distributionPlatform=linux-amd64 - ;; - esac - distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" - ;; -maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; -*) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; -esac - -# apply MVNW_REPOURL and calculate MAVEN_HOME -# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ -[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" -distributionUrlName="${distributionUrl##*/}" -distributionUrlNameMain="${distributionUrlName%.*}" -distributionUrlNameMain="${distributionUrlNameMain%-bin}" -MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" -MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" - -exec_maven() { - unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : - exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" -} - -if [ -d "$MAVEN_HOME" ]; then - verbose "found existing MAVEN_HOME at $MAVEN_HOME" - exec_maven "$@" -fi - -case "${distributionUrl-}" in -*?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; -*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; -esac - -# prepare tmp dir -if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then - clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } - trap clean HUP INT TERM EXIT -else - die "cannot create temp dir" -fi - -mkdir -p -- "${MAVEN_HOME%/*}" - -# Download and Install Apache Maven -verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." -verbose "Downloading from: $distributionUrl" -verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" - -# select .zip or .tar.gz -if ! command -v unzip >/dev/null; then - distributionUrl="${distributionUrl%.zip}.tar.gz" - distributionUrlName="${distributionUrl##*/}" -fi - -# verbose opt -__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' -[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v - -# normalize http auth -case "${MVNW_PASSWORD:+has-password}" in -'') MVNW_USERNAME='' MVNW_PASSWORD='' ;; -has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; -esac - -if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then - verbose "Found wget ... using wget" - wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" -elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then - verbose "Found curl ... using curl" - curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" -elif set_java_home; then - verbose "Falling back to use Java to download" - javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" - targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" - cat >"$javaSource" <<-END - public class Downloader extends java.net.Authenticator - { - protected java.net.PasswordAuthentication getPasswordAuthentication() - { - return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); - } - public static void main( String[] args ) throws Exception - { - setDefault( new Downloader() ); - java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); - } - } - END - # For Cygwin/MinGW, switch paths to Windows format before running javac and java - verbose " - Compiling Downloader.java ..." - "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" - verbose " - Running Downloader.java ..." - "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" -fi - -# If specified, validate the SHA-256 sum of the Maven distribution zip file -if [ -n "${distributionSha256Sum-}" ]; then - distributionSha256Result=false - if [ "$MVN_CMD" = mvnd.sh ]; then - echo "Checksum validation is not supported for maven-mvnd." >&2 - echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 - exit 1 - elif command -v sha256sum >/dev/null; then - if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then - distributionSha256Result=true - fi - elif command -v shasum >/dev/null; then - if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then - distributionSha256Result=true - fi - else - echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 - echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 - exit 1 - fi - if [ $distributionSha256Result = false ]; then - echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 - echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 - exit 1 - fi -fi - -# unzip and move -if command -v unzip >/dev/null; then - unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" -else - tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" -fi -printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url" -mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" - -clean || : -exec_maven "$@" diff --git a/langchain4j-google-ai-gemini/mvnw.cmd b/langchain4j-google-ai-gemini/mvnw.cmd deleted file mode 100644 index 249bdf38..00000000 --- a/langchain4j-google-ai-gemini/mvnw.cmd +++ /dev/null @@ -1,149 +0,0 @@ -<# : batch portion -@REM ---------------------------------------------------------------------------- -@REM Licensed to the Apache Software Foundation (ASF) under one -@REM or more contributor license agreements. See the NOTICE file -@REM distributed with this work for additional information -@REM regarding copyright ownership. The ASF licenses this file -@REM to you under the Apache License, Version 2.0 (the -@REM "License"); you may not use this file except in compliance -@REM with the License. You may obtain a copy of the License at -@REM -@REM http://www.apache.org/licenses/LICENSE-2.0 -@REM -@REM Unless required by applicable law or agreed to in writing, -@REM software distributed under the License is distributed on an -@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -@REM KIND, either express or implied. See the License for the -@REM specific language governing permissions and limitations -@REM under the License. -@REM ---------------------------------------------------------------------------- - -@REM ---------------------------------------------------------------------------- -@REM Apache Maven Wrapper startup batch script, version 3.3.2 -@REM -@REM Optional ENV vars -@REM MVNW_REPOURL - repo url base for downloading maven distribution -@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven -@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output -@REM ---------------------------------------------------------------------------- - -@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) -@SET __MVNW_CMD__= -@SET __MVNW_ERROR__= -@SET __MVNW_PSMODULEP_SAVE=%PSModulePath% -@SET PSModulePath= -@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( - IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) -) -@SET PSModulePath=%__MVNW_PSMODULEP_SAVE% -@SET __MVNW_PSMODULEP_SAVE= -@SET __MVNW_ARG0_NAME__= -@SET MVNW_USERNAME= -@SET MVNW_PASSWORD= -@IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*) -@echo Cannot start maven from wrapper >&2 && exit /b 1 -@GOTO :EOF -: end batch / begin powershell #> - -$ErrorActionPreference = "Stop" -if ($env:MVNW_VERBOSE -eq "true") { - $VerbosePreference = "Continue" -} - -# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties -$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl -if (!$distributionUrl) { - Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" -} - -switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { - "maven-mvnd-*" { - $USE_MVND = $true - $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" - $MVN_CMD = "mvnd.cmd" - break - } - default { - $USE_MVND = $false - $MVN_CMD = $script -replace '^mvnw','mvn' - break - } -} - -# apply MVNW_REPOURL and calculate MAVEN_HOME -# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ -if ($env:MVNW_REPOURL) { - $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" } - $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')" -} -$distributionUrlName = $distributionUrl -replace '^.*/','' -$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' -$MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain" -if ($env:MAVEN_USER_HOME) { - $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain" -} -$MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' -$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" - -if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { - Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" - Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" - exit $? -} - -if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { - Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" -} - -# prepare tmp dir -$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile -$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" -$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null -trap { - if ($TMP_DOWNLOAD_DIR.Exists) { - try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } - catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } - } -} - -New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null - -# Download and Install Apache Maven -Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." -Write-Verbose "Downloading from: $distributionUrl" -Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" - -$webclient = New-Object System.Net.WebClient -if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { - $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) -} -[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null - -# If specified, validate the SHA-256 sum of the Maven distribution zip file -$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum -if ($distributionSha256Sum) { - if ($USE_MVND) { - Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." - } - Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash - if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { - Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." - } -} - -# unzip and move -Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null -Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null -try { - Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null -} catch { - if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { - Write-Error "fail to move MAVEN_HOME" - } -} finally { - try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } - catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } -} - -Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" From 0dfbc0de93c21ebd288306554dd3746c1c0e8f9b Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 14 Nov 2024 03:17:10 +0530 Subject: [PATCH 04/41] Check --- langchain4j-google-ai-gemini/.idea/vcs.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/langchain4j-google-ai-gemini/.idea/vcs.xml b/langchain4j-google-ai-gemini/.idea/vcs.xml index 6c0b8635..288b36b1 100644 --- a/langchain4j-google-ai-gemini/.idea/vcs.xml +++ b/langchain4j-google-ai-gemini/.idea/vcs.xml @@ -2,5 +2,6 @@ + \ No newline at end of file From 8335e679cc157601f067341697b6ccbcadae4a2f Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 14 Nov 2024 03:35:25 +0530 Subject: [PATCH 05/41] Adding Streaming Google AI Chat Model and Removing Lombok uses --- .../inspectionProfiles/Project_Default.xml | 3 + langchain4j-google-ai-gemini/pom.xml | 14 ++--- .../googleaigemini/AutoConfig.java | 23 ++++++-- .../googleaigemini/ChatModelProperties.java | 56 ++++++++++++++++--- .../Langchain4jGoogleAiGeminiApplication.java | 13 ----- .../googleaigemini/Properties.java | 20 +++++-- .../src/main/resources/application.properties | 1 + 7 files changed, 92 insertions(+), 38 deletions(-) delete mode 100644 langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplication.java diff --git a/langchain4j-google-ai-gemini/.idea/inspectionProfiles/Project_Default.xml b/langchain4j-google-ai-gemini/.idea/inspectionProfiles/Project_Default.xml index 67e28fb5..a84f2414 100644 --- a/langchain4j-google-ai-gemini/.idea/inspectionProfiles/Project_Default.xml +++ b/langchain4j-google-ai-gemini/.idea/inspectionProfiles/Project_Default.xml @@ -6,16 +6,19 @@ diff --git a/langchain4j-google-ai-gemini/pom.xml b/langchain4j-google-ai-gemini/pom.xml index bda8fa8b..0beea692 100644 --- a/langchain4j-google-ai-gemini/pom.xml +++ b/langchain4j-google-ai-gemini/pom.xml @@ -31,24 +31,18 @@ - + dev.langchain4j langchain4j-google-ai-gemini - 0.34.0 - - - org.springframework.boot - spring-boot-starter-web + 0.36.0 + org.springframework.boot spring-boot-starter - - org.projectlombok - lombok - + org.springframework.boot spring-boot-starter-test diff --git a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java index 526d0214..4bcb88af 100644 --- a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java +++ b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java @@ -1,16 +1,16 @@ package dev.langchain4j.googleaigemini; - import dev.langchain4j.model.chat.ChatLanguageModel; +import dev.langchain4j.model.chat.StreamingChatLanguageModel; import dev.langchain4j.model.chat.request.ResponseFormat; -import dev.langchain4j.model.chat.request.ResponseFormatType; import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel; +import dev.langchain4j.model.googleai.GoogleAiGeminiStreamingChatModel; + import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; - import static dev.langchain4j.googleaigemini.Properties.PREFIX; @AutoConfiguration @@ -28,7 +28,22 @@ ChatLanguageModel googleAiGeminiChatModel(Properties properties) { .topP(chatModelProperties.getTopP()) .topK(chatModelProperties.getTopK()) .maxOutputTokens(chatModelProperties.getMaxOutputTokens()) - .responseFormat(ResponseFormat.builder().type(ResponseFormatType.JSON).build()) + .responseFormat(ResponseFormat.JSON) + .build(); + } + + @Bean + @ConditionalOnProperty(name = PREFIX + ".streamingChatModel.enabled", havingValue = "true") + StreamingChatLanguageModel googleAiGeminiStreamingChatModel(Properties properties) { + ChatModelProperties chatModelProperties = properties.getChatModel(); + return GoogleAiGeminiStreamingChatModel.builder() + .apiKey(chatModelProperties.getAPI_KEY()) + .modelName(chatModelProperties.getModelName()) + .temperature(chatModelProperties.getTemperature()) + .topP(chatModelProperties.getTopP()) + .topK(chatModelProperties.getTopK()) + .maxOutputTokens(chatModelProperties.getMaxOutputTokens()) + .responseFormat(ResponseFormat.JSON) .build(); } } \ No newline at end of file diff --git a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java index 7ddd040e..27aabfbe 100644 --- a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java @@ -1,13 +1,6 @@ package dev.langchain4j.googleaigemini; -import lombok.Getter; -import lombok.Setter; - -import java.time.Duration; - -@Getter -@Setter public class ChatModelProperties { private String API_KEY; @@ -15,6 +8,55 @@ public class ChatModelProperties { private double temperature; private double topP; private Integer topK; + + public String getAPI_KEY() { + return API_KEY; + } + + public void setAPI_KEY(String API_KEY) { + this.API_KEY = API_KEY; + } + + 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 getTopK() { + return topK; + } + + public void setTopK(Integer topK) { + this.topK = topK; + } + + public Integer getMaxOutputTokens() { + return maxOutputTokens; + } + + public void setMaxOutputTokens(Integer maxOutputTokens) { + this.maxOutputTokens = maxOutputTokens; + } + private Integer maxOutputTokens; } diff --git a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplication.java b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplication.java deleted file mode 100644 index 58378423..00000000 --- a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplication.java +++ /dev/null @@ -1,13 +0,0 @@ -package dev.langchain4j.googleaigemini; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class Langchain4jGoogleAiGeminiApplication { - - public static void main(String[] args) { - SpringApplication.run(Langchain4jGoogleAiGeminiApplication.class, args); - } - -} diff --git a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Properties.java b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Properties.java index 1035184c..c78ebf32 100644 --- a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Properties.java +++ b/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Properties.java @@ -1,12 +1,8 @@ package dev.langchain4j.googleaigemini; -import lombok.Getter; -import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; -@Getter -@Setter @ConfigurationProperties(prefix = Properties.PREFIX) public class Properties { @@ -15,6 +11,22 @@ public class Properties { @NestedConfigurationProperty ChatModelProperties chatModel; + public ChatModelProperties getStreamingChatModel() { + return streamingChatModel; + } + + public void setStreamingChatModel(ChatModelProperties streamingChatModel) { + this.streamingChatModel = streamingChatModel; + } + + public ChatModelProperties getChatModel() { + return chatModel; + } + + public void setChatModel(ChatModelProperties chatModel) { + this.chatModel = chatModel; + } + @NestedConfigurationProperty ChatModelProperties streamingChatModel; } diff --git a/langchain4j-google-ai-gemini/src/main/resources/application.properties b/langchain4j-google-ai-gemini/src/main/resources/application.properties index d4d1d5d3..7451f208 100644 --- a/langchain4j-google-ai-gemini/src/main/resources/application.properties +++ b/langchain4j-google-ai-gemini/src/main/resources/application.properties @@ -2,6 +2,7 @@ spring.application.name=langchain4j-google-ai-gemini # Enable Google AI Gemini chat model langchain4j.googleaigemini.chatModel.enabled=true +langchain4j.googleaigemini.streamingChatModel.enabled=true # API Key for Google AI Gemini langchain4j.googleaigemini.chatModel.apiKey=YOUR_API_KEY_HERE From 84bc5e3fc2ec6767a4516c717ba8647539b022b4 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 14 Nov 2024 03:37:01 +0530 Subject: [PATCH 06/41] Adding application.properties.local for easy usage --- .../{application.properties => application.properties.local} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename langchain4j-google-ai-gemini/src/main/resources/{application.properties => application.properties.local} (100%) diff --git a/langchain4j-google-ai-gemini/src/main/resources/application.properties b/langchain4j-google-ai-gemini/src/main/resources/application.properties.local similarity index 100% rename from langchain4j-google-ai-gemini/src/main/resources/application.properties rename to langchain4j-google-ai-gemini/src/main/resources/application.properties.local From 6d7c4cf84ffcc0daa587c653191c962d5f8886b3 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 14 Nov 2024 03:41:33 +0530 Subject: [PATCH 07/41] Ignore all the .idea folders in subfolders --- .gitignore | 1 + .gitignore.save | 39 +++++++++++++++++++++++++++++++++++++++ .gitignore.swp | Bin 0 -> 12288 bytes 3 files changed, 40 insertions(+) create mode 100644 .gitignore.save create mode 100644 .gitignore.swp diff --git a/.gitignore b/.gitignore index b891694b..5b5f7fe9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ target/ ### IntelliJ IDEA ### .idea/* +**/.idea/ .idea/modules.xml .idea/jarRepositories.xml .idea/compiler.xml diff --git a/.gitignore.save b/.gitignore.save new file mode 100644 index 00000000..b75a86c7 --- /dev/null +++ b/.gitignore.save @@ -0,0 +1,39 @@ +target/ + +### IntelliJ IDEA ### +**/.idea/ +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store + +### .env files contain local environment variables ### +.env diff --git a/.gitignore.swp b/.gitignore.swp new file mode 100644 index 0000000000000000000000000000000000000000..7ac0105d7291471f360774f3f94ca1f0b4a6c29f GIT binary patch literal 12288 zcmeI2J#P~+7{^@}SPB&{3)6*61l_qWL84=!9a_;M8nqGvsq*>Ui(AKcvYk+tGQr9x zU}0ltgAc%j#6;PE4*`t)bGb-qB?c;%O3z8Z^V!d1`?+6~A&S%4?bY>YF_~kyW*B=r zb36Z%rflOIW4_-NKHe|7Dig!-->%8L*odG*KU48Sdw%x#jKj%9z9t5O8)0+V723A< zVqZFKbDrcn=-d`g9iL4-GC&4SZJ-}^<2%z^3-fc;x3YPo5nZ{wb?W74A{ih9WPl8i z0Wv@a$N(82182`bD5uzSlzpx){bt=xooMSWHDrJckO4A42FL&zAOmE843GgbKnBRb z88jd=#vV;GcI_gH$N&H1-~YcaG4>052VcPv_yj(HH{caG1P8zY1NOmVa33szHn;^E z;3~KP&V!%W>jyXjpTS4)9=rpu!AtN0^nn5U;0f3S55aX%)q4hgQ^{n243GgbKnBPF z86X2>fDDj<|7{>qcEFm=W;A?=cC|_V6NbmPhug0U89nIKk%5;*rg#Nj`GB&aqdKBuq3x>rZJiEy0Pgv&8DR*_LK}92h0Z|)CFv)a90UCuG$CRj-`-Y z#gfrU^eT#u3IXTRJxl6kSh6c5Uhr8dLYF1d2=7Oaq6|B^vdUqTta{58X>{r9w`?W4 zbPV^N8Qz~_Nu4Pig4d~oL*;orAd6n9jdIBWGDkUk!fmRu@ERKkl<5==1;p literal 0 HcmV?d00001 From 300cfa28809404abaa7d7fc2c1cb881509b6c164 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 14 Nov 2024 03:42:36 +0530 Subject: [PATCH 08/41] Ignore all the .idea folders in subfolders --- .gitignore.save | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 .gitignore.save diff --git a/.gitignore.save b/.gitignore.save deleted file mode 100644 index b75a86c7..00000000 --- a/.gitignore.save +++ /dev/null @@ -1,39 +0,0 @@ -target/ - -### IntelliJ IDEA ### -**/.idea/ -.idea/modules.xml -.idea/jarRepositories.xml -.idea/compiler.xml -.idea/libraries/ -*.iws -*.iml -*.ipr - -### Eclipse ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ -build/ -!**/src/main/**/build/ -!**/src/test/**/build/ - -### VS Code ### -.vscode/ - -### Mac OS ### -.DS_Store - -### .env files contain local environment variables ### -.env From 961841099fc856559469517f693b4c5ccf8a6ad1 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 14 Nov 2024 03:43:03 +0530 Subject: [PATCH 09/41] Ignore all the .idea folders in subfolders --- .gitignore.swp | Bin 12288 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .gitignore.swp diff --git a/.gitignore.swp b/.gitignore.swp deleted file mode 100644 index 7ac0105d7291471f360774f3f94ca1f0b4a6c29f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2J#P~+7{^@}SPB&{3)6*61l_qWL84=!9a_;M8nqGvsq*>Ui(AKcvYk+tGQr9x zU}0ltgAc%j#6;PE4*`t)bGb-qB?c;%O3z8Z^V!d1`?+6~A&S%4?bY>YF_~kyW*B=r zb36Z%rflOIW4_-NKHe|7Dig!-->%8L*odG*KU48Sdw%x#jKj%9z9t5O8)0+V723A< zVqZFKbDrcn=-d`g9iL4-GC&4SZJ-}^<2%z^3-fc;x3YPo5nZ{wb?W74A{ih9WPl8i z0Wv@a$N(82182`bD5uzSlzpx){bt=xooMSWHDrJckO4A42FL&zAOmE843GgbKnBRb z88jd=#vV;GcI_gH$N&H1-~YcaG4>052VcPv_yj(HH{caG1P8zY1NOmVa33szHn;^E z;3~KP&V!%W>jyXjpTS4)9=rpu!AtN0^nn5U;0f3S55aX%)q4hgQ^{n243GgbKnBPF z86X2>fDDj<|7{>qcEFm=W;A?=cC|_V6NbmPhug0U89nIKk%5;*rg#Nj`GB&aqdKBuq3x>rZJiEy0Pgv&8DR*_LK}92h0Z|)CFv)a90UCuG$CRj-`-Y z#gfrU^eT#u3IXTRJxl6kSh6c5Uhr8dLYF1d2=7Oaq6|B^vdUqTta{58X>{r9w`?W4 zbPV^N8Qz~_Nu4Pig4d~oL*;orAd6n9jdIBWGDkUk!fmRu@ERKkl<5==1;p From 7ee36c66afc914d52151369fbcb707fac19ffb0f Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 14 Nov 2024 17:15:01 +0530 Subject: [PATCH 10/41] Changing pom.xml headers --- .../pom.xml | 33 +++++++------------ .../googleaigemini/AutoConfig.java | 0 .../googleaigemini/ChatModelProperties.java | 0 .../googleaigemini/Properties.java | 0 .../resources/application.properties.local | 0 ...chain4jGoogleAiGeminiApplicationTests.java | 0 langchain4j-google-ai-gemini/.idea/.gitignore | 8 ----- .../.idea/compiler.xml | 19 ----------- .../.idea/encodings.xml | 6 ---- .../inspectionProfiles/Project_Default.xml | 26 --------------- .../.idea/jarRepositories.xml | 20 ----------- langchain4j-google-ai-gemini/.idea/misc.xml | 12 ------- langchain4j-google-ai-gemini/.idea/vcs.xml | 7 ---- 13 files changed, 11 insertions(+), 120 deletions(-) rename {langchain4j-google-ai-gemini => langchain4j-google-ai-gemini-spring-boot-starter}/pom.xml (65%) rename {langchain4j-google-ai-gemini => langchain4j-google-ai-gemini-spring-boot-starter}/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java (100%) rename {langchain4j-google-ai-gemini => langchain4j-google-ai-gemini-spring-boot-starter}/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java (100%) rename {langchain4j-google-ai-gemini => langchain4j-google-ai-gemini-spring-boot-starter}/src/main/java/dev/langchain4j/googleaigemini/Properties.java (100%) rename {langchain4j-google-ai-gemini => langchain4j-google-ai-gemini-spring-boot-starter}/src/main/resources/application.properties.local (100%) rename {langchain4j-google-ai-gemini => langchain4j-google-ai-gemini-spring-boot-starter}/src/test/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplicationTests.java (100%) delete mode 100644 langchain4j-google-ai-gemini/.idea/.gitignore delete mode 100644 langchain4j-google-ai-gemini/.idea/compiler.xml delete mode 100644 langchain4j-google-ai-gemini/.idea/encodings.xml delete mode 100644 langchain4j-google-ai-gemini/.idea/inspectionProfiles/Project_Default.xml delete mode 100644 langchain4j-google-ai-gemini/.idea/jarRepositories.xml delete mode 100644 langchain4j-google-ai-gemini/.idea/misc.xml delete mode 100644 langchain4j-google-ai-gemini/.idea/vcs.xml diff --git a/langchain4j-google-ai-gemini/pom.xml b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml similarity index 65% rename from langchain4j-google-ai-gemini/pom.xml rename to langchain4j-google-ai-gemini-spring-boot-starter/pom.xml index 0beea692..0949d861 100644 --- a/langchain4j-google-ai-gemini/pom.xml +++ b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml @@ -2,30 +2,19 @@ 4.0.0 + - org.springframework.boot - spring-boot-starter-parent - 3.3.5 - + dev.langchain4j + langchain4j-spring + 0.37.0-SNAPSHOT + ../pom.xml - dev.langchain4j - langchain4j-google-ai-gemini - 0.0.1-SNAPSHOT - langchain4j-google-ai-gemini - langchain4j-google-ai-gemini - - - - - - - - - - - - - + + langchain4j-google-ai-gemini-spring-boot-starter + + Langchain4j Spring Boot Starter for Google Gemini AI Model + + 21 diff --git a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java similarity index 100% rename from langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java rename to langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java diff --git a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java similarity index 100% rename from langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java rename to langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java diff --git a/langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Properties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java similarity index 100% rename from langchain4j-google-ai-gemini/src/main/java/dev/langchain4j/googleaigemini/Properties.java rename to langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java diff --git a/langchain4j-google-ai-gemini/src/main/resources/application.properties.local b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties.local similarity index 100% rename from langchain4j-google-ai-gemini/src/main/resources/application.properties.local rename to langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties.local diff --git a/langchain4j-google-ai-gemini/src/test/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplicationTests.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplicationTests.java similarity index 100% rename from langchain4j-google-ai-gemini/src/test/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplicationTests.java rename to langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplicationTests.java diff --git a/langchain4j-google-ai-gemini/.idea/.gitignore b/langchain4j-google-ai-gemini/.idea/.gitignore deleted file mode 100644 index 13566b81..00000000 --- a/langchain4j-google-ai-gemini/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/langchain4j-google-ai-gemini/.idea/compiler.xml b/langchain4j-google-ai-gemini/.idea/compiler.xml deleted file mode 100644 index ed7b6364..00000000 --- a/langchain4j-google-ai-gemini/.idea/compiler.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/langchain4j-google-ai-gemini/.idea/encodings.xml b/langchain4j-google-ai-gemini/.idea/encodings.xml deleted file mode 100644 index 63e90019..00000000 --- a/langchain4j-google-ai-gemini/.idea/encodings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/langchain4j-google-ai-gemini/.idea/inspectionProfiles/Project_Default.xml b/langchain4j-google-ai-gemini/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index a84f2414..00000000 --- a/langchain4j-google-ai-gemini/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - \ No newline at end of file diff --git a/langchain4j-google-ai-gemini/.idea/jarRepositories.xml b/langchain4j-google-ai-gemini/.idea/jarRepositories.xml deleted file mode 100644 index 712ab9d9..00000000 --- a/langchain4j-google-ai-gemini/.idea/jarRepositories.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/langchain4j-google-ai-gemini/.idea/misc.xml b/langchain4j-google-ai-gemini/.idea/misc.xml deleted file mode 100644 index 6546f14f..00000000 --- a/langchain4j-google-ai-gemini/.idea/misc.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/langchain4j-google-ai-gemini/.idea/vcs.xml b/langchain4j-google-ai-gemini/.idea/vcs.xml deleted file mode 100644 index 288b36b1..00000000 --- a/langchain4j-google-ai-gemini/.idea/vcs.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file From f696e829792d18cfbad192d0e1c9a80d7807bec6 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 14 Nov 2024 17:19:53 +0530 Subject: [PATCH 11/41] Resolving API_KEY to apikey --- .../dev/langchain4j/googleaigemini/AutoConfig.java | 4 ++-- .../googleaigemini/ChatModelProperties.java | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java index 4bcb88af..a5b220c2 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java @@ -22,7 +22,7 @@ public class AutoConfig { ChatLanguageModel googleAiGeminiChatModel(Properties properties) { ChatModelProperties chatModelProperties = properties.getChatModel(); return GoogleAiGeminiChatModel.builder() - .apiKey(chatModelProperties.getAPI_KEY()) + .apiKey(chatModelProperties.getApikey()) .modelName(chatModelProperties.getModelName()) .temperature(chatModelProperties.getTemperature()) .topP(chatModelProperties.getTopP()) @@ -37,7 +37,7 @@ ChatLanguageModel googleAiGeminiChatModel(Properties properties) { StreamingChatLanguageModel googleAiGeminiStreamingChatModel(Properties properties) { ChatModelProperties chatModelProperties = properties.getChatModel(); return GoogleAiGeminiStreamingChatModel.builder() - .apiKey(chatModelProperties.getAPI_KEY()) + .apiKey(chatModelProperties.getApikey()) .modelName(chatModelProperties.getModelName()) .temperature(chatModelProperties.getTemperature()) .topP(chatModelProperties.getTopP()) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java index 27aabfbe..a50a2177 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java @@ -3,18 +3,19 @@ public class ChatModelProperties { - private String API_KEY; + private String apikey; private String modelName; private double temperature; private double topP; private Integer topK; - public String getAPI_KEY() { - return API_KEY; + + public String getApikey() { + return apikey; } - public void setAPI_KEY(String API_KEY) { - this.API_KEY = API_KEY; + public void setApikey(String apikey) { + this.apikey = apikey; } public String getModelName() { From 4b8f2ecfedebccf0c52864344da1da4d35981d13 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 14 Nov 2024 17:21:28 +0530 Subject: [PATCH 12/41] Resolving API_KEY to apikey --- .../dev/langchain4j/googleaigemini/AutoConfig.java | 2 +- .../googleaigemini/ChatModelProperties.java | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java index a5b220c2..cbcbf4b9 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java @@ -22,7 +22,7 @@ public class AutoConfig { ChatLanguageModel googleAiGeminiChatModel(Properties properties) { ChatModelProperties chatModelProperties = properties.getChatModel(); return GoogleAiGeminiChatModel.builder() - .apiKey(chatModelProperties.getApikey()) + .apiKey(chatModelProperties.getApiKey()) .modelName(chatModelProperties.getModelName()) .temperature(chatModelProperties.getTemperature()) .topP(chatModelProperties.getTopP()) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java index a50a2177..992060c9 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java @@ -3,19 +3,19 @@ public class ChatModelProperties { - private String apikey; + private String apiKey; private String modelName; private double temperature; private double topP; private Integer topK; - public String getApikey() { - return apikey; + public String getApiKey() { + return apiKey; } - public void setApikey(String apikey) { - this.apikey = apikey; + public void setApiKey(String apiKey) { + this.apiKey = apiKey; } public String getModelName() { From 9453ced19f94625374679b3e28fce812422b01b9 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 14 Nov 2024 17:22:29 +0530 Subject: [PATCH 13/41] Resolving API_KEY to apikey --- .../main/java/dev/langchain4j/googleaigemini/AutoConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java index cbcbf4b9..81d2608d 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java @@ -37,7 +37,7 @@ ChatLanguageModel googleAiGeminiChatModel(Properties properties) { StreamingChatLanguageModel googleAiGeminiStreamingChatModel(Properties properties) { ChatModelProperties chatModelProperties = properties.getChatModel(); return GoogleAiGeminiStreamingChatModel.builder() - .apiKey(chatModelProperties.getApikey()) + .apiKey(chatModelProperties.getApiKey()) .modelName(chatModelProperties.getModelName()) .temperature(chatModelProperties.getTemperature()) .topP(chatModelProperties.getTopP()) From 4f267fff61766e6b4dfbbbfd69bd125023667ac2 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Sat, 16 Nov 2024 02:28:16 +0530 Subject: [PATCH 14/41] Change Response Format to User Defined Props --- .../dev/langchain4j/googleaigemini/AutoConfig.java | 4 ++-- .../googleaigemini/ChatModelProperties.java | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java index 81d2608d..c638b175 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java @@ -28,7 +28,7 @@ ChatLanguageModel googleAiGeminiChatModel(Properties properties) { .topP(chatModelProperties.getTopP()) .topK(chatModelProperties.getTopK()) .maxOutputTokens(chatModelProperties.getMaxOutputTokens()) - .responseFormat(ResponseFormat.JSON) + .responseFormat(chatModelProperties.getResponseFormat()) .build(); } @@ -42,7 +42,7 @@ StreamingChatLanguageModel googleAiGeminiStreamingChatModel(Properties propertie .temperature(chatModelProperties.getTemperature()) .topP(chatModelProperties.getTopP()) .topK(chatModelProperties.getTopK()) - .maxOutputTokens(chatModelProperties.getMaxOutputTokens()) + .responseFormat(chatModelProperties.getResponseFormat()) .responseFormat(ResponseFormat.JSON) .build(); } diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java index 992060c9..7e4502e0 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java @@ -1,6 +1,8 @@ package dev.langchain4j.googleaigemini; +import dev.langchain4j.model.chat.request.ResponseFormat; + public class ChatModelProperties { private String apiKey; @@ -8,7 +10,15 @@ public class ChatModelProperties { private double temperature; private double topP; private Integer topK; + private ResponseFormat responseFormat; + + public ResponseFormat getResponseFormat() { + return responseFormat; + } + public void setResponseFormat(ResponseFormat responseFormat) { + this.responseFormat = responseFormat; + } public String getApiKey() { return apiKey; From affefe3d1542102160e56cd30606308ba778168d Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Sat, 16 Nov 2024 02:32:13 +0530 Subject: [PATCH 15/41] Change Response Format to User Defined Props --- .../java/dev/langchain4j/googleaigemini/AutoConfig.java | 4 +++- .../langchain4j/googleaigemini/ChatModelProperties.java | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java index c638b175..32c940ad 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java @@ -29,6 +29,7 @@ ChatLanguageModel googleAiGeminiChatModel(Properties properties) { .topK(chatModelProperties.getTopK()) .maxOutputTokens(chatModelProperties.getMaxOutputTokens()) .responseFormat(chatModelProperties.getResponseFormat()) + .logRequestsAndResponses(chatModelProperties.isLogRequestsAndResponses()) .build(); } @@ -43,7 +44,8 @@ StreamingChatLanguageModel googleAiGeminiStreamingChatModel(Properties propertie .topP(chatModelProperties.getTopP()) .topK(chatModelProperties.getTopK()) .responseFormat(chatModelProperties.getResponseFormat()) - .responseFormat(ResponseFormat.JSON) + .responseFormat(chatModelProperties.getResponseFormat()) + .logRequestsAndResponses(chatModelProperties.isLogRequestsAndResponses()) .build(); } } \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java index 7e4502e0..697afe20 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java @@ -11,6 +11,15 @@ public class ChatModelProperties { private double topP; private Integer topK; private ResponseFormat responseFormat; + private boolean logRequestsAndResponses; + + public boolean isLogRequestsAndResponses() { + return logRequestsAndResponses; + } + + public void setLogRequestsAndResponses(boolean logRequestsAndResponses) { + this.logRequestsAndResponses = logRequestsAndResponses; + } public ResponseFormat getResponseFormat() { return responseFormat; From 76053d999c4dd33e8ed3efef949573b4c1ca040a Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Sat, 16 Nov 2024 03:59:05 +0530 Subject: [PATCH 16/41] Write test to check working of ChatLangaugeModel and StreamingCHatLangaugeModel --- .../pom.xml | 1 + .../googleaigemini/AutoConfig.java | 2 - .../googleaigemini/ChatModelProperties.java | 121 +++++++++--------- .../googleaigemini/Properties.java | 2 +- .../src/main/resources/application.properties | 22 ++++ .../resources/application.properties.local | 15 --- .../googleaigemini/AutoConfigIT.java | 82 ++++++++++++ ...chain4jGoogleAiGeminiApplicationTests.java | 13 -- 8 files changed, 166 insertions(+), 92 deletions(-) create mode 100644 langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties delete mode 100644 langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties.local create mode 100644 langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java delete mode 100644 langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplicationTests.java diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml index 0949d861..0ca5b1a5 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml +++ b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml @@ -37,6 +37,7 @@ spring-boot-starter-test test + diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java index 32c940ad..c5094f14 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java @@ -2,7 +2,6 @@ import dev.langchain4j.model.chat.ChatLanguageModel; import dev.langchain4j.model.chat.StreamingChatLanguageModel; -import dev.langchain4j.model.chat.request.ResponseFormat; import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel; import dev.langchain4j.model.googleai.GoogleAiGeminiStreamingChatModel; @@ -44,7 +43,6 @@ StreamingChatLanguageModel googleAiGeminiStreamingChatModel(Properties propertie .topP(chatModelProperties.getTopP()) .topK(chatModelProperties.getTopK()) .responseFormat(chatModelProperties.getResponseFormat()) - .responseFormat(chatModelProperties.getResponseFormat()) .logRequestsAndResponses(chatModelProperties.isLogRequestsAndResponses()) .build(); } diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java index 697afe20..672dcd48 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java @@ -1,82 +1,81 @@ -package dev.langchain4j.googleaigemini; + package dev.langchain4j.googleaigemini; -import dev.langchain4j.model.chat.request.ResponseFormat; + import dev.langchain4j.model.chat.request.ResponseFormat; -public class ChatModelProperties { + public class ChatModelProperties { - private String apiKey; - private String modelName; - private double temperature; - private double topP; - private Integer topK; - private ResponseFormat responseFormat; - private boolean logRequestsAndResponses; + private String apiKey; + private String modelName; + private double temperature; + private double topP; + private Integer topK; + private Integer maxOutputTokens; + private ResponseFormat responseFormat; + private boolean logRequestsAndResponses; - public boolean isLogRequestsAndResponses() { - return logRequestsAndResponses; - } + public boolean isLogRequestsAndResponses() { + return logRequestsAndResponses; + } - public void setLogRequestsAndResponses(boolean logRequestsAndResponses) { - this.logRequestsAndResponses = logRequestsAndResponses; - } + public void setLogRequestsAndResponses(boolean logRequestsAndResponses) { + this.logRequestsAndResponses = logRequestsAndResponses; + } - public ResponseFormat getResponseFormat() { - return responseFormat; - } + public ResponseFormat getResponseFormat() { + return responseFormat; + } - public void setResponseFormat(ResponseFormat responseFormat) { - this.responseFormat = responseFormat; - } + public void setResponseFormat(ResponseFormat responseFormat) { + this.responseFormat = responseFormat; + } - public String getApiKey() { - return apiKey; - } + public String getApiKey() { + return apiKey; + } - public void setApiKey(String apiKey) { - this.apiKey = apiKey; - } - - public String getModelName() { - return modelName; - } + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } - public void setModelName(String modelName) { - this.modelName = modelName; - } + public String getModelName() { + return modelName; + } - public double getTemperature() { - return temperature; - } + public void setModelName(String modelName) { + this.modelName = modelName; + } - public void setTemperature(double temperature) { - this.temperature = temperature; - } + public double getTemperature() { + return temperature; + } - public double getTopP() { - return topP; - } + public void setTemperature(double temperature) { + this.temperature = temperature; + } - public void setTopP(double topP) { - this.topP = topP; - } + public double getTopP() { + return topP; + } - public Integer getTopK() { - return topK; - } + public void setTopP(double topP) { + this.topP = topP; + } - public void setTopK(Integer topK) { - this.topK = topK; - } + public Integer getTopK() { + return topK; + } - public Integer getMaxOutputTokens() { - return maxOutputTokens; - } + public void setTopK(Integer topK) { + this.topK = topK; + } - public void setMaxOutputTokens(Integer maxOutputTokens) { - this.maxOutputTokens = maxOutputTokens; - } + public Integer getMaxOutputTokens() { + return maxOutputTokens; + } - private Integer maxOutputTokens; + public void setMaxOutputTokens(Integer maxOutputTokens) { + this.maxOutputTokens = maxOutputTokens; + } -} + } diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java index c78ebf32..13178ecb 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java @@ -6,7 +6,7 @@ @ConfigurationProperties(prefix = Properties.PREFIX) public class Properties { - static final String PREFIX = "langchain4j.googleaigemini"; + static final String PREFIX = "langchain4j.google-ai-gemini"; @NestedConfigurationProperty ChatModelProperties chatModel; diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties new file mode 100644 index 00000000..91fe887b --- /dev/null +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties @@ -0,0 +1,22 @@ + +# Google AI Gemini Configuration +langchain4j.google-ai-gemini.apiKey=API_KEY_HERE + +langchain4j.google-ai-gemini.enabled=true + +# Chat Model Configuration +langchain4j.google-ai-gemini.chatModel.enabled=true +langchain4j.google-ai-gemini.chatModel.modelName=gemini-1.5-flash +langchain4j.google-ai-gemini.chatModel.temperature=0.7 +langchain4j.google-ai-gemini.chatModel.topP=0.9 +langchain4j.google-ai-gemini.chatModel.topK=40 +langchain4j.google-ai-gemini.chatModel.maxOutputTokens=100 + +# Streaming Chat Model Configuration +langchain4j.google-ai-gemini.streamingChatModel.enabled=true +langchain4j.google-ai-gemini.streamingChatModel.modelName=gemini-1.5-flash +langchain4j.google-ai-gemini.streamingChatModel.temperature=0.7 +langchain4j.google-ai-gemini.streamingChatModel.topP=0.9 +langchain4j.google-ai-gemini.streamingChatModel.topK=40 +langchain4j.google-ai-gemini.streamingChatModel.maxOutputTokens=100 + diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties.local b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties.local deleted file mode 100644 index 7451f208..00000000 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties.local +++ /dev/null @@ -1,15 +0,0 @@ -spring.application.name=langchain4j-google-ai-gemini - -# Enable Google AI Gemini chat model -langchain4j.googleaigemini.chatModel.enabled=true -langchain4j.googleaigemini.streamingChatModel.enabled=true - -# API Key for Google AI Gemini -langchain4j.googleaigemini.chatModel.apiKey=YOUR_API_KEY_HERE - -# Model configuration -langchain4j.googleaigemini.chatModel.modelName=YOUR_MODEL_NAME_HERE -langchain4j.googleaigemini.chatModel.temperature=0.7 -langchain4j.googleaigemini.chatModel.topP=0.9 -langchain4j.googleaigemini.chatModel.topK=40 -langchain4j.googleaigemini.chatModel.maxOutputTokens=200 diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java new file mode 100644 index 00000000..1629ff50 --- /dev/null +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java @@ -0,0 +1,82 @@ +package dev.langchain4j.googleaigemini; + +import dev.langchain4j.data.message.AiMessage; +import dev.langchain4j.model.StreamingResponseHandler; +import dev.langchain4j.model.chat.ChatLanguageModel; +import dev.langchain4j.model.chat.StreamingChatLanguageModel; +import dev.langchain4j.model.output.Response; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import java.util.concurrent.CompletableFuture; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(classes = AutoConfig.class) +class AutoConfigIT { + + private static final String API_KEY = System.getenv("GOOGLE_API_KEY"); + + ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(AutoConfig.class)); + + @Test + void provide_chat_model() { + contextRunner.withPropertyValues( + "langchain4j.google-ai-gemini.apiKey=" + API_KEY, + "langchain4j.google-ai-gemini.enabled=true", + "langchain4j.google-ai-gemini.chatModel.enabled=true", + "langchain4j.google-ai-gemini.chatModel.modelName=gemini-1.5-flash", + "langchain4j.google-ai-gemini.chatModel.temperature=0.7", + "langchain4j.google-ai-gemini.chatModel.topP=0.9", + "langchain4j.google-ai-gemini.chatModel.topK=40", + "langchain4j.google-ai-gemini.chatModel.maxOutputTokens=100" + ) + .run(context -> { + ChatLanguageModel chatLanguageModel = context.getBean(ChatLanguageModel.class); + assertThat(chatLanguageModel).isInstanceOf(ChatLanguageModel.class); + String response = chatLanguageModel.generate("What is the capital of India"); + assertThat(response).contains("Delhi"); + }); + } + + @Test + void provide_streaming_chat_model() { + contextRunner.withPropertyValues( + "langchain4j.google-ai-gemini.apiKey=" + API_KEY, + "langchain4j.google-ai-gemini.enabled=true", + "langchain4j.google-ai-gemini.streamingChatModel.enabled=true", + "langchain4j.google-ai-gemini.streamingChatModel.modelName=gemini-1.5-flash", + "langchain4j.google-ai-gemini.streamingChatModel.temperature=0.7", + "langchain4j.google-ai-gemini.streamingChatModel.topP=0.9", + "langchain4j.google-ai-gemini.streamingChatModel.topK=40", + "langchain4j.google-ai-gemini.streamingChatModel.maxOutputTokens=100" + ) + .run(context -> { + StreamingChatLanguageModel streamingChatLanguageModel = context.getBean(StreamingChatLanguageModel.class); + assertThat(streamingChatLanguageModel).isInstanceOf(StreamingChatLanguageModel.class); + + CompletableFuture> future = new CompletableFuture<>(); + streamingChatLanguageModel.generate("What is the capital of India", new StreamingResponseHandler<>() { + @Override + public void onNext(String s) {} + + @Override + public void onComplete(Response response) { + future.complete(response); + } + + @Override + public void onError(Throwable throwable) { + future.completeExceptionally(throwable); + } + }); + + Response response = future.get(60, SECONDS); + assertThat(response.content().text()).contains("Delhi"); + }); + } +} \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplicationTests.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplicationTests.java deleted file mode 100644 index 9ec5d6aa..00000000 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/Langchain4jGoogleAiGeminiApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package dev.langchain4j.googleaigemini; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class Langchain4jGoogleAiGeminiApplicationTests { - - @Test - void contextLoads() { - } - -} From 6ee971ea8d00eb8d40265f0e08ee45d2b0d80661 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Sat, 16 Nov 2024 04:40:18 +0530 Subject: [PATCH 17/41] Complete Test for Chat Language Model --- .../dev/langchain4j/googleaigemini/AutoConfig.java | 4 ++-- .../googleaigemini/ChatModelProperties.java | 9 --------- .../dev/langchain4j/googleaigemini/Properties.java | 10 ++++++++++ .../src/main/resources/application.properties | 2 +- .../dev/langchain4j/googleaigemini/AutoConfigIT.java | 2 +- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java index c5094f14..2e8f3126 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java @@ -21,7 +21,7 @@ public class AutoConfig { ChatLanguageModel googleAiGeminiChatModel(Properties properties) { ChatModelProperties chatModelProperties = properties.getChatModel(); return GoogleAiGeminiChatModel.builder() - .apiKey(chatModelProperties.getApiKey()) + .apiKey(properties.getApiKey()) .modelName(chatModelProperties.getModelName()) .temperature(chatModelProperties.getTemperature()) .topP(chatModelProperties.getTopP()) @@ -37,7 +37,7 @@ ChatLanguageModel googleAiGeminiChatModel(Properties properties) { StreamingChatLanguageModel googleAiGeminiStreamingChatModel(Properties properties) { ChatModelProperties chatModelProperties = properties.getChatModel(); return GoogleAiGeminiStreamingChatModel.builder() - .apiKey(chatModelProperties.getApiKey()) + .apiKey(properties.getApiKey()) .modelName(chatModelProperties.getModelName()) .temperature(chatModelProperties.getTemperature()) .topP(chatModelProperties.getTopP()) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java index 672dcd48..fa5f73a3 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java @@ -5,7 +5,6 @@ public class ChatModelProperties { - private String apiKey; private String modelName; private double temperature; private double topP; @@ -30,14 +29,6 @@ public void setResponseFormat(ResponseFormat responseFormat) { this.responseFormat = responseFormat; } - public String getApiKey() { - return apiKey; - } - - public void setApiKey(String apiKey) { - this.apiKey = apiKey; - } - public String getModelName() { return modelName; } diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java index 13178ecb..759b2741 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java @@ -11,6 +11,16 @@ public class Properties { @NestedConfigurationProperty ChatModelProperties chatModel; + public String apiKey; + + public String getApiKey() { + return apiKey; + } + + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + public ChatModelProperties getStreamingChatModel() { return streamingChatModel; } diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties index 91fe887b..645a8695 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties @@ -1,6 +1,6 @@ # Google AI Gemini Configuration -langchain4j.google-ai-gemini.apiKey=API_KEY_HERE +langchain4j.google-ai-gemini.apiKey=AIzaSyCML0GPDaTfToi18eHdyntRnV1QsTO_bek langchain4j.google-ai-gemini.enabled=true diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java index 1629ff50..56226f46 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java @@ -18,7 +18,7 @@ @SpringBootTest(classes = AutoConfig.class) class AutoConfigIT { - private static final String API_KEY = System.getenv("GOOGLE_API_KEY"); + private static final String API_KEY = " AIzaSyCML0GPDaTfToi18eHdyntRnV1QsTO_bek"; ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(AutoConfig.class)); From 637cd0e675c709c43e262db58577077ec4804bfb Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Sat, 16 Nov 2024 04:41:18 +0530 Subject: [PATCH 18/41] Complete Test for Chat Language Model --- .../src/main/resources/application.properties | 2 +- .../test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties index 645a8695..508439fe 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties @@ -1,6 +1,6 @@ # Google AI Gemini Configuration -langchain4j.google-ai-gemini.apiKey=AIzaSyCML0GPDaTfToi18eHdyntRnV1QsTO_bek +langchain4j.google-ai-gemini.apiKey=YOUR_API_KEY langchain4j.google-ai-gemini.enabled=true diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java index 56226f46..0f6128d0 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java @@ -18,7 +18,7 @@ @SpringBootTest(classes = AutoConfig.class) class AutoConfigIT { - private static final String API_KEY = " AIzaSyCML0GPDaTfToi18eHdyntRnV1QsTO_bek"; + private static final String API_KEY = " YOUR_API_KEY"; ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(AutoConfig.class)); From 3e5fca5141c14ebb3ceef3a331dcaab4a44014c3 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Sat, 16 Nov 2024 04:58:31 +0530 Subject: [PATCH 19/41] Implement test for Streaming Chat Language Model --- .../googleaigemini/AutoConfig.java | 2 +- .../googleaigemini/Properties.java | 12 ++--- .../src/main/resources/application.properties | 44 +++++++++---------- .../googleaigemini/AutoConfigIT.java | 2 +- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java index 2e8f3126..6afb965d 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java @@ -35,7 +35,7 @@ ChatLanguageModel googleAiGeminiChatModel(Properties properties) { @Bean @ConditionalOnProperty(name = PREFIX + ".streamingChatModel.enabled", havingValue = "true") StreamingChatLanguageModel googleAiGeminiStreamingChatModel(Properties properties) { - ChatModelProperties chatModelProperties = properties.getChatModel(); + ChatModelProperties chatModelProperties = properties.getStreamingChatModel(); return GoogleAiGeminiStreamingChatModel.builder() .apiKey(properties.getApiKey()) .modelName(chatModelProperties.getModelName()) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java index 759b2741..3da3a16b 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java @@ -9,9 +9,12 @@ public class Properties { static final String PREFIX = "langchain4j.google-ai-gemini"; @NestedConfigurationProperty - ChatModelProperties chatModel; + private ChatModelProperties chatModel; - public String apiKey; + @NestedConfigurationProperty + private ChatModelProperties streamingChatModel; + + private String apiKey; public String getApiKey() { return apiKey; @@ -36,7 +39,4 @@ public ChatModelProperties getChatModel() { public void setChatModel(ChatModelProperties chatModel) { this.chatModel = chatModel; } - - @NestedConfigurationProperty - ChatModelProperties streamingChatModel; -} +} \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties index 508439fe..79566007 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties @@ -1,22 +1,22 @@ - -# Google AI Gemini Configuration -langchain4j.google-ai-gemini.apiKey=YOUR_API_KEY - -langchain4j.google-ai-gemini.enabled=true - -# Chat Model Configuration -langchain4j.google-ai-gemini.chatModel.enabled=true -langchain4j.google-ai-gemini.chatModel.modelName=gemini-1.5-flash -langchain4j.google-ai-gemini.chatModel.temperature=0.7 -langchain4j.google-ai-gemini.chatModel.topP=0.9 -langchain4j.google-ai-gemini.chatModel.topK=40 -langchain4j.google-ai-gemini.chatModel.maxOutputTokens=100 - -# Streaming Chat Model Configuration -langchain4j.google-ai-gemini.streamingChatModel.enabled=true -langchain4j.google-ai-gemini.streamingChatModel.modelName=gemini-1.5-flash -langchain4j.google-ai-gemini.streamingChatModel.temperature=0.7 -langchain4j.google-ai-gemini.streamingChatModel.topP=0.9 -langchain4j.google-ai-gemini.streamingChatModel.topK=40 -langchain4j.google-ai-gemini.streamingChatModel.maxOutputTokens=100 - +# +## Google AI Gemini Configuration +#langchain4j.google-ai-gemini.apiKey=YOUR_API_KEY +# +#langchain4j.google-ai-gemini.enabled=true +# +## Chat Model Configuration +#langchain4j.google-ai-gemini.chatModel.enabled=true +#langchain4j.google-ai-gemini.chatModel.modelName=gemini-1.5-flash +#langchain4j.google-ai-gemini.chatModel.temperature=0.7 +#langchain4j.google-ai-gemini.chatModel.topP=0.9 +#langchain4j.google-ai-gemini.chatModel.topK=40 +#langchain4j.google-ai-gemini.chatModel.maxOutputTokens=100 +# +## Streaming Chat Model Configuration +#langchain4j.google-ai-gemini.streamingChatModel.enabled=true +#langchain4j.google-ai-gemini.streamingChatModel.modelName=gemini-1.5-flash +#langchain4j.google-ai-gemini.streamingChatModel.temperature=0.7 +#langchain4j.google-ai-gemini.streamingChatModel.topP=0.9 +#langchain4j.google-ai-gemini.streamingChatModel.topK=40 +#langchain4j.google-ai-gemini.streamingChatModel.maxOutputTokens=100 +# diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java index 0f6128d0..9c3e818b 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java @@ -18,7 +18,7 @@ @SpringBootTest(classes = AutoConfig.class) class AutoConfigIT { - private static final String API_KEY = " YOUR_API_KEY"; + private static final String API_KEY = "YOUR_API_KEY"; // Replace with your actual API key ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(AutoConfig.class)); From cfc5cae1ebfba4174f10e4f139ba1e18c12d3280 Mon Sep 17 00:00:00 2001 From: Suhas Koheda Date: Sat, 16 Nov 2024 05:01:13 +0530 Subject: [PATCH 20/41] Delete langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties --- .../src/main/resources/application.properties | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties deleted file mode 100644 index 79566007..00000000 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/application.properties +++ /dev/null @@ -1,22 +0,0 @@ -# -## Google AI Gemini Configuration -#langchain4j.google-ai-gemini.apiKey=YOUR_API_KEY -# -#langchain4j.google-ai-gemini.enabled=true -# -## Chat Model Configuration -#langchain4j.google-ai-gemini.chatModel.enabled=true -#langchain4j.google-ai-gemini.chatModel.modelName=gemini-1.5-flash -#langchain4j.google-ai-gemini.chatModel.temperature=0.7 -#langchain4j.google-ai-gemini.chatModel.topP=0.9 -#langchain4j.google-ai-gemini.chatModel.topK=40 -#langchain4j.google-ai-gemini.chatModel.maxOutputTokens=100 -# -## Streaming Chat Model Configuration -#langchain4j.google-ai-gemini.streamingChatModel.enabled=true -#langchain4j.google-ai-gemini.streamingChatModel.modelName=gemini-1.5-flash -#langchain4j.google-ai-gemini.streamingChatModel.temperature=0.7 -#langchain4j.google-ai-gemini.streamingChatModel.topP=0.9 -#langchain4j.google-ai-gemini.streamingChatModel.topK=40 -#langchain4j.google-ai-gemini.streamingChatModel.maxOutputTokens=100 -# From 66c828ecaa55c27316473bced481aae98bb7d8d7 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Sun, 17 Nov 2024 00:44:58 +0530 Subject: [PATCH 21/41] Changing Java Version in pom.xml from 21 to 17 to support maven (mvn) operations --- langchain4j-google-ai-gemini-spring-boot-starter/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml index 0ca5b1a5..0b17d719 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml +++ b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml @@ -16,7 +16,7 @@ - 21 + 17 From ec1275bb96443f9570495b7b4ac4bb54365d9a36 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Sun, 17 Nov 2024 02:23:25 +0530 Subject: [PATCH 22/41] Changing Java version back to 21 :) in pom.xml --- langchain4j-google-ai-gemini-spring-boot-starter/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml index 0b17d719..0ca5b1a5 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml +++ b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml @@ -16,7 +16,7 @@ - 17 + 21 From 0fd6868880d5d36422fac8b7df97a65ce80299ea Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Mon, 18 Nov 2024 21:45:49 +0530 Subject: [PATCH 23/41] Resolving other issues except Embedding model --- .../pom.xml | 54 ++++++++++++------- .../{ => spring}/AutoConfig.java | 4 +- .../{ => spring}/ChatModelProperties.java | 2 +- .../{ => spring}/Properties.java | 2 +- ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../{ => spring}/AutoConfigIT.java | 10 ++-- pom.xml | 2 +- 7 files changed, 48 insertions(+), 27 deletions(-) rename langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/{ => spring}/AutoConfig.java (95%) rename langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/{ => spring}/ChatModelProperties.java (97%) rename langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/{ => spring}/Properties.java (95%) create mode 100644 langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports rename langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/{ => spring}/AutoConfigIT.java (88%) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml index 0ca5b1a5..c91e0317 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml +++ b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml @@ -1,6 +1,7 @@ - + 4.0.0 @@ -11,16 +12,11 @@ langchain4j-google-ai-gemini-spring-boot-starter + LangChain4j Spring Boot starter for Google Gemini AI + jar - Langchain4j Spring Boot Starter for Google Gemini AI Model - - - - 21 - - dev.langchain4j langchain4j-google-ai-gemini @@ -32,21 +28,43 @@ spring-boot-starter + + org.springframework.boot + spring-boot-autoconfigure-processor + true + + + + + org.projectlombok + lombok + provided + + + + + org.springframework.boot + spring-boot-configuration-processor + true + + org.springframework.boot spring-boot-starter-test test + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + Apache-2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + repo + A business-friendly OSS license + + - + \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java similarity index 95% rename from langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java rename to langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java index 6afb965d..f31cf8a1 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java @@ -1,4 +1,4 @@ -package dev.langchain4j.googleaigemini; +package dev.langchain4j.googleaigemini.spring; import dev.langchain4j.model.chat.ChatLanguageModel; import dev.langchain4j.model.chat.StreamingChatLanguageModel; @@ -10,7 +10,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; -import static dev.langchain4j.googleaigemini.Properties.PREFIX; +import static dev.langchain4j.googleaigemini.spring.Properties.PREFIX; @AutoConfiguration @EnableConfigurationProperties(Properties.class) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java similarity index 97% rename from langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java rename to langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java index fa5f73a3..5967c8fb 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java @@ -1,4 +1,4 @@ - package dev.langchain4j.googleaigemini; + package dev.langchain4j.googleaigemini.spring; import dev.langchain4j.model.chat.request.ResponseFormat; diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java similarity index 95% rename from langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java rename to langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java index 3da3a16b..b48302f2 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/Properties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java @@ -1,4 +1,4 @@ -package dev.langchain4j.googleaigemini; +package dev.langchain4j.googleaigemini.spring; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..8a8a5b16 --- /dev/null +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +dev.langchain4j.googleaigemini.spring.AutoConfig \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java similarity index 88% rename from langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java rename to langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java index 9c3e818b..b215268e 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/AutoConfigIT.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java @@ -1,9 +1,11 @@ -package dev.langchain4j.googleaigemini; +package dev.langchain4j.googleaigemini.spring; import dev.langchain4j.data.message.AiMessage; import dev.langchain4j.model.StreamingResponseHandler; import dev.langchain4j.model.chat.ChatLanguageModel; import dev.langchain4j.model.chat.StreamingChatLanguageModel; +import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel; +import dev.langchain4j.model.googleai.GoogleAiGeminiStreamingChatModel; import dev.langchain4j.model.output.Response; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -15,10 +17,9 @@ import static java.util.concurrent.TimeUnit.SECONDS; import static org.assertj.core.api.Assertions.assertThat; -@SpringBootTest(classes = AutoConfig.class) class AutoConfigIT { - private static final String API_KEY = "YOUR_API_KEY"; // Replace with your actual API key + private static final String API_KEY =System.getenv("GOOGLE_API_KEY"); ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(AutoConfig.class)); @@ -38,6 +39,7 @@ void provide_chat_model() { .run(context -> { ChatLanguageModel chatLanguageModel = context.getBean(ChatLanguageModel.class); assertThat(chatLanguageModel).isInstanceOf(ChatLanguageModel.class); + assertThat(context.getBean(GoogleAiGeminiChatModel.class)).isSameAs(chatLanguageModel); String response = chatLanguageModel.generate("What is the capital of India"); assertThat(response).contains("Delhi"); }); @@ -58,7 +60,7 @@ void provide_streaming_chat_model() { .run(context -> { StreamingChatLanguageModel streamingChatLanguageModel = context.getBean(StreamingChatLanguageModel.class); assertThat(streamingChatLanguageModel).isInstanceOf(StreamingChatLanguageModel.class); - + assertThat(context.getBean(GoogleAiGeminiStreamingChatModel.class)).isSameAs(streamingChatLanguageModel); CompletableFuture> future = new CompletableFuture<>(); streamingChatLanguageModel.generate("What is the capital of India", new StreamingResponseHandler<>() { @Override diff --git a/pom.xml b/pom.xml index b87d59d2..ffaf03bf 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ langchain4j-azure-open-ai-spring-boot-starter langchain4j-voyage-ai-spring-boot-starter langchain4j-github-models-spring-boot-starter - + langchain4j-google-ai-gemini-spring-boot-starter langchain4j-vertex-ai-gemini-spring-boot-starter langchain4j-elasticsearch-spring-boot-starter langchain4j-redis-spring-boot-starter From 070a71e639f671864793b10a74e6fe36a2eb6e1f Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Mon, 18 Nov 2024 21:48:53 +0530 Subject: [PATCH 24/41] Adding env variable to main.yaml and release.yaml --- .github/workflows/main.yaml | 1 + .github/workflows/release.yaml | 1 + .../dev/langchain4j/googleaigemini/spring/AutoConfigIT.java | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index b5c2555c..e0ea391a 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -36,6 +36,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }} + GOOGLE_AI_GEMINI_API_KEY: ${{secrets.GOOGLE_AI_GEMINI_API_KEY}} OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 13dc7f1e..fe0df526 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -35,6 +35,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }} + GOOGLE_AI_GEMINI_API_KEY: ${{secrets.GOOGLE_AI_GEMINI_API_KEY}} GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java index b215268e..f46f8629 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java @@ -19,7 +19,7 @@ class AutoConfigIT { - private static final String API_KEY =System.getenv("GOOGLE_API_KEY"); + private static final String API_KEY =System.getenv("GOOGLE_AI_GEMINI_API_KEY"); ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(AutoConfig.class)); From 889aa2f214e952df6a4db2507eed575f61bba014 Mon Sep 17 00:00:00 2001 From: Suhas Koheda Date: Mon, 18 Nov 2024 21:56:17 +0530 Subject: [PATCH 25/41] Update release.yaml to add Google AI Gemini API Key --- .github/workflows/release.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 13dc7f1e..fe0df526 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -35,6 +35,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }} + GOOGLE_AI_GEMINI_API_KEY: ${{secrets.GOOGLE_AI_GEMINI_API_KEY}} GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} From db889dbde81b155c841c37c3c34f2dd510f43e84 Mon Sep 17 00:00:00 2001 From: Suhas Koheda Date: Mon, 18 Nov 2024 21:56:43 +0530 Subject: [PATCH 26/41] Update main.yaml to add Google AI Gemini API Key --- .github/workflows/main.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index b5c2555c..9154299d 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -36,6 +36,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }} + GOOGLE_AI_GEMINI_API_KEY: ${{secrets.GOOGLE_AI_GEMINI_API_KEY}} OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} @@ -50,4 +51,4 @@ jobs: distribution: 'temurin' cache: 'maven' - name: License Compliance - run: mvn -U -P compliance org.honton.chas:license-maven-plugin:compliance \ No newline at end of file + run: mvn -U -P compliance org.honton.chas:license-maven-plugin:compliance From 4ae025a5e0532a4b6ab9a6def845fa23700ed06e Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Mon, 18 Nov 2024 22:04:50 +0530 Subject: [PATCH 27/41] Changing the version of dependencies to project version --- langchain4j-google-ai-gemini-spring-boot-starter/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml index c91e0317..65f7043f 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml +++ b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml @@ -20,7 +20,7 @@ dev.langchain4j langchain4j-google-ai-gemini - 0.36.0 + ${project.version} From 9aad39fec889e8db4f3caabddb739c7886fe5c89 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Wed, 20 Nov 2024 00:28:36 +0530 Subject: [PATCH 28/41] adding GeminiSafetyProperties and GeminiSafetySettings to googleGeminiCHatModel --- .../pom.xml | 9 +------ .../googleaigemini/spring/AutoConfig.java | 15 +++++++++++ .../spring/ChatModelProperties.java | 21 +++++++++++++++ .../spring/GeminiFunctionCallingConfig.java | 27 +++++++++++++++++++ .../spring/GeminiSafetySetting.java | 26 ++++++++++++++++++ .../googleaigemini/spring/Properties.java | 26 ++++++++++++++++++ 6 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiFunctionCallingConfig.java create mode 100644 langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml index 65f7043f..a80a083c 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml +++ b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml @@ -20,7 +20,7 @@ dev.langchain4j langchain4j-google-ai-gemini - ${project.version} + 0.37.0-SNAPSHOT @@ -34,13 +34,6 @@ true - - - org.projectlombok - lombok - provided - - org.springframework.boot diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java index f31cf8a1..43bd10cd 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java @@ -2,6 +2,7 @@ import dev.langchain4j.model.chat.ChatLanguageModel; import dev.langchain4j.model.chat.StreamingChatLanguageModel; +import dev.langchain4j.model.googleai.GeminiSafetySetting; import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel; import dev.langchain4j.model.googleai.GoogleAiGeminiStreamingChatModel; @@ -10,6 +11,9 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; + +import java.util.Map; + import static dev.langchain4j.googleaigemini.spring.Properties.PREFIX; @AutoConfiguration @@ -29,6 +33,12 @@ ChatLanguageModel googleAiGeminiChatModel(Properties properties) { .maxOutputTokens(chatModelProperties.getMaxOutputTokens()) .responseFormat(chatModelProperties.getResponseFormat()) .logRequestsAndResponses(chatModelProperties.isLogRequestsAndResponses()) + .safetySettings( + Map.of(properties.getSafetySetting().getGeminiHarmCategory(), + properties.getSafetySetting().getGeminiHarmBlockThreshold())) + .toolConfig( + properties.getFunctionCallingConfig().getGeminiMode(), + properties.getFunctionCallingConfig().getAllowedFunctionNames().toArray(new String[0])) .build(); } @@ -44,6 +54,11 @@ StreamingChatLanguageModel googleAiGeminiStreamingChatModel(Properties propertie .topK(chatModelProperties.getTopK()) .responseFormat(chatModelProperties.getResponseFormat()) .logRequestsAndResponses(chatModelProperties.isLogRequestsAndResponses()) +// .safetySettings( +// List.of(GeminiSafetySetting)) +// .toolConfig( +// properties.getFunctionCallingConfig().getGeminiMode(), +// properties.getFunctionCallingConfig().getAllowedFunctionNames().toArray(new String[0])) .build(); } } \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java index 5967c8fb..d5c7316b 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java @@ -3,6 +3,8 @@ import dev.langchain4j.model.chat.request.ResponseFormat; + import java.time.Duration; + public class ChatModelProperties { private String modelName; @@ -12,6 +14,25 @@ public class ChatModelProperties { private Integer maxOutputTokens; private ResponseFormat responseFormat; private boolean logRequestsAndResponses; + private Integer maxRetries; + + public Integer getMaxRetries() { + return maxRetries; + } + + public void setMaxRetries(Integer maxRetries) { + this.maxRetries = maxRetries; + } + + public Duration getTimeout() { + return timeout; + } + + public void setTimeout(Duration timeout) { + this.timeout = timeout; + } + + private Duration timeout; public boolean isLogRequestsAndResponses() { return logRequestsAndResponses; diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiFunctionCallingConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiFunctionCallingConfig.java new file mode 100644 index 00000000..700bbc68 --- /dev/null +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiFunctionCallingConfig.java @@ -0,0 +1,27 @@ +package dev.langchain4j.googleaigemini.spring; + +import dev.langchain4j.model.googleai.GeminiMode; + +import java.util.List; + +public class GeminiFunctionCallingConfig { + + private GeminiMode geminiMode; + private List allowedFunctionNames; + + public GeminiMode getGeminiMode() { + return geminiMode; + } + + public void setGeminiMode(GeminiMode geminiMode) { + this.geminiMode = geminiMode; + } + + public List getAllowedFunctionNames() { + return allowedFunctionNames; + } + + public void setAllowedFunctionNames(List allowedFunctionNames) { + this.allowedFunctionNames = allowedFunctionNames; + } +} diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java new file mode 100644 index 00000000..407bb3fa --- /dev/null +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java @@ -0,0 +1,26 @@ +package dev.langchain4j.googleaigemini.spring; + +import dev.langchain4j.model.googleai.GeminiHarmBlockThreshold; +import dev.langchain4j.model.googleai.GeminiHarmCategory; + +public class GeminiSafetySetting { + + private GeminiHarmCategory geminiHarmCategory; + private GeminiHarmBlockThreshold geminiHarmBlockThreshold; + + public GeminiHarmCategory getGeminiHarmCategory() { + return geminiHarmCategory; + } + + public void setGeminiHarmCategory(GeminiHarmCategory geminiHarmCategory) { + this.geminiHarmCategory = geminiHarmCategory; + } + + public GeminiHarmBlockThreshold getGeminiHarmBlockThreshold() { + return geminiHarmBlockThreshold; + } + + public void setGeminiHarmBlockThreshold(GeminiHarmBlockThreshold geminiHarmBlockThreshold) { + this.geminiHarmBlockThreshold = geminiHarmBlockThreshold; + } +} diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java index b48302f2..957e2260 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java @@ -1,8 +1,12 @@ package dev.langchain4j.googleaigemini.spring; +import dev.langchain4j.googleaigemini.spring.GeminiSafetySetting; +import dev.langchain4j.googleaigemini.spring.GeminiFunctionCallingConfig; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; +import java.util.List; + @ConfigurationProperties(prefix = Properties.PREFIX) public class Properties { @@ -16,6 +20,28 @@ public class Properties { private String apiKey; + @NestedConfigurationProperty + private GeminiSafetySetting safetySetting; + + public GeminiSafetySetting getSafetySetting() { + return safetySetting; + } + + public void setSafetySetting(GeminiSafetySetting safetySetting) { + this.safetySetting = safetySetting; + } + + public GeminiFunctionCallingConfig getFunctionCallingConfig() { + return functionCallingConfig; + } + + public void setFunctionCallingConfig(GeminiFunctionCallingConfig functionCallingConfig) { + this.functionCallingConfig = functionCallingConfig; + } + + @NestedConfigurationProperty + private GeminiFunctionCallingConfig functionCallingConfig; + public String getApiKey() { return apiKey; } From 3c32174783b6e5594cb4d2b287be7b43e17d23a8 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Wed, 20 Nov 2024 03:22:34 +0530 Subject: [PATCH 29/41] Add tests for chatlanguagemodel with updated properties --- .../pom.xml | 2 +- .../googleaigemini/spring/AutoConfigIT.java | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml index a80a083c..1c37e39d 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml +++ b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml @@ -20,7 +20,7 @@ dev.langchain4j langchain4j-google-ai-gemini - 0.37.0-SNAPSHOT + 0.36.0 diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java index f46f8629..8782817e 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java @@ -30,11 +30,15 @@ void provide_chat_model() { "langchain4j.google-ai-gemini.apiKey=" + API_KEY, "langchain4j.google-ai-gemini.enabled=true", "langchain4j.google-ai-gemini.chatModel.enabled=true", - "langchain4j.google-ai-gemini.chatModel.modelName=gemini-1.5-flash", + "langchain4j.google-ai-gemini.chatModel.modelName=gemini-1.0-pro ", "langchain4j.google-ai-gemini.chatModel.temperature=0.7", "langchain4j.google-ai-gemini.chatModel.topP=0.9", "langchain4j.google-ai-gemini.chatModel.topK=40", - "langchain4j.google-ai-gemini.chatModel.maxOutputTokens=100" + "langchain4j.google-ai-gemini.chatModel.maxOutputTokens=100", + "langchain4j.google-ai-gemini.safetySetting.geminiHarmCategory=HARM_CATEGORY_SEXUALLY_EXPLICIT", + "langchain4j.google-ai-gemini.safetySetting.geminiHarmBlockThreshold=HARM_BLOCK_THRESHOLD_UNSPECIFIED", + "langchain4j.google-ai-gemini.functionCallingConfig.geminiMode=ANY", + "langchain4j.google-ai-gemini.functionCallingConfig.allowedFunctionNames=allowCodeExecution,includeCodeExecutionOutput" ) .run(context -> { ChatLanguageModel chatLanguageModel = context.getBean(ChatLanguageModel.class); @@ -42,6 +46,8 @@ void provide_chat_model() { assertThat(context.getBean(GoogleAiGeminiChatModel.class)).isSameAs(chatLanguageModel); String response = chatLanguageModel.generate("What is the capital of India"); assertThat(response).contains("Delhi"); + String newResponse = chatLanguageModel.generate("Calculate the Fibonacci of 22 and return the result as an integer value along with the code. "); + assertThat(newResponse).contains("17711"); }); } From c4ba686b54d7b16863a88c115c5007d1e5603c6e Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Thu, 21 Nov 2024 01:58:53 +0530 Subject: [PATCH 30/41] Adding Embedding model configuration and test for Embedding model --- .../pom.xml | 2 +- .../googleaigemini/spring/AutoConfig.java | 25 +++++-- .../spring/ChatModelProperties.java | 4 +- .../spring/EmbeddingModelProperties.java | 73 +++++++++++++++++++ .../googleaigemini/spring/Properties.java | 25 ++++--- .../googleaigemini/spring/AutoConfigIT.java | 28 ++++++- 6 files changed, 135 insertions(+), 22 deletions(-) create mode 100644 langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml index 1c37e39d..39a168e3 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml +++ b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml @@ -20,7 +20,7 @@ dev.langchain4j langchain4j-google-ai-gemini - 0.36.0 + ${project.version} diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java index 43bd10cd..f3bed6b5 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java @@ -2,7 +2,8 @@ import dev.langchain4j.model.chat.ChatLanguageModel; import dev.langchain4j.model.chat.StreamingChatLanguageModel; -import dev.langchain4j.model.googleai.GeminiSafetySetting; +import dev.langchain4j.model.embedding.EmbeddingModel; +import dev.langchain4j.model.googleai.GoogleAiEmbeddingModel; import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel; import dev.langchain4j.model.googleai.GoogleAiGeminiStreamingChatModel; @@ -11,7 +12,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; - import java.util.Map; import static dev.langchain4j.googleaigemini.spring.Properties.PREFIX; @@ -54,11 +54,22 @@ StreamingChatLanguageModel googleAiGeminiStreamingChatModel(Properties propertie .topK(chatModelProperties.getTopK()) .responseFormat(chatModelProperties.getResponseFormat()) .logRequestsAndResponses(chatModelProperties.isLogRequestsAndResponses()) -// .safetySettings( -// List.of(GeminiSafetySetting)) -// .toolConfig( -// properties.getFunctionCallingConfig().getGeminiMode(), -// properties.getFunctionCallingConfig().getAllowedFunctionNames().toArray(new String[0])) + .build(); + } + + @Bean + @ConditionalOnProperty(name = PREFIX + ".embeddingModel.enabled", havingValue = "true") + EmbeddingModel googleAiGeminiEmbeddingModel(Properties properties) { + EmbeddingModelProperties embeddingModelProperties = properties.getEmbeddingModel(); + return GoogleAiEmbeddingModel.builder() + .apiKey(properties.getApiKey()) + .modelName(embeddingModelProperties.getModelName()) + .logRequestsAndResponses(embeddingModelProperties.isLogRequestsAndResponses()) + .maxRetries(embeddingModelProperties.getMaxRetries()) + .outputDimensionality(embeddingModelProperties.getOutputDimensionality()) + .taskType(embeddingModelProperties.getTaskType()) + .timeout(embeddingModelProperties.getTimeout()) + .titleMetadataKey("title") .build(); } } \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java index d5c7316b..d6f27fdc 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java @@ -1,6 +1,5 @@ package dev.langchain4j.googleaigemini.spring; - import dev.langchain4j.model.chat.request.ResponseFormat; import java.time.Duration; @@ -15,6 +14,7 @@ public class ChatModelProperties { private ResponseFormat responseFormat; private boolean logRequestsAndResponses; private Integer maxRetries; + private Duration timeout; public Integer getMaxRetries() { return maxRetries; @@ -32,8 +32,6 @@ public void setTimeout(Duration timeout) { this.timeout = timeout; } - private Duration timeout; - public boolean isLogRequestsAndResponses() { return logRequestsAndResponses; } diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java new file mode 100644 index 00000000..362328c2 --- /dev/null +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java @@ -0,0 +1,73 @@ +package dev.langchain4j.googleaigemini.spring; + +import dev.langchain4j.model.googleai.GoogleAiEmbeddingModel; +import dev.langchain4j.model.googleai.GoogleAiEmbeddingModel.TaskType; + +import java.time.Duration; + +public class EmbeddingModelProperties { + + private String titleMetadataKey; + private String modelName; + private boolean logRequestsAndResponses; + private Integer maxRetries; + private Integer outputDimensionality; + private TaskType taskType; + private Duration timeout; + + public String getTitleMetadataKey() { + return titleMetadataKey; + } + + public void setTitleMetadataKey(String titleMetadataKey) { + this.titleMetadataKey = titleMetadataKey; + } + + public String getModelName() { + return modelName; + } + + public void setModelName(String modelName) { + this.modelName = modelName; + } + + public boolean isLogRequestsAndResponses() { + return logRequestsAndResponses; + } + + public void setLogRequestsAndResponses(boolean logRequestsAndResponses) { + this.logRequestsAndResponses = logRequestsAndResponses; + } + + public Integer getMaxRetries() { + return maxRetries; + } + + public void setMaxRetries(Integer maxRetries) { + this.maxRetries = maxRetries; + } + + public Integer getOutputDimensionality() { + return outputDimensionality; + } + + public void setOutputDimensionality(Integer outputDimensionality) { + this.outputDimensionality = outputDimensionality; + } + + public GoogleAiEmbeddingModel.TaskType getTaskType() { + return taskType; + } + + public void setTaskType(GoogleAiEmbeddingModel.TaskType taskType) { + this.taskType = taskType; + } + + public Duration getTimeout() { + return timeout; + } + + public void setTimeout(Duration timeout) { + this.timeout = timeout; + } +} diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java index 957e2260..795c1c28 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java @@ -1,12 +1,8 @@ package dev.langchain4j.googleaigemini.spring; -import dev.langchain4j.googleaigemini.spring.GeminiSafetySetting; -import dev.langchain4j.googleaigemini.spring.GeminiFunctionCallingConfig; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; -import java.util.List; - @ConfigurationProperties(prefix = Properties.PREFIX) public class Properties { @@ -18,11 +14,25 @@ public class Properties { @NestedConfigurationProperty private ChatModelProperties streamingChatModel; - private String apiKey; - @NestedConfigurationProperty private GeminiSafetySetting safetySetting; + @NestedConfigurationProperty + private EmbeddingModelProperties embeddingModel; + + @NestedConfigurationProperty + private GeminiFunctionCallingConfig functionCallingConfig; + + private String apiKey; + + public EmbeddingModelProperties getEmbeddingModel() { + return embeddingModel; + } + + public void setEmbeddingModel(EmbeddingModelProperties embeddingModel) { + this.embeddingModel = embeddingModel; + } + public GeminiSafetySetting getSafetySetting() { return safetySetting; } @@ -39,9 +49,6 @@ public void setFunctionCallingConfig(GeminiFunctionCallingConfig functionCalling this.functionCallingConfig = functionCallingConfig; } - @NestedConfigurationProperty - private GeminiFunctionCallingConfig functionCallingConfig; - public String getApiKey() { return apiKey; } diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java index 8782817e..79b9c1fe 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java @@ -1,15 +1,18 @@ package dev.langchain4j.googleaigemini.spring; +import dev.langchain4j.data.embedding.Embedding; import dev.langchain4j.data.message.AiMessage; import dev.langchain4j.model.StreamingResponseHandler; import dev.langchain4j.model.chat.ChatLanguageModel; import dev.langchain4j.model.chat.StreamingChatLanguageModel; +import dev.langchain4j.model.embedding.EmbeddingModel; +import dev.langchain4j.model.googleai.GoogleAiEmbeddingModel; import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel; import dev.langchain4j.model.googleai.GoogleAiGeminiStreamingChatModel; import dev.langchain4j.model.output.Response; + import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import java.util.concurrent.CompletableFuture; @@ -19,7 +22,7 @@ class AutoConfigIT { - private static final String API_KEY =System.getenv("GOOGLE_AI_GEMINI_API_KEY"); +private static final String API_KEY =System.getenv("GOOGLE_AI_GEMINI_API_KEY"); ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(AutoConfig.class)); @@ -87,4 +90,25 @@ public void onError(Throwable throwable) { assertThat(response.content().text()).contains("Delhi"); }); } + + @Test + void provide_embedding_model(){ + contextRunner.withPropertyValues( + "langchain4j.google-ai-gemini.apiKey=" + API_KEY, + "langchain4j.google-ai-gemini.embeddingModel.enabled=true", + "langchain4j.google-ai-gemini.embeddingModel.titleMetadataKey=title-key", + "langchain4j.google-ai-gemini.embeddingModel.modelName=text-embedding-004", + "langchain4j.google-ai-gemini.embeddingModel.logRequestsAndResponses=true", + "langchain4j.google-ai-gemini.embeddingModel.maxRetries=3", + "langchain4j.google-ai-gemini.embeddingModel.outputDimensionality=512", + "langchain4j.google-ai-gemini.embeddingModel.taskType=CLASSIFICATION", + "langchain4j.google-ai-gemini.embeddingModel.timeout=PT30S" + ).run(context -> { + EmbeddingModel embeddingModel = context.getBean(GoogleAiEmbeddingModel.class); + assertThat(embeddingModel).isInstanceOf(EmbeddingModel.class); + assertThat(context.getBean(GoogleAiEmbeddingModel.class)).isSameAs(embeddingModel); + Response response= embeddingModel.embed("Hi, I live in India"); + assertThat(response.content().dimension()==512); + }); + } } \ No newline at end of file From b0a8f9a8371a360b7fe77601ca43703a9678161c Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Tue, 26 Nov 2024 23:47:31 +0530 Subject: [PATCH 31/41] Resolving issues in spring boot starter for google ai gemini --- .../pom.xml | 2 +- .../googleaigemini/spring/AutoConfig.java | 14 ++--- .../spring/ChatModelProperties.java | 51 +++++++++++++------ .../spring/EmbeddingModelProperties.java | 4 +- .../googleaigemini/spring/Properties.java | 22 -------- ...ot.autoconfigure.AutoConfiguration.imports | 0 .../googleaigemini/spring/AutoConfigIT.java | 32 ++++++------ 7 files changed, 61 insertions(+), 64 deletions(-) rename langchain4j-google-ai-gemini-spring-boot-starter/src/main/{java/resources/META-INF/spring => resources/META-INF.spring}/org.springframework.boot.autoconfigure.AutoConfiguration.imports (100%) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml index 39a168e3..e607e184 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml +++ b/langchain4j-google-ai-gemini-spring-boot-starter/pom.xml @@ -12,7 +12,7 @@ langchain4j-google-ai-gemini-spring-boot-starter - LangChain4j Spring Boot starter for Google Gemini AI + LangChain4j Spring Boot starter for Google AI Gemini jar diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java index f3bed6b5..184cf843 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java @@ -32,13 +32,13 @@ ChatLanguageModel googleAiGeminiChatModel(Properties properties) { .topK(chatModelProperties.getTopK()) .maxOutputTokens(chatModelProperties.getMaxOutputTokens()) .responseFormat(chatModelProperties.getResponseFormat()) - .logRequestsAndResponses(chatModelProperties.isLogRequestsAndResponses()) + .logRequestsAndResponses(chatModelProperties.getLogRequestsAndResponses()) .safetySettings( - Map.of(properties.getSafetySetting().getGeminiHarmCategory(), - properties.getSafetySetting().getGeminiHarmBlockThreshold())) + Map.of(chatModelProperties.getSafetySetting().getGeminiHarmCategory(), + chatModelProperties.getSafetySetting().getGeminiHarmBlockThreshold())) .toolConfig( - properties.getFunctionCallingConfig().getGeminiMode(), - properties.getFunctionCallingConfig().getAllowedFunctionNames().toArray(new String[0])) + chatModelProperties.getFunctionCallingConfig().getGeminiMode(), + chatModelProperties.getFunctionCallingConfig().getAllowedFunctionNames().toArray(new String[0])) .build(); } @@ -53,7 +53,7 @@ StreamingChatLanguageModel googleAiGeminiStreamingChatModel(Properties propertie .topP(chatModelProperties.getTopP()) .topK(chatModelProperties.getTopK()) .responseFormat(chatModelProperties.getResponseFormat()) - .logRequestsAndResponses(chatModelProperties.isLogRequestsAndResponses()) + .logRequestsAndResponses(chatModelProperties.getLogRequestsAndResponses()) .build(); } @@ -69,7 +69,7 @@ EmbeddingModel googleAiGeminiEmbeddingModel(Properties properties) { .outputDimensionality(embeddingModelProperties.getOutputDimensionality()) .taskType(embeddingModelProperties.getTaskType()) .timeout(embeddingModelProperties.getTimeout()) - .titleMetadataKey("title") + .titleMetadataKey(embeddingModelProperties.getTitleMetadataKey()) .build(); } } \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java index d6f27fdc..46e4007f 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java @@ -1,21 +1,26 @@ package dev.langchain4j.googleaigemini.spring; import dev.langchain4j.model.chat.request.ResponseFormat; + import org.springframework.boot.context.properties.NestedConfigurationProperty; import java.time.Duration; public class ChatModelProperties { private String modelName; - private double temperature; - private double topP; + private Double temperature; + private Double topP; private Integer topK; private Integer maxOutputTokens; private ResponseFormat responseFormat; - private boolean logRequestsAndResponses; + private Boolean logRequestsAndResponses; private Integer maxRetries; private Duration timeout; + private GeminiSafetySetting safetySetting; + + private GeminiFunctionCallingConfig functionCallingConfig; + public Integer getMaxRetries() { return maxRetries; } @@ -32,14 +37,6 @@ public void setTimeout(Duration timeout) { this.timeout = timeout; } - public boolean isLogRequestsAndResponses() { - return logRequestsAndResponses; - } - - public void setLogRequestsAndResponses(boolean logRequestsAndResponses) { - this.logRequestsAndResponses = logRequestsAndResponses; - } - public ResponseFormat getResponseFormat() { return responseFormat; } @@ -56,19 +53,19 @@ public void setModelName(String modelName) { this.modelName = modelName; } - public double getTemperature() { + public Double getTemperature() { return temperature; } - public void setTemperature(double temperature) { + public void setTemperature(Double temperature) { this.temperature = temperature; } - public double getTopP() { + public Double getTopP() { return topP; } - public void setTopP(double topP) { + public void setTopP(Double topP) { this.topP = topP; } @@ -80,6 +77,30 @@ public void setTopK(Integer topK) { this.topK = topK; } + public Boolean getLogRequestsAndResponses() { + return logRequestsAndResponses; + } + + public void setLogRequestsAndResponses(Boolean logRequestsAndResponses) { + this.logRequestsAndResponses = logRequestsAndResponses; + } + + public GeminiSafetySetting getSafetySetting() { + return safetySetting; + } + + public void setSafetySetting(GeminiSafetySetting safetySetting) { + this.safetySetting = safetySetting; + } + + public GeminiFunctionCallingConfig getFunctionCallingConfig() { + return functionCallingConfig; + } + + public void setFunctionCallingConfig(GeminiFunctionCallingConfig functionCallingConfig) { + this.functionCallingConfig = functionCallingConfig; + } + public Integer getMaxOutputTokens() { return maxOutputTokens; } diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java index 362328c2..5fb0d2ef 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java @@ -9,7 +9,7 @@ public class EmbeddingModelProperties { private String titleMetadataKey; private String modelName; - private boolean logRequestsAndResponses; + private Boolean logRequestsAndResponses; private Integer maxRetries; private Integer outputDimensionality; private TaskType taskType; @@ -31,7 +31,7 @@ public void setModelName(String modelName) { this.modelName = modelName; } - public boolean isLogRequestsAndResponses() { + public Boolean isLogRequestsAndResponses() { return logRequestsAndResponses; } diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java index 795c1c28..cfe37536 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java @@ -14,15 +14,9 @@ public class Properties { @NestedConfigurationProperty private ChatModelProperties streamingChatModel; - @NestedConfigurationProperty - private GeminiSafetySetting safetySetting; - @NestedConfigurationProperty private EmbeddingModelProperties embeddingModel; - @NestedConfigurationProperty - private GeminiFunctionCallingConfig functionCallingConfig; - private String apiKey; public EmbeddingModelProperties getEmbeddingModel() { @@ -33,22 +27,6 @@ public void setEmbeddingModel(EmbeddingModelProperties embeddingModel) { this.embeddingModel = embeddingModel; } - public GeminiSafetySetting getSafetySetting() { - return safetySetting; - } - - public void setSafetySetting(GeminiSafetySetting safetySetting) { - this.safetySetting = safetySetting; - } - - public GeminiFunctionCallingConfig getFunctionCallingConfig() { - return functionCallingConfig; - } - - public void setFunctionCallingConfig(GeminiFunctionCallingConfig functionCallingConfig) { - this.functionCallingConfig = functionCallingConfig; - } - public String getApiKey() { return apiKey; } diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/META-INF.spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports similarity index 100% rename from langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports rename to langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/META-INF.spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java index 79b9c1fe..cb6cf200 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java @@ -30,18 +30,17 @@ class AutoConfigIT { @Test void provide_chat_model() { contextRunner.withPropertyValues( - "langchain4j.google-ai-gemini.apiKey=" + API_KEY, - "langchain4j.google-ai-gemini.enabled=true", + "langchain4j.google-ai-gemini.api-key=" + API_KEY, "langchain4j.google-ai-gemini.chatModel.enabled=true", - "langchain4j.google-ai-gemini.chatModel.modelName=gemini-1.0-pro ", + "langchain4j.google-ai-gemini.chatModel.model-name=gemini-1.5-flash", "langchain4j.google-ai-gemini.chatModel.temperature=0.7", "langchain4j.google-ai-gemini.chatModel.topP=0.9", "langchain4j.google-ai-gemini.chatModel.topK=40", - "langchain4j.google-ai-gemini.chatModel.maxOutputTokens=100", - "langchain4j.google-ai-gemini.safetySetting.geminiHarmCategory=HARM_CATEGORY_SEXUALLY_EXPLICIT", - "langchain4j.google-ai-gemini.safetySetting.geminiHarmBlockThreshold=HARM_BLOCK_THRESHOLD_UNSPECIFIED", - "langchain4j.google-ai-gemini.functionCallingConfig.geminiMode=ANY", - "langchain4j.google-ai-gemini.functionCallingConfig.allowedFunctionNames=allowCodeExecution,includeCodeExecutionOutput" + "langchain4j.google-ai-gemini.chatModel.max-output-tokens=300", + "langchain4j.google-ai-gemini.chatModel.safetySetting.gemini-harm-category=HARM_CATEGORY_SEXUALLY_EXPLICIT", + "langchain4j.google-ai-gemini.chatModel.safetySetting.gemini-harm-block-threshold=HARM_BLOCK_THRESHOLD_UNSPECIFIED", + "langchain4j.google-ai-gemini.chatModel.functionCallingConfig.gemini-mode=ANY", + "langchain4j.google-ai-gemini.chatModel.functionCallingConfig.allowed-function-names=allowCodeExecution,includeCodeExecutionOutput" ) .run(context -> { ChatLanguageModel chatLanguageModel = context.getBean(ChatLanguageModel.class); @@ -49,7 +48,7 @@ void provide_chat_model() { assertThat(context.getBean(GoogleAiGeminiChatModel.class)).isSameAs(chatLanguageModel); String response = chatLanguageModel.generate("What is the capital of India"); assertThat(response).contains("Delhi"); - String newResponse = chatLanguageModel.generate("Calculate the Fibonacci of 22 and return the result as an integer value along with the code. "); + String newResponse = chatLanguageModel.generate("Calculate the Fibonacci of 22 and give me the result as an integer value along with the code. "); assertThat(newResponse).contains("17711"); }); } @@ -58,7 +57,6 @@ void provide_chat_model() { void provide_streaming_chat_model() { contextRunner.withPropertyValues( "langchain4j.google-ai-gemini.apiKey=" + API_KEY, - "langchain4j.google-ai-gemini.enabled=true", "langchain4j.google-ai-gemini.streamingChatModel.enabled=true", "langchain4j.google-ai-gemini.streamingChatModel.modelName=gemini-1.5-flash", "langchain4j.google-ai-gemini.streamingChatModel.temperature=0.7", @@ -96,15 +94,15 @@ void provide_embedding_model(){ contextRunner.withPropertyValues( "langchain4j.google-ai-gemini.apiKey=" + API_KEY, "langchain4j.google-ai-gemini.embeddingModel.enabled=true", - "langchain4j.google-ai-gemini.embeddingModel.titleMetadataKey=title-key", - "langchain4j.google-ai-gemini.embeddingModel.modelName=text-embedding-004", - "langchain4j.google-ai-gemini.embeddingModel.logRequestsAndResponses=true", - "langchain4j.google-ai-gemini.embeddingModel.maxRetries=3", - "langchain4j.google-ai-gemini.embeddingModel.outputDimensionality=512", - "langchain4j.google-ai-gemini.embeddingModel.taskType=CLASSIFICATION", + "langchain4j.google-ai-gemini.embeddingModel.title-metadata-key=title-key", + "langchain4j.google-ai-gemini.embeddingModel.model-name=text-embedding-004", + "langchain4j.google-ai-gemini.embeddingModel.log-requests-and-responses=true", + "langchain4j.google-ai-gemini.embeddingModel.max-retries=3", + "langchain4j.google-ai-gemini.embeddingModel.output-dimensionality=512", + "langchain4j.google-ai-gemini.embeddingModel.task-type=CLASSIFICATION", "langchain4j.google-ai-gemini.embeddingModel.timeout=PT30S" ).run(context -> { - EmbeddingModel embeddingModel = context.getBean(GoogleAiEmbeddingModel.class); + EmbeddingModel embeddingModel = context.getBean(EmbeddingModel.class); assertThat(embeddingModel).isInstanceOf(EmbeddingModel.class); assertThat(context.getBean(GoogleAiEmbeddingModel.class)).isSameAs(embeddingModel); Response response= embeddingModel.embed("Hi, I live in India"); From 89694f5fa5b2ca954122a97721a9bb28d49975ce Mon Sep 17 00:00:00 2001 From: Suhas Koheda Date: Tue, 26 Nov 2024 23:49:28 +0530 Subject: [PATCH 32/41] Update main.yaml for google ai gemini key --- .github/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 9154299d..31bfe446 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -34,9 +34,9 @@ jobs: AZURE_SEARCH_ENDPOINT: ${{ secrets.AZURE_SEARCH_ENDPOINT }} AZURE_SEARCH_KEY: ${{ secrets.AZURE_SEARCH_KEY }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GOOGLE_AI_GEMINI_API_KEY: ${{secrets.GOOGLE_AI_GEMINI_API_KEY}} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }} - GOOGLE_AI_GEMINI_API_KEY: ${{secrets.GOOGLE_AI_GEMINI_API_KEY}} OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} From 907424b655103f27aac7b654603e90fe64a3a56a Mon Sep 17 00:00:00 2001 From: Suhas Koheda Date: Tue, 26 Nov 2024 23:50:08 +0530 Subject: [PATCH 33/41] Update release.yaml for google ai gemini key --- .github/workflows/release.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index fe0df526..3d4ebf53 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -33,9 +33,9 @@ jobs: AZURE_SEARCH_ENDPOINT: ${{ secrets.AZURE_SEARCH_ENDPOINT }} AZURE_SEARCH_KEY: ${{ secrets.AZURE_SEARCH_KEY }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GOOGLE_AI_GEMINI_API_KEY: ${{secrets.GOOGLE_AI_GEMINI_API_KEY}} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }} - GOOGLE_AI_GEMINI_API_KEY: ${{secrets.GOOGLE_AI_GEMINI_API_KEY}} GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} From 615916575fa0fd434cb594e8517583f53b74ae4f Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Tue, 26 Nov 2024 23:55:15 +0530 Subject: [PATCH 34/41] refactoring resources --- ...g.springframework.boot.autoconfigure.AutoConfiguration.imports | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/{META-INF.spring => META-INF/spring}/org.springframework.boot.autoconfigure.AutoConfiguration.imports (100%) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/META-INF.spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports similarity index 100% rename from langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/META-INF.spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports rename to langchain4j-google-ai-gemini-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports From 7554b251be894b4f1104558488b289aa3ad3c65d Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Sun, 1 Dec 2024 01:51:44 +0530 Subject: [PATCH 35/41] Changing the safteySetting and toolConfig and writing test --- .../dev/langchain4j/googleaigemini/spring/AutoConfig.java | 6 ++++++ .../dev/langchain4j/googleaigemini/spring/AutoConfigIT.java | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java index 184cf843..93624613 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java @@ -54,6 +54,12 @@ StreamingChatLanguageModel googleAiGeminiStreamingChatModel(Properties propertie .topK(chatModelProperties.getTopK()) .responseFormat(chatModelProperties.getResponseFormat()) .logRequestsAndResponses(chatModelProperties.getLogRequestsAndResponses()) + .safetySettings( + Map.of(chatModelProperties.getSafetySetting().getGeminiHarmCategory(), + chatModelProperties.getSafetySetting().getGeminiHarmBlockThreshold())) + .toolConfig( + chatModelProperties.getFunctionCallingConfig().getGeminiMode(), + chatModelProperties.getFunctionCallingConfig().getAllowedFunctionNames().toArray(new String[0])) .build(); } diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java index cb6cf200..e017d038 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java @@ -62,7 +62,11 @@ void provide_streaming_chat_model() { "langchain4j.google-ai-gemini.streamingChatModel.temperature=0.7", "langchain4j.google-ai-gemini.streamingChatModel.topP=0.9", "langchain4j.google-ai-gemini.streamingChatModel.topK=40", - "langchain4j.google-ai-gemini.streamingChatModel.maxOutputTokens=100" + "langchain4j.google-ai-gemini.streamingChatModel.maxOutputTokens=100", + "langchain4j.google-ai-gemini.chatModel.safetySetting.gemini-harm-category=HARM_CATEGORY_SEXUALLY_EXPLICIT", + "langchain4j.google-ai-gemini.chatModel.safetySetting.gemini-harm-block-threshold=HARM_BLOCK_THRESHOLD_UNSPECIFIED", + "langchain4j.google-ai-gemini.chatModel.functionCallingConfig.gemini-mode=ANY", + "langchain4j.google-ai-gemini.chatModel.functionCallingConfig.allowed-function-names=allowCodeExecution,includeCodeExecutionOutput" ) .run(context -> { StreamingChatLanguageModel streamingChatLanguageModel = context.getBean(StreamingChatLanguageModel.class); From 69660a445760c0d2887f25afcabc724812caba7f Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Tue, 3 Dec 2024 22:38:13 +0530 Subject: [PATCH 36/41] Resolving silly mistake --- .../googleaigemini/spring/AutoConfigIT.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java index e017d038..b4ebd559 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java @@ -22,7 +22,7 @@ class AutoConfigIT { -private static final String API_KEY =System.getenv("GOOGLE_AI_GEMINI_API_KEY"); +private static final String API_KEY = System.getenv("GOOGLE_AI_GEMINI_API_KEY"); ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(AutoConfig.class)); @@ -63,10 +63,10 @@ void provide_streaming_chat_model() { "langchain4j.google-ai-gemini.streamingChatModel.topP=0.9", "langchain4j.google-ai-gemini.streamingChatModel.topK=40", "langchain4j.google-ai-gemini.streamingChatModel.maxOutputTokens=100", - "langchain4j.google-ai-gemini.chatModel.safetySetting.gemini-harm-category=HARM_CATEGORY_SEXUALLY_EXPLICIT", - "langchain4j.google-ai-gemini.chatModel.safetySetting.gemini-harm-block-threshold=HARM_BLOCK_THRESHOLD_UNSPECIFIED", - "langchain4j.google-ai-gemini.chatModel.functionCallingConfig.gemini-mode=ANY", - "langchain4j.google-ai-gemini.chatModel.functionCallingConfig.allowed-function-names=allowCodeExecution,includeCodeExecutionOutput" + "langchain4j.google-ai-gemini.streamingChatModel.safetySetting.gemini-harm-category=HARM_CATEGORY_SEXUALLY_EXPLICIT", + "langchain4j.google-ai-gemini.streamingChatModel.safetySetting.gemini-harm-block-threshold=HARM_BLOCK_THRESHOLD_UNSPECIFIED", + "langchain4j.google-ai-gemini.streamingChatModel.functionCallingConfig.gemini-mode=ANY", + "langchain4j.google-ai-gemini.streamingChatModel.functionCallingConfig.allowed-function-names=allowCodeExecution,includeCodeExecutionOutput" ) .run(context -> { StreamingChatLanguageModel streamingChatLanguageModel = context.getBean(StreamingChatLanguageModel.class); From 3107cb9503bfaaa4c0e6af4a644f6f8a676f18b8 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Sun, 8 Dec 2024 01:05:46 +0530 Subject: [PATCH 37/41] Resolving issues --- .../googleaigemini/spring/AutoConfig.java | 65 ++++----- .../spring/ChatModelProperties.java | 133 +++--------------- .../spring/EmbeddingModelProperties.java | 73 +--------- .../spring/GeminiSafetySetting.java | 23 +-- 4 files changed, 61 insertions(+), 233 deletions(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java index 93624613..d70c7306 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java @@ -1,8 +1,5 @@ package dev.langchain4j.googleaigemini.spring; -import dev.langchain4j.model.chat.ChatLanguageModel; -import dev.langchain4j.model.chat.StreamingChatLanguageModel; -import dev.langchain4j.model.embedding.EmbeddingModel; import dev.langchain4j.model.googleai.GoogleAiEmbeddingModel; import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel; import dev.langchain4j.model.googleai.GoogleAiGeminiStreamingChatModel; @@ -22,60 +19,60 @@ public class AutoConfig { @Bean @ConditionalOnProperty(name = PREFIX + ".chatModel.enabled", havingValue = "true") - ChatLanguageModel googleAiGeminiChatModel(Properties properties) { + GoogleAiGeminiChatModel googleAiGeminiChatModel(Properties properties) { ChatModelProperties chatModelProperties = properties.getChatModel(); return GoogleAiGeminiChatModel.builder() .apiKey(properties.getApiKey()) - .modelName(chatModelProperties.getModelName()) - .temperature(chatModelProperties.getTemperature()) - .topP(chatModelProperties.getTopP()) - .topK(chatModelProperties.getTopK()) - .maxOutputTokens(chatModelProperties.getMaxOutputTokens()) - .responseFormat(chatModelProperties.getResponseFormat()) - .logRequestsAndResponses(chatModelProperties.getLogRequestsAndResponses()) + .modelName(chatModelProperties.modelName()) + .temperature(chatModelProperties.temperature()) + .topP(chatModelProperties.topP()) + .topK(chatModelProperties.topK()) + .maxOutputTokens(chatModelProperties.maxOutputTokens()) + .responseFormat(chatModelProperties.responseFormat()) + .logRequestsAndResponses(chatModelProperties.logRequestsAndResponses()) .safetySettings( - Map.of(chatModelProperties.getSafetySetting().getGeminiHarmCategory(), - chatModelProperties.getSafetySetting().getGeminiHarmBlockThreshold())) + Map.of(chatModelProperties.safetySetting().geminiHarmCategory(), + chatModelProperties.safetySetting().geminiHarmBlockThreshold())) .toolConfig( - chatModelProperties.getFunctionCallingConfig().getGeminiMode(), - chatModelProperties.getFunctionCallingConfig().getAllowedFunctionNames().toArray(new String[0])) + chatModelProperties.functionCallingConfig().getGeminiMode(), + chatModelProperties.functionCallingConfig().getAllowedFunctionNames().toArray(new String[0])) .build(); } @Bean @ConditionalOnProperty(name = PREFIX + ".streamingChatModel.enabled", havingValue = "true") - StreamingChatLanguageModel googleAiGeminiStreamingChatModel(Properties properties) { + GoogleAiGeminiStreamingChatModel googleAiGeminiStreamingChatModel(Properties properties) { ChatModelProperties chatModelProperties = properties.getStreamingChatModel(); return GoogleAiGeminiStreamingChatModel.builder() .apiKey(properties.getApiKey()) - .modelName(chatModelProperties.getModelName()) - .temperature(chatModelProperties.getTemperature()) - .topP(chatModelProperties.getTopP()) - .topK(chatModelProperties.getTopK()) - .responseFormat(chatModelProperties.getResponseFormat()) - .logRequestsAndResponses(chatModelProperties.getLogRequestsAndResponses()) + .modelName(chatModelProperties.modelName()) + .temperature(chatModelProperties.temperature()) + .topP(chatModelProperties.topP()) + .topK(chatModelProperties.topK()) + .responseFormat(chatModelProperties.responseFormat()) + .logRequestsAndResponses(chatModelProperties.logRequestsAndResponses()) .safetySettings( - Map.of(chatModelProperties.getSafetySetting().getGeminiHarmCategory(), - chatModelProperties.getSafetySetting().getGeminiHarmBlockThreshold())) + Map.of(chatModelProperties.safetySetting().geminiHarmCategory(), + chatModelProperties.safetySetting().geminiHarmBlockThreshold())) .toolConfig( - chatModelProperties.getFunctionCallingConfig().getGeminiMode(), - chatModelProperties.getFunctionCallingConfig().getAllowedFunctionNames().toArray(new String[0])) + chatModelProperties.functionCallingConfig().getGeminiMode(), + chatModelProperties.functionCallingConfig().getAllowedFunctionNames().toArray(new String[0])) .build(); } @Bean @ConditionalOnProperty(name = PREFIX + ".embeddingModel.enabled", havingValue = "true") - EmbeddingModel googleAiGeminiEmbeddingModel(Properties properties) { + GoogleAiEmbeddingModel googleAiGeminiEmbeddingModel(Properties properties) { EmbeddingModelProperties embeddingModelProperties = properties.getEmbeddingModel(); return GoogleAiEmbeddingModel.builder() .apiKey(properties.getApiKey()) - .modelName(embeddingModelProperties.getModelName()) - .logRequestsAndResponses(embeddingModelProperties.isLogRequestsAndResponses()) - .maxRetries(embeddingModelProperties.getMaxRetries()) - .outputDimensionality(embeddingModelProperties.getOutputDimensionality()) - .taskType(embeddingModelProperties.getTaskType()) - .timeout(embeddingModelProperties.getTimeout()) - .titleMetadataKey(embeddingModelProperties.getTitleMetadataKey()) + .modelName(embeddingModelProperties.modelName()) + .logRequestsAndResponses(embeddingModelProperties.logRequestsAndResponses()) + .maxRetries(embeddingModelProperties.maxRetries()) + .outputDimensionality(embeddingModelProperties.outputDimensionality()) + .taskType(embeddingModelProperties.taskType()) + .timeout(embeddingModelProperties.timeout()) + .titleMetadataKey(embeddingModelProperties.titleMetadataKey()) .build(); } } \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java index 46e4007f..90e7d024 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java @@ -1,112 +1,21 @@ - package dev.langchain4j.googleaigemini.spring; - - import dev.langchain4j.model.chat.request.ResponseFormat; - import org.springframework.boot.context.properties.NestedConfigurationProperty; - - import java.time.Duration; - - public class ChatModelProperties { - - private String modelName; - private Double temperature; - private Double topP; - private Integer topK; - private Integer maxOutputTokens; - private ResponseFormat responseFormat; - private Boolean logRequestsAndResponses; - private Integer maxRetries; - private Duration timeout; - - private GeminiSafetySetting safetySetting; - - private GeminiFunctionCallingConfig functionCallingConfig; - - public Integer getMaxRetries() { - return maxRetries; - } - - public void setMaxRetries(Integer maxRetries) { - this.maxRetries = maxRetries; - } - - public Duration getTimeout() { - return timeout; - } - - public void setTimeout(Duration timeout) { - this.timeout = timeout; - } - - public ResponseFormat getResponseFormat() { - return responseFormat; - } - - public void setResponseFormat(ResponseFormat responseFormat) { - this.responseFormat = responseFormat; - } - - 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 getTopK() { - return topK; - } - - public void setTopK(Integer topK) { - this.topK = topK; - } - - public Boolean getLogRequestsAndResponses() { - return logRequestsAndResponses; - } - - public void setLogRequestsAndResponses(Boolean logRequestsAndResponses) { - this.logRequestsAndResponses = logRequestsAndResponses; - } - - public GeminiSafetySetting getSafetySetting() { - return safetySetting; - } - - public void setSafetySetting(GeminiSafetySetting safetySetting) { - this.safetySetting = safetySetting; - } - - public GeminiFunctionCallingConfig getFunctionCallingConfig() { - return functionCallingConfig; - } - - public void setFunctionCallingConfig(GeminiFunctionCallingConfig functionCallingConfig) { - this.functionCallingConfig = functionCallingConfig; - } - - public Integer getMaxOutputTokens() { - return maxOutputTokens; - } - - public void setMaxOutputTokens(Integer maxOutputTokens) { - this.maxOutputTokens = maxOutputTokens; - } - - } +package dev.langchain4j.googleaigemini.spring; + +import dev.langchain4j.model.chat.request.ResponseFormat; + +import java.time.Duration; + + +public record ChatModelProperties( + String modelName, + Double temperature, + Double topP, + Integer topK, + Integer maxOutputTokens, + ResponseFormat responseFormat, + Boolean logRequestsAndResponses, + Integer maxRetries, + Duration timeout, + GeminiSafetySetting safetySetting, + GeminiFunctionCallingConfig functionCallingConfig +) { +} \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java index 5fb0d2ef..58fdd1bc 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java @@ -5,69 +5,10 @@ import java.time.Duration; -public class EmbeddingModelProperties { - - private String titleMetadataKey; - private String modelName; - private Boolean logRequestsAndResponses; - private Integer maxRetries; - private Integer outputDimensionality; - private TaskType taskType; - private Duration timeout; - - public String getTitleMetadataKey() { - return titleMetadataKey; - } - - public void setTitleMetadataKey(String titleMetadataKey) { - this.titleMetadataKey = titleMetadataKey; - } - - public String getModelName() { - return modelName; - } - - public void setModelName(String modelName) { - this.modelName = modelName; - } - - public Boolean isLogRequestsAndResponses() { - return logRequestsAndResponses; - } - - public void setLogRequestsAndResponses(boolean logRequestsAndResponses) { - this.logRequestsAndResponses = logRequestsAndResponses; - } - - public Integer getMaxRetries() { - return maxRetries; - } - - public void setMaxRetries(Integer maxRetries) { - this.maxRetries = maxRetries; - } - - public Integer getOutputDimensionality() { - return outputDimensionality; - } - - public void setOutputDimensionality(Integer outputDimensionality) { - this.outputDimensionality = outputDimensionality; - } - - public GoogleAiEmbeddingModel.TaskType getTaskType() { - return taskType; - } - - public void setTaskType(GoogleAiEmbeddingModel.TaskType taskType) { - this.taskType = taskType; - } - - public Duration getTimeout() { - return timeout; - } - - public void setTimeout(Duration timeout) { - this.timeout = timeout; - } -} +public record EmbeddingModelProperties( String titleMetadataKey, + String modelName, + Boolean logRequestsAndResponses, + Integer maxRetries, + Integer outputDimensionality, + TaskType taskType, + Duration timeout){} \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java index 407bb3fa..e138bd2a 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java @@ -3,24 +3,5 @@ import dev.langchain4j.model.googleai.GeminiHarmBlockThreshold; import dev.langchain4j.model.googleai.GeminiHarmCategory; -public class GeminiSafetySetting { - - private GeminiHarmCategory geminiHarmCategory; - private GeminiHarmBlockThreshold geminiHarmBlockThreshold; - - public GeminiHarmCategory getGeminiHarmCategory() { - return geminiHarmCategory; - } - - public void setGeminiHarmCategory(GeminiHarmCategory geminiHarmCategory) { - this.geminiHarmCategory = geminiHarmCategory; - } - - public GeminiHarmBlockThreshold getGeminiHarmBlockThreshold() { - return geminiHarmBlockThreshold; - } - - public void setGeminiHarmBlockThreshold(GeminiHarmBlockThreshold geminiHarmBlockThreshold) { - this.geminiHarmBlockThreshold = geminiHarmBlockThreshold; - } -} +public record GeminiSafetySetting(GeminiHarmCategory geminiHarmCategory, + GeminiHarmBlockThreshold geminiHarmBlockThreshold){} \ No newline at end of file From 56d7546b78293f6ebde69835983e52a89ce6799b Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Mon, 9 Dec 2024 21:47:37 +0530 Subject: [PATCH 38/41] Changing bean properties --- .../langchain4j/googleaigemini/spring/AutoConfig.java | 10 +++++----- .../googleaigemini/spring/ChatModelProperties.java | 3 ++- .../spring/EmbeddingModelProperties.java | 3 ++- .../langchain4j/googleaigemini/spring/Properties.java | 10 ---------- .../googleaigemini/spring/AutoConfigIT.java | 10 ++++------ 5 files changed, 13 insertions(+), 23 deletions(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java index d70c7306..35052c03 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java @@ -18,11 +18,11 @@ public class AutoConfig { @Bean - @ConditionalOnProperty(name = PREFIX + ".chatModel.enabled", havingValue = "true") + @ConditionalOnProperty(name = {PREFIX + ".chatModel.api-key", PREFIX + ".chatModel.model-name"}) GoogleAiGeminiChatModel googleAiGeminiChatModel(Properties properties) { ChatModelProperties chatModelProperties = properties.getChatModel(); return GoogleAiGeminiChatModel.builder() - .apiKey(properties.getApiKey()) + .apiKey(chatModelProperties.apiKey()) .modelName(chatModelProperties.modelName()) .temperature(chatModelProperties.temperature()) .topP(chatModelProperties.topP()) @@ -40,11 +40,11 @@ GoogleAiGeminiChatModel googleAiGeminiChatModel(Properties properties) { } @Bean - @ConditionalOnProperty(name = PREFIX + ".streamingChatModel.enabled", havingValue = "true") + @ConditionalOnProperty(name = {PREFIX + ".streamingChatModel.api-key", PREFIX + ".streamingChatModel.model-name"}) GoogleAiGeminiStreamingChatModel googleAiGeminiStreamingChatModel(Properties properties) { ChatModelProperties chatModelProperties = properties.getStreamingChatModel(); return GoogleAiGeminiStreamingChatModel.builder() - .apiKey(properties.getApiKey()) + .apiKey(chatModelProperties.apiKey()) .modelName(chatModelProperties.modelName()) .temperature(chatModelProperties.temperature()) .topP(chatModelProperties.topP()) @@ -65,7 +65,7 @@ GoogleAiGeminiStreamingChatModel googleAiGeminiStreamingChatModel(Properties pro GoogleAiEmbeddingModel googleAiGeminiEmbeddingModel(Properties properties) { EmbeddingModelProperties embeddingModelProperties = properties.getEmbeddingModel(); return GoogleAiEmbeddingModel.builder() - .apiKey(properties.getApiKey()) + .apiKey(embeddingModelProperties.apiKey()) .modelName(embeddingModelProperties.modelName()) .logRequestsAndResponses(embeddingModelProperties.logRequestsAndResponses()) .maxRetries(embeddingModelProperties.maxRetries()) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java index 90e7d024..8504b33a 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java @@ -16,6 +16,7 @@ public record ChatModelProperties( Integer maxRetries, Duration timeout, GeminiSafetySetting safetySetting, - GeminiFunctionCallingConfig functionCallingConfig + GeminiFunctionCallingConfig functionCallingConfig, + String apiKey ) { } \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java index 58fdd1bc..c8af2275 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java @@ -11,4 +11,5 @@ public record EmbeddingModelProperties( String titleMetadataKey, Integer maxRetries, Integer outputDimensionality, TaskType taskType, - Duration timeout){} \ No newline at end of file + Duration timeout, + String apiKey){} \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java index cfe37536..72eb6b1a 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/Properties.java @@ -17,8 +17,6 @@ public class Properties { @NestedConfigurationProperty private EmbeddingModelProperties embeddingModel; - private String apiKey; - public EmbeddingModelProperties getEmbeddingModel() { return embeddingModel; } @@ -27,14 +25,6 @@ public void setEmbeddingModel(EmbeddingModelProperties embeddingModel) { this.embeddingModel = embeddingModel; } - public String getApiKey() { - return apiKey; - } - - public void setApiKey(String apiKey) { - this.apiKey = apiKey; - } - public ChatModelProperties getStreamingChatModel() { return streamingChatModel; } diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java index b4ebd559..b0227465 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java @@ -30,8 +30,7 @@ class AutoConfigIT { @Test void provide_chat_model() { contextRunner.withPropertyValues( - "langchain4j.google-ai-gemini.api-key=" + API_KEY, - "langchain4j.google-ai-gemini.chatModel.enabled=true", + "langchain4j.google-ai-gemini.chatModel.api-key=" + API_KEY, "langchain4j.google-ai-gemini.chatModel.model-name=gemini-1.5-flash", "langchain4j.google-ai-gemini.chatModel.temperature=0.7", "langchain4j.google-ai-gemini.chatModel.topP=0.9", @@ -56,9 +55,8 @@ void provide_chat_model() { @Test void provide_streaming_chat_model() { contextRunner.withPropertyValues( - "langchain4j.google-ai-gemini.apiKey=" + API_KEY, - "langchain4j.google-ai-gemini.streamingChatModel.enabled=true", - "langchain4j.google-ai-gemini.streamingChatModel.modelName=gemini-1.5-flash", + "langchain4j.google-ai-gemini.streamingChatModel.api-key=" + API_KEY, + "langchain4j.google-ai-gemini.streamingChatModel.model-name=gemini-1.5-flash", "langchain4j.google-ai-gemini.streamingChatModel.temperature=0.7", "langchain4j.google-ai-gemini.streamingChatModel.topP=0.9", "langchain4j.google-ai-gemini.streamingChatModel.topK=40", @@ -96,7 +94,7 @@ public void onError(Throwable throwable) { @Test void provide_embedding_model(){ contextRunner.withPropertyValues( - "langchain4j.google-ai-gemini.apiKey=" + API_KEY, + "langchain4j.google-ai-gemini.embeddingModel.apiKey=" + API_KEY, "langchain4j.google-ai-gemini.embeddingModel.enabled=true", "langchain4j.google-ai-gemini.embeddingModel.title-metadata-key=title-key", "langchain4j.google-ai-gemini.embeddingModel.model-name=text-embedding-004", From ed5afe002c12e97f8ef6b5d1349bb80f641af2e6 Mon Sep 17 00:00:00 2001 From: LangChain4j Date: Mon, 16 Dec 2024 15:00:53 +0100 Subject: [PATCH 39/41] cosmetics --- .../googleaigemini/spring/AutoConfig.java | 46 ++++++++++------ .../spring/ChatModelProperties.java | 5 +- .../spring/EmbeddingModelProperties.java | 20 +++---- .../spring/GeminiFunctionCallingConfig.java | 24 ++------- .../spring/GeminiSafetySetting.java | 7 ++- .../googleaigemini/spring/AutoConfigIT.java | 54 ++++++------------- 6 files changed, 69 insertions(+), 87 deletions(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java index 35052c03..cff4695b 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java @@ -3,7 +3,6 @@ import dev.langchain4j.model.googleai.GoogleAiEmbeddingModel; import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel; import dev.langchain4j.model.googleai.GoogleAiGeminiStreamingChatModel; - import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -18,7 +17,10 @@ public class AutoConfig { @Bean - @ConditionalOnProperty(name = {PREFIX + ".chatModel.api-key", PREFIX + ".chatModel.model-name"}) + @ConditionalOnProperty({ + PREFIX + ".chat-model.api-key", + PREFIX + ".chat-model.model-name" + }) GoogleAiGeminiChatModel googleAiGeminiChatModel(Properties properties) { ChatModelProperties chatModelProperties = properties.getChatModel(); return GoogleAiGeminiChatModel.builder() @@ -30,17 +32,24 @@ GoogleAiGeminiChatModel googleAiGeminiChatModel(Properties properties) { .maxOutputTokens(chatModelProperties.maxOutputTokens()) .responseFormat(chatModelProperties.responseFormat()) .logRequestsAndResponses(chatModelProperties.logRequestsAndResponses()) - .safetySettings( - Map.of(chatModelProperties.safetySetting().geminiHarmCategory(), - chatModelProperties.safetySetting().geminiHarmBlockThreshold())) + .safetySettings(Map.of( + // TODO NPE? + chatModelProperties.safetySetting().geminiHarmCategory(), + chatModelProperties.safetySetting().geminiHarmBlockThreshold() + )) .toolConfig( - chatModelProperties.functionCallingConfig().getGeminiMode(), - chatModelProperties.functionCallingConfig().getAllowedFunctionNames().toArray(new String[0])) + // TODO NPE? + chatModelProperties.functionCallingConfig().geminiMode(), + chatModelProperties.functionCallingConfig().allowedFunctionNames().toArray(new String[0]) + ) .build(); } @Bean - @ConditionalOnProperty(name = {PREFIX + ".streamingChatModel.api-key", PREFIX + ".streamingChatModel.model-name"}) + @ConditionalOnProperty({ + PREFIX + ".streaming-chat-model.api-key", + PREFIX + ".streaming-chat-model.model-name" + }) GoogleAiGeminiStreamingChatModel googleAiGeminiStreamingChatModel(Properties properties) { ChatModelProperties chatModelProperties = properties.getStreamingChatModel(); return GoogleAiGeminiStreamingChatModel.builder() @@ -51,18 +60,25 @@ GoogleAiGeminiStreamingChatModel googleAiGeminiStreamingChatModel(Properties pro .topK(chatModelProperties.topK()) .responseFormat(chatModelProperties.responseFormat()) .logRequestsAndResponses(chatModelProperties.logRequestsAndResponses()) - .safetySettings( - Map.of(chatModelProperties.safetySetting().geminiHarmCategory(), - chatModelProperties.safetySetting().geminiHarmBlockThreshold())) + .safetySettings(Map.of( + // TODO NPE? + chatModelProperties.safetySetting().geminiHarmCategory(), + chatModelProperties.safetySetting().geminiHarmBlockThreshold() + )) .toolConfig( - chatModelProperties.functionCallingConfig().getGeminiMode(), - chatModelProperties.functionCallingConfig().getAllowedFunctionNames().toArray(new String[0])) + // TODO NPE? + chatModelProperties.functionCallingConfig().geminiMode(), + chatModelProperties.functionCallingConfig().allowedFunctionNames().toArray(new String[0]) + ) .build(); } @Bean - @ConditionalOnProperty(name = PREFIX + ".embeddingModel.enabled", havingValue = "true") - GoogleAiEmbeddingModel googleAiGeminiEmbeddingModel(Properties properties) { + @ConditionalOnProperty({ + PREFIX + ".embedding-model.api-key", + PREFIX + ".embedding-model.model-name" + }) + GoogleAiEmbeddingModel googleAiEmbeddingModel(Properties properties) { EmbeddingModelProperties embeddingModelProperties = properties.getEmbeddingModel(); return GoogleAiEmbeddingModel.builder() .apiKey(embeddingModelProperties.apiKey()) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java index 8504b33a..b9748123 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java @@ -4,8 +4,8 @@ import java.time.Duration; - public record ChatModelProperties( + String apiKey, String modelName, Double temperature, Double topP, @@ -16,7 +16,6 @@ public record ChatModelProperties( Integer maxRetries, Duration timeout, GeminiSafetySetting safetySetting, - GeminiFunctionCallingConfig functionCallingConfig, - String apiKey + GeminiFunctionCallingConfig functionCallingConfig ) { } \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java index c8af2275..b6d9ff29 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/EmbeddingModelProperties.java @@ -1,15 +1,17 @@ package dev.langchain4j.googleaigemini.spring; -import dev.langchain4j.model.googleai.GoogleAiEmbeddingModel; import dev.langchain4j.model.googleai.GoogleAiEmbeddingModel.TaskType; import java.time.Duration; -public record EmbeddingModelProperties( String titleMetadataKey, - String modelName, - Boolean logRequestsAndResponses, - Integer maxRetries, - Integer outputDimensionality, - TaskType taskType, - Duration timeout, - String apiKey){} \ No newline at end of file +public record EmbeddingModelProperties( + String apiKey, + String modelName, + String titleMetadataKey, + Boolean logRequestsAndResponses, + Integer maxRetries, + Integer outputDimensionality, + TaskType taskType, + Duration timeout +) { +} \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiFunctionCallingConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiFunctionCallingConfig.java index 700bbc68..a5bc5090 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiFunctionCallingConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiFunctionCallingConfig.java @@ -4,24 +4,8 @@ import java.util.List; -public class GeminiFunctionCallingConfig { - - private GeminiMode geminiMode; - private List allowedFunctionNames; - - public GeminiMode getGeminiMode() { - return geminiMode; - } - - public void setGeminiMode(GeminiMode geminiMode) { - this.geminiMode = geminiMode; - } - - public List getAllowedFunctionNames() { - return allowedFunctionNames; - } - - public void setAllowedFunctionNames(List allowedFunctionNames) { - this.allowedFunctionNames = allowedFunctionNames; - } +public record GeminiFunctionCallingConfig( + GeminiMode geminiMode, + List allowedFunctionNames +) { } diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java index e138bd2a..89e1cd26 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java @@ -3,5 +3,8 @@ import dev.langchain4j.model.googleai.GeminiHarmBlockThreshold; import dev.langchain4j.model.googleai.GeminiHarmCategory; -public record GeminiSafetySetting(GeminiHarmCategory geminiHarmCategory, - GeminiHarmBlockThreshold geminiHarmBlockThreshold){} \ No newline at end of file +public record GeminiSafetySetting( + GeminiHarmCategory geminiHarmCategory, + GeminiHarmBlockThreshold geminiHarmBlockThreshold +) { +} \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java index b0227465..23e60704 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/test/java/dev/langchain4j/googleaigemini/spring/AutoConfigIT.java @@ -10,7 +10,6 @@ import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel; import dev.langchain4j.model.googleai.GoogleAiGeminiStreamingChatModel; import dev.langchain4j.model.output.Response; - import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; @@ -22,7 +21,7 @@ class AutoConfigIT { -private static final String API_KEY = System.getenv("GOOGLE_AI_GEMINI_API_KEY"); + private static final String API_KEY = System.getenv("GOOGLE_AI_GEMINI_API_KEY"); ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(AutoConfig.class)); @@ -30,23 +29,16 @@ class AutoConfigIT { @Test void provide_chat_model() { contextRunner.withPropertyValues( - "langchain4j.google-ai-gemini.chatModel.api-key=" + API_KEY, - "langchain4j.google-ai-gemini.chatModel.model-name=gemini-1.5-flash", - "langchain4j.google-ai-gemini.chatModel.temperature=0.7", - "langchain4j.google-ai-gemini.chatModel.topP=0.9", - "langchain4j.google-ai-gemini.chatModel.topK=40", - "langchain4j.google-ai-gemini.chatModel.max-output-tokens=300", - "langchain4j.google-ai-gemini.chatModel.safetySetting.gemini-harm-category=HARM_CATEGORY_SEXUALLY_EXPLICIT", - "langchain4j.google-ai-gemini.chatModel.safetySetting.gemini-harm-block-threshold=HARM_BLOCK_THRESHOLD_UNSPECIFIED", - "langchain4j.google-ai-gemini.chatModel.functionCallingConfig.gemini-mode=ANY", - "langchain4j.google-ai-gemini.chatModel.functionCallingConfig.allowed-function-names=allowCodeExecution,includeCodeExecutionOutput" + "langchain4j.google-ai-gemini.chat-model.api-key=" + API_KEY, + "langchain4j.google-ai-gemini.chat-model.model-name=gemini-1.5-flash" ) .run(context -> { ChatLanguageModel chatLanguageModel = context.getBean(ChatLanguageModel.class); - assertThat(chatLanguageModel).isInstanceOf(ChatLanguageModel.class); assertThat(context.getBean(GoogleAiGeminiChatModel.class)).isSameAs(chatLanguageModel); + String response = chatLanguageModel.generate("What is the capital of India"); assertThat(response).contains("Delhi"); + String newResponse = chatLanguageModel.generate("Calculate the Fibonacci of 22 and give me the result as an integer value along with the code. "); assertThat(newResponse).contains("17711"); }); @@ -55,25 +47,18 @@ void provide_chat_model() { @Test void provide_streaming_chat_model() { contextRunner.withPropertyValues( - "langchain4j.google-ai-gemini.streamingChatModel.api-key=" + API_KEY, - "langchain4j.google-ai-gemini.streamingChatModel.model-name=gemini-1.5-flash", - "langchain4j.google-ai-gemini.streamingChatModel.temperature=0.7", - "langchain4j.google-ai-gemini.streamingChatModel.topP=0.9", - "langchain4j.google-ai-gemini.streamingChatModel.topK=40", - "langchain4j.google-ai-gemini.streamingChatModel.maxOutputTokens=100", - "langchain4j.google-ai-gemini.streamingChatModel.safetySetting.gemini-harm-category=HARM_CATEGORY_SEXUALLY_EXPLICIT", - "langchain4j.google-ai-gemini.streamingChatModel.safetySetting.gemini-harm-block-threshold=HARM_BLOCK_THRESHOLD_UNSPECIFIED", - "langchain4j.google-ai-gemini.streamingChatModel.functionCallingConfig.gemini-mode=ANY", - "langchain4j.google-ai-gemini.streamingChatModel.functionCallingConfig.allowed-function-names=allowCodeExecution,includeCodeExecutionOutput" + "langchain4j.google-ai-gemini.streaming-chat-model.api-key=" + API_KEY, + "langchain4j.google-ai-gemini.streaming-chat-model.model-name=gemini-1.5-flash" ) .run(context -> { StreamingChatLanguageModel streamingChatLanguageModel = context.getBean(StreamingChatLanguageModel.class); - assertThat(streamingChatLanguageModel).isInstanceOf(StreamingChatLanguageModel.class); assertThat(context.getBean(GoogleAiGeminiStreamingChatModel.class)).isSameAs(streamingChatLanguageModel); + CompletableFuture> future = new CompletableFuture<>(); streamingChatLanguageModel.generate("What is the capital of India", new StreamingResponseHandler<>() { @Override - public void onNext(String s) {} + public void onNext(String s) { + } @Override public void onComplete(Response response) { @@ -92,23 +77,16 @@ public void onError(Throwable throwable) { } @Test - void provide_embedding_model(){ + void provide_embedding_model() { contextRunner.withPropertyValues( - "langchain4j.google-ai-gemini.embeddingModel.apiKey=" + API_KEY, - "langchain4j.google-ai-gemini.embeddingModel.enabled=true", - "langchain4j.google-ai-gemini.embeddingModel.title-metadata-key=title-key", - "langchain4j.google-ai-gemini.embeddingModel.model-name=text-embedding-004", - "langchain4j.google-ai-gemini.embeddingModel.log-requests-and-responses=true", - "langchain4j.google-ai-gemini.embeddingModel.max-retries=3", - "langchain4j.google-ai-gemini.embeddingModel.output-dimensionality=512", - "langchain4j.google-ai-gemini.embeddingModel.task-type=CLASSIFICATION", - "langchain4j.google-ai-gemini.embeddingModel.timeout=PT30S" + "langchain4j.google-ai-gemini.embedding-model.apiKey=" + API_KEY, + "langchain4j.google-ai-gemini.embedding-model.model-name=text-embedding-004" ).run(context -> { EmbeddingModel embeddingModel = context.getBean(EmbeddingModel.class); - assertThat(embeddingModel).isInstanceOf(EmbeddingModel.class); assertThat(context.getBean(GoogleAiEmbeddingModel.class)).isSameAs(embeddingModel); - Response response= embeddingModel.embed("Hi, I live in India"); - assertThat(response.content().dimension()==512); + + Response response = embeddingModel.embed("Hi, I live in India"); + assertThat(response.content().dimension()).isEqualTo(768); }); } } \ No newline at end of file From 9c167d1471f66a9bfbe4750f1beb17f482a40478 Mon Sep 17 00:00:00 2001 From: suhas-koheda Date: Sat, 21 Dec 2024 18:50:48 +0530 Subject: [PATCH 40/41] Adding NPE check for Safety Setting and Tool Config --- .../googleaigemini/spring/AutoConfig.java | 56 ++++++++++++------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java index cff4695b..cb406aa7 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java @@ -1,16 +1,17 @@ package dev.langchain4j.googleaigemini.spring; -import dev.langchain4j.model.googleai.GoogleAiEmbeddingModel; -import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel; -import dev.langchain4j.model.googleai.GoogleAiGeminiStreamingChatModel; +import dev.langchain4j.model.googleai.*; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; +import java.util.HashMap; import java.util.Map; import static dev.langchain4j.googleaigemini.spring.Properties.PREFIX; +import static dev.langchain4j.model.googleai.GeminiHarmBlockThreshold.HARM_BLOCK_THRESHOLD_UNSPECIFIED; +import static dev.langchain4j.model.googleai.GeminiHarmCategory.HARM_CATEGORY_CIVIC_INTEGRITY; @AutoConfiguration @EnableConfigurationProperties(Properties.class) @@ -32,15 +33,10 @@ GoogleAiGeminiChatModel googleAiGeminiChatModel(Properties properties) { .maxOutputTokens(chatModelProperties.maxOutputTokens()) .responseFormat(chatModelProperties.responseFormat()) .logRequestsAndResponses(chatModelProperties.logRequestsAndResponses()) - .safetySettings(Map.of( - // TODO NPE? - chatModelProperties.safetySetting().geminiHarmCategory(), - chatModelProperties.safetySetting().geminiHarmBlockThreshold() - )) + .safetySettings(checkSafetySettingForNull(chatModelProperties.safetySetting())) .toolConfig( - // TODO NPE? - chatModelProperties.functionCallingConfig().geminiMode(), - chatModelProperties.functionCallingConfig().allowedFunctionNames().toArray(new String[0]) + checkGeminiModeForNull(chatModelProperties.functionCallingConfig()), + checkFunctionNamesForNull(chatModelProperties.functionCallingConfig()) ) .build(); } @@ -60,15 +56,10 @@ GoogleAiGeminiStreamingChatModel googleAiGeminiStreamingChatModel(Properties pro .topK(chatModelProperties.topK()) .responseFormat(chatModelProperties.responseFormat()) .logRequestsAndResponses(chatModelProperties.logRequestsAndResponses()) - .safetySettings(Map.of( - // TODO NPE? - chatModelProperties.safetySetting().geminiHarmCategory(), - chatModelProperties.safetySetting().geminiHarmBlockThreshold() - )) + .safetySettings(checkSafetySettingForNull(chatModelProperties.safetySetting())) .toolConfig( - // TODO NPE? - chatModelProperties.functionCallingConfig().geminiMode(), - chatModelProperties.functionCallingConfig().allowedFunctionNames().toArray(new String[0]) + checkGeminiModeForNull(chatModelProperties.functionCallingConfig()), + checkFunctionNamesForNull(chatModelProperties.functionCallingConfig()) ) .build(); } @@ -91,4 +82,31 @@ GoogleAiEmbeddingModel googleAiEmbeddingModel(Properties properties) { .titleMetadataKey(embeddingModelProperties.titleMetadataKey()) .build(); } + + private String[] checkFunctionNamesForNull(GeminiFunctionCallingConfig geminiFunctionCallingConfig) { + if(geminiFunctionCallingConfig==null){ + return new String[0]; + } + return geminiFunctionCallingConfig.allowedFunctionNames().toArray(new String[0]); + } + + private GeminiMode checkGeminiModeForNull(GeminiFunctionCallingConfig geminiFunctionCallingConfig) { + if(geminiFunctionCallingConfig==null){ + return GeminiMode.NONE; + } + return geminiFunctionCallingConfig.geminiMode(); + } + + private Map checkSafetySettingForNull(GeminiSafetySetting safetySetting) { + if(safetySetting==null){ + Map defaultMap= new HashMap<>(); + defaultMap.put(HARM_CATEGORY_CIVIC_INTEGRITY,HARM_BLOCK_THRESHOLD_UNSPECIFIED); + return defaultMap; + } + return Map.of( + safetySetting.geminiHarmCategory(), + safetySetting.geminiHarmBlockThreshold() + ); + } + } \ No newline at end of file From 816652c2844eb09cdb35d8e83a886c1caeb2ee94 Mon Sep 17 00:00:00 2001 From: suhas-koheda Date: Sat, 4 Jan 2025 00:57:15 +0530 Subject: [PATCH 41/41] Changing safetySettings to map of harm category and harm threshold --- .../langchain4j/googleaigemini/spring/AutoConfig.java | 11 +++++------ .../googleaigemini/spring/ChatModelProperties.java | 5 ++++- .../googleaigemini/spring/GeminiSafetySetting.java | 10 ---------- 3 files changed, 9 insertions(+), 17 deletions(-) delete mode 100644 langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java index cb406aa7..bd9aef4f 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/AutoConfig.java @@ -97,16 +97,15 @@ private GeminiMode checkGeminiModeForNull(GeminiFunctionCallingConfig geminiFunc return geminiFunctionCallingConfig.geminiMode(); } - private Map checkSafetySettingForNull(GeminiSafetySetting safetySetting) { - if(safetySetting==null){ + private Map checkSafetySettingForNull(Map safetySetting) { + if(safetySetting==null || safetySetting.isEmpty()){ Map defaultMap= new HashMap<>(); defaultMap.put(HARM_CATEGORY_CIVIC_INTEGRITY,HARM_BLOCK_THRESHOLD_UNSPECIFIED); return defaultMap; } - return Map.of( - safetySetting.geminiHarmCategory(), - safetySetting.geminiHarmBlockThreshold() - ); + Map userMap= new HashMap<>(); + safetySetting.keySet().forEach(key-> userMap.put(key,safetySetting.get(key))); + return userMap; } } \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java index b9748123..dc204b71 100644 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java +++ b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/ChatModelProperties.java @@ -1,8 +1,11 @@ package dev.langchain4j.googleaigemini.spring; import dev.langchain4j.model.chat.request.ResponseFormat; +import dev.langchain4j.model.googleai.GeminiHarmBlockThreshold; +import dev.langchain4j.model.googleai.GeminiHarmCategory; import java.time.Duration; +import java.util.Map; public record ChatModelProperties( String apiKey, @@ -15,7 +18,7 @@ public record ChatModelProperties( Boolean logRequestsAndResponses, Integer maxRetries, Duration timeout, - GeminiSafetySetting safetySetting, + Map safetySetting, GeminiFunctionCallingConfig functionCallingConfig ) { } \ No newline at end of file diff --git a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java b/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java deleted file mode 100644 index 89e1cd26..00000000 --- a/langchain4j-google-ai-gemini-spring-boot-starter/src/main/java/dev/langchain4j/googleaigemini/spring/GeminiSafetySetting.java +++ /dev/null @@ -1,10 +0,0 @@ -package dev.langchain4j.googleaigemini.spring; - -import dev.langchain4j.model.googleai.GeminiHarmBlockThreshold; -import dev.langchain4j.model.googleai.GeminiHarmCategory; - -public record GeminiSafetySetting( - GeminiHarmCategory geminiHarmCategory, - GeminiHarmBlockThreshold geminiHarmBlockThreshold -) { -} \ No newline at end of file