├── .github └── workflows │ └── build.yml ├── .gitignore ├── COPYING ├── Makefile ├── README.md ├── buildSrc ├── build.gradle └── src │ └── main │ └── groovy │ ├── io.nextflow.groovy-application-conventions.gradle │ ├── io.nextflow.groovy-common-conventions.gradle │ └── io.nextflow.groovy-library-conventions.gradle ├── changelog.txt ├── examples ├── example1.nf ├── example2.nf ├── example3.nf ├── example4.nf └── example5.nf ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── launch.sh ├── plugins ├── build.gradle └── nf-gpt │ ├── build.gradle │ └── src │ ├── main │ └── nextflow │ │ ├── gpt │ │ ├── GptPlugin.groovy │ │ ├── client │ │ │ ├── GptChatCompletionRequest.groovy │ │ │ ├── GptChatCompletionResponse.groovy │ │ │ └── GptClient.groovy │ │ ├── config │ │ │ ├── GptConfig.groovy │ │ │ └── GptRetryOpts.groovy │ │ └── prompt │ │ │ ├── GptHelper.groovy │ │ │ ├── GptPromptExtension.groovy │ │ │ └── GptPromptModel.groovy │ │ └── processor │ │ └── tip │ │ └── GptTaskTipProvider.groovy │ ├── resources │ └── META-INF │ │ ├── MANIFEST.MF │ │ └── extensions.idx │ └── test │ └── nextflow │ └── gpt │ ├── client │ ├── GptChatCompletionRequestTest.groovy │ └── GptClientTest.groovy │ ├── config │ └── GptConfigTest.groovy │ └── prompt │ ├── GptHelperTest.groovy │ ├── GptPromptExtensionTest.groovy │ └── GptPromptModelTest.groovy ├── prompt-eng.nf └── settings.gradle /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: nf-gpt CI 2 | on: 3 | push: 4 | branches: 5 | - '*' 6 | tags-ignore: 7 | - '*' 8 | pull_request: 9 | branches: 10 | - '*' 11 | jobs: 12 | build: 13 | name: Build nf-gpt 14 | if: "!contains(github.event.head_commit.message, '[ci skip]')" 15 | runs-on: ubuntu-latest 16 | timeout-minutes: 10 17 | strategy: 18 | fail-fast: false 19 | matrix: 20 | java_version: [11, 21] 21 | 22 | steps: 23 | - name: Environment 24 | run: env | sort 25 | 26 | - name: Checkout 27 | uses: actions/checkout@v4 28 | with: 29 | fetch-depth: 1 30 | submodules: true 31 | 32 | - name: Setup Java ${{ matrix.java_version }} 33 | uses: actions/setup-java@v4 34 | with: 35 | java-version: ${{matrix.java_version}} 36 | architecture: x64 37 | distribution: 'temurin' 38 | 39 | - name: Compile 40 | run: ./gradlew assemble 41 | 42 | - name: Tests 43 | run: ./gradlew check 44 | env: 45 | GRADLE_OPTS: '-Dorg.gradle.daemon=false' 46 | OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} 47 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore Gradle project-specific cache directory 2 | .gradle 3 | .idea 4 | .nextflow* 5 | 6 | # Ignore Gradle build output directory 7 | build 8 | work 9 | out 10 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | config ?= compileClasspath 3 | 4 | ifdef module 5 | mm = :${module}: 6 | else 7 | mm = 8 | endif 9 | 10 | clean: 11 | rm -rf .nextflow* 12 | rm -rf work 13 | rm -rf build 14 | rm -rf plugins/*/build 15 | ./gradlew clean 16 | 17 | compile: 18 | ./gradlew :nextflow:exportClasspath compileGroovy 19 | @echo "DONE `date`" 20 | 21 | 22 | check: 23 | ./gradlew check 24 | 25 | 26 | # 27 | # Show dependencies try `make deps config=runtime`, `make deps config=google` 28 | # 29 | deps: 30 | ./gradlew -q ${mm}dependencies --configuration ${config} 31 | 32 | deps-all: 33 | ./gradlew -q dependencyInsight --configuration ${config} --dependency ${module} 34 | 35 | # 36 | # Refresh SNAPSHOTs dependencies 37 | # 38 | refresh: 39 | ./gradlew --refresh-dependencies 40 | 41 | # 42 | # Run all tests or selected ones 43 | # 44 | test: 45 | ifndef class 46 | ./gradlew ${mm}test 47 | else 48 | ./gradlew ${mm}test --tests ${class} 49 | endif 50 | 51 | assemble: 52 | ./gradlew assemble 53 | 54 | # 55 | # generate build zips under build/plugins 56 | # you can install the plugin copying manually these files to $HOME/.nextflow/plugins 57 | # 58 | buildPlugins: 59 | ./gradlew copyPluginZip 60 | 61 | # 62 | # Upload JAR artifacts to Maven Central 63 | # 64 | upload: 65 | ./gradlew upload 66 | 67 | 68 | upload-plugins: 69 | ./gradlew plugins:upload 70 | 71 | publish-index: 72 | ./gradlew plugins:publishIndex 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nf-gpt plugin 2 | 3 | nf-gpt is an experimental plugin to integrate GPT prompts into Nextflow scripts. It allows submitting 4 | prompts via OpenAI API and collect the response in form of structured data for downstream analysis. 5 | 6 | ## Get started 7 | 8 | 1. Configure the [OpenAI API](https://platform.openai.com/api-keys) key in your environment by using the following variable: 9 | 10 | ```bash 11 | export OPENAI_API_KEY= 12 | ``` 13 | 14 | 2. Add the following snippet at the beginning of your script: 15 | 16 | ```nextflow 17 | include { gptPromptForText } from 'plugin/nf-gpt' 18 | ``` 19 | 20 | 3. Use the `gptPromptForText` operator to perform a ChatGPT prompt and get the response. 21 | 22 | ``` 23 | include { gptPromptForText } from 'plugin/nf-gpt' 24 | 25 | println gptPromptForText('Tell me a joke') 26 | 27 | ``` 28 | 29 | 4. run using Nextflow as usual 30 | 31 | ``` 32 | nextflow run 33 | ``` 34 | 35 | 5. See the folder [examples] for more examples. 36 | 37 | 38 | ## Reference 39 | 40 | ### Function `gptPromptForText` 41 | 42 | The `gptPromptForText` function carries out a Gpt chat prompt and return the corresponding message as response as a string. Example: 43 | 44 | 45 | ```nextflow 46 | println gptPromptForText('Tell me a joke') 47 | ``` 48 | 49 | 50 | When the option `numOfChoices` is specified the response is a list of strings. 51 | 52 | ```nextflow 53 | def response = gptPromptForText('Tell me a joke', numOfChoices: 3) 54 | for( String it : response ) 55 | println it 56 | ``` 57 | 58 | Available options: 59 | 60 | 61 | | name | description | 62 | |-----------------|----------------------------------------------------------------------------------------------------------------------------------| 63 | | `logitBias` | Accepts an object mapping each token (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100 | 64 | | `model` | The AI model to be used (default: `gpt-3.5-turbo`) | 65 | | `maxTokens` | The maximum number of tokens that can be generated in the chat completion | 66 | | `numOfChoices` | How many chat completion choices to generate for each input message (default: 1) | 67 | | `temperature` | What sampling temperature to use, between 0 and 2 (default: `0.7`) | 68 | 69 | 70 | ### Function `gptPromptForData` 71 | 72 | The `gptPromptForData` function carries out a GPT chat prompt and returns the response as a list of 73 | objects having the schema specified. For example: 74 | 75 | ```nextflow 76 | 77 | def query = ''' 78 | Extract information about a person from In 1968, amidst the fading echoes of Independence Day, 79 | a child named John arrived under the calm evening sky. This newborn, bearing the surname Doe, 80 | marked the start of a new journey. 81 | ''' 82 | 83 | def response = gptPromptForData(query, schema: [firstName: 'string', lastName: 'string', birthDate: 'date (YYYY-MM-DD)']) 84 | 85 | println "First name: ${response[0].firstName}" 86 | println "Last name: ${response[0].lastName}" 87 | println "Birth date: ${response[0].birthDate}" 88 | ``` 89 | 90 | 91 | The following options are available: 92 | 93 | 94 | | name | description | 95 | |-----------------|-------------| 96 | | `model` | The AI model to be used (default: `gpt-3.5-turbo`) | 97 | | `maxTokens` | The maximum number of tokens that can be generated in the chat completion | 98 | | `schema` | The expected strcuture for the result object represented as map object in which represent the attribute name and the value the attribute type | 99 | | `temperature` | What sampling temperature to use, between 0 and 2 (default: `0.7`) | 100 | 101 | 102 | ### Configuration file 103 | 104 | The following config options can be specified in the `nextflow.config` file: 105 | 106 | 107 | | name | description | 108 | |-----------------|-------------| 109 | | `gpt.apiKey` | Your OpenAI API key. If missing it uses the `OPENAI_API_KEY` env variable | 110 | | `gpt.endpoint` | The OpenAI endpoint (defualt: `https://api.openai.com`) | 111 | | `gpt.model` | The AI model to be used (default: `gpt-3.5-turbo`) | 112 | | `gpt.maxTokens` | The maximum number of tokens that can be generated in the chat completion | 113 | | `gpt.temperature` | What sampling temperature to use, between 0 and 2 (default: `0.7`) | 114 | 115 | 116 | ## Development 117 | 118 | To build and test the plugin during development, configure a local Nextflow build with the following steps: 119 | 120 | 1. Clone the Nextflow repository in your computer into a sibling directory: 121 | ```bash 122 | git clone --depth 1 https://github.com/nextflow-io/nextflow ../nextflow 123 | ``` 124 | 125 | 2. Configure the plugin build to use the local Nextflow code: 126 | ```bash 127 | echo "includeBuild('../nextflow')" >> settings.gradle 128 | ``` 129 | 130 | (Make sure to not add it more than once!) 131 | 132 | 3. Compile the plugin alongside the Nextflow code: 133 | ```bash 134 | make assemble 135 | ``` 136 | 137 | 4. Run Nextflow with the plugin, using `./launch.sh` as a drop-in replacement for the `nextflow` command, and adding the option `-plugins nf-gpt` to load the plugin: 138 | ```bash 139 | ./launch.sh run nextflow-io/hello -plugins nf-gpt 140 | ``` 141 | 142 | ### Testing without Nextflow build 143 | 144 | The plugin can be tested without using a local Nextflow build using the following steps: 145 | 146 | 1. Build the plugin: `make buildPlugins` 147 | 2. Copy `build/plugins/` to `$HOME/.nextflow/plugins` 148 | 3. Create a pipeline that uses your plugin and run it: `nextflow run ./my-pipeline-script.nf` 149 | 150 | ### Package, upload, and publish 151 | 152 | The project should be hosted in a GitHub repository whose name matches the name of the plugin, that is the name of the directory in the `plugins` folder (e.g. `nf-gpt`). 153 | 154 | Follow these steps to package, upload and publish the plugin: 155 | 156 | 1. Create a file named `gradle.properties` in the project root containing the following attributes (this file should not be committed to Git): 157 | 158 | * `github_organization`: the GitHub organisation where the plugin repository is hosted. 159 | * `github_username`: The GitHub username granting access to the plugin repository. 160 | * `github_access_token`: The GitHub access token required to upload and commit changes to the plugin repository. 161 | * `github_commit_email`: The email address associated with your GitHub account. 162 | 163 | 2. Use the following command to package and create a release for your plugin on GitHub: 164 | ```bash 165 | ./gradlew :plugins:nf-gpt:upload 166 | ``` 167 | 168 | 3. Create a pull request against [nextflow-io/plugins](https://github.com/nextflow-io/plugins/blob/main/plugins.json) to make the plugin accessible to Nextflow. 169 | -------------------------------------------------------------------------------- /buildSrc/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This file was generated by the Gradle 'init' task. 3 | */ 4 | 5 | plugins { 6 | // Support convention plugins written in Groovy. Convention plugins are build scripts in 'src/main' that automatically become available as plugins in the main build. 7 | id 'groovy-gradle-plugin' 8 | } 9 | 10 | repositories { 11 | // Use the plugin portal to apply community plugins in convention plugins. 12 | gradlePluginPortal() 13 | } 14 | 15 | -------------------------------------------------------------------------------- /buildSrc/src/main/groovy/io.nextflow.groovy-application-conventions.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This file was generated by the Gradle 'init' task. 3 | */ 4 | 5 | plugins { 6 | // Apply the common convention plugin for shared build configuration between library and application projects. 7 | id 'io.nextflow.groovy-common-conventions' 8 | 9 | // Apply the application plugin to add support for building a CLI application in Java. 10 | id 'application' 11 | } 12 | -------------------------------------------------------------------------------- /buildSrc/src/main/groovy/io.nextflow.groovy-common-conventions.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This file was generated by the Gradle 'init' task. 3 | */ 4 | 5 | plugins { 6 | // Apply the groovy Plugin to add support for Groovy. 7 | id 'groovy' 8 | } 9 | 10 | repositories { 11 | // Use Maven Central for resolving dependencies. 12 | mavenCentral() 13 | } 14 | 15 | java { 16 | toolchain { 17 | languageVersion = JavaLanguageVersion.of(19) 18 | } 19 | } 20 | 21 | compileJava { 22 | options.release.set(11) 23 | } 24 | 25 | tasks.withType(GroovyCompile) { 26 | sourceCompatibility = '11' 27 | targetCompatibility = '11' 28 | } 29 | 30 | tasks.withType(Test) { 31 | jvmArgs ([ 32 | '--add-opens=java.base/java.lang=ALL-UNNAMED', 33 | '--add-opens=java.base/java.io=ALL-UNNAMED', 34 | '--add-opens=java.base/java.nio=ALL-UNNAMED', 35 | '--add-opens=java.base/java.nio.file.spi=ALL-UNNAMED', 36 | '--add-opens=java.base/java.net=ALL-UNNAMED', 37 | '--add-opens=java.base/java.util=ALL-UNNAMED', 38 | '--add-opens=java.base/java.util.concurrent.locks=ALL-UNNAMED', 39 | '--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED', 40 | '--add-opens=java.base/sun.nio.ch=ALL-UNNAMED', 41 | '--add-opens=java.base/sun.nio.fs=ALL-UNNAMED', 42 | '--add-opens=java.base/sun.net.www.protocol.http=ALL-UNNAMED', 43 | '--add-opens=java.base/sun.net.www.protocol.https=ALL-UNNAMED', 44 | '--add-opens=java.base/sun.net.www.protocol.ftp=ALL-UNNAMED', 45 | '--add-opens=java.base/sun.net.www.protocol.file=ALL-UNNAMED', 46 | '--add-opens=java.base/jdk.internal.misc=ALL-UNNAMED', 47 | '--add-opens=java.base/jdk.internal.vm=ALL-UNNAMED', 48 | ]) 49 | } 50 | -------------------------------------------------------------------------------- /buildSrc/src/main/groovy/io.nextflow.groovy-library-conventions.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This file was generated by the Gradle 'init' task. 3 | */ 4 | 5 | plugins { 6 | // Apply the common convention plugin for shared build configuration between library and application projects. 7 | id 'io.nextflow.groovy-common-conventions' 8 | // Apply the java-library plugin for API and implementation separation. 9 | id 'java-library' 10 | } 11 | 12 | -------------------------------------------------------------------------------- /changelog.txt: -------------------------------------------------------------------------------- 1 | nf-gpt changelog 2 | =============== 3 | 0.4.0 - 15 Apr 2024 4 | - Add Gpt task tip provider [4b0037a7] 5 | - Bump nextflow 24.03.0-edge requirement [2df6734] 6 | - Fix tests [1e129222] 7 | 8 | 0.3.0 - 5 Apr 2024 9 | - Add `gptPromptForText` and `gptPromptForText` funtions 10 | - Fix temperature & maxToken params [6b3584bf] 11 | - Bump actions @4 [7750bb34] 12 | 13 | 0.2.0 - 15 Mar 2024 14 | 15 | 0.1.0 - 17 Mar 2024 16 | - Initial version 17 | -------------------------------------------------------------------------------- /examples/example1.nf: -------------------------------------------------------------------------------- 1 | include { gptPromptForText } from 'plugin/nf-gpt' 2 | 3 | /* 4 | * This example show how to use the `gptPromptForText` function in the map operator 5 | */ 6 | 7 | channel 8 | .of('Tell me joke') 9 | .map { gptPromptForText(it) } 10 | .view() 11 | -------------------------------------------------------------------------------- /examples/example2.nf: -------------------------------------------------------------------------------- 1 | include { gptPromptForText } from 'plugin/nf-gpt' 2 | 3 | /* 4 | * This example show how to use the `gptPromptForText` function in a process 5 | */ 6 | 7 | process prompt { 8 | input: 9 | val query 10 | output: 11 | val response 12 | exec: 13 | response = gptPromptForText(query) 14 | } 15 | 16 | workflow { 17 | prompt('Tell me a joke') | view 18 | } 19 | -------------------------------------------------------------------------------- /examples/example3.nf: -------------------------------------------------------------------------------- 1 | include { gptPromptForData } from 'plugin/nf-gpt' 2 | 3 | /** 4 | * This example show how to perform a GPT prompt and map the response to a structured object 5 | */ 6 | 7 | def text = ''' 8 | Extract information about a person from In 1968, amidst the fading echoes of Independence Day, 9 | a child named John arrived under the calm evening sky. This newborn, bearing the surname Doe, 10 | marked the start of a new journey. 11 | ''' 12 | 13 | channel 14 | .of(text) 15 | .flatMap { gptPromptForData(it, schema: [firstName: 'string', lastName: 'string', birthDate: 'date (YYYY-MM-DD)']) } 16 | .view() 17 | -------------------------------------------------------------------------------- /examples/example4.nf: -------------------------------------------------------------------------------- 1 | include { gptPromptForData } from 'plugin/nf-gpt' 2 | 3 | /** 4 | * This example show how to perform a GPT prompt and map the response to a structured object 5 | */ 6 | 7 | 8 | def query = ''' 9 | Who won most gold medals in swimming and Athletics categories during Barcelona 1992 and London 2012 olympic games?" 10 | ''' 11 | 12 | def RECORD = [athlete: 'string', numberOfMedals: 'number', location:'string', sport:'string'] 13 | 14 | channel .of(query) 15 | .flatMap { gptPromptForData(it, schema:RECORD, temperature: 2d) } 16 | .view() 17 | -------------------------------------------------------------------------------- /examples/example5.nf: -------------------------------------------------------------------------------- 1 | include { gptPromptForData } from 'plugin/nf-gpt' 2 | 3 | /** 4 | * This example show how to perform multiple GPT prompts using combine and flatMap operators 5 | */ 6 | 7 | 8 | channel 9 | .fromList(['Barcelona, 1992', 'London, 2012']) 10 | .combine(['Swimming', 'Athletics']) 11 | .flatMap { edition, sport -> 12 | gptPromptForData( 13 | "Who won most gold medals in $sport category during $edition olympic games?", 14 | schema: [athlete: 'string', numberOfMedals: 'number', location: 'string', sport: 'string']) 15 | } 16 | .view() 17 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nextflow-io/nf-gpt/4c1f5feafc1c70a33de1347766ab0953f08fe30a/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /launch.sh: -------------------------------------------------------------------------------- 1 | export NXF_PLUGINS_DEV=$PWD/plugins 2 | ../nextflow/launch.sh "$@" 3 | -------------------------------------------------------------------------------- /plugins/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-2022, Seqera Labs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | id "java" 19 | id "io.nextflow.nf-build-plugin" version "1.0.1" 20 | } 21 | 22 | ext.github_organization = 'nextflow-io' 23 | ext.github_username = project.findProperty('github_username') ?: 'pditommaso' 24 | ext.github_access_token = project.findProperty('github_access_token') ?: System.getenv('GITHUB_TOKEN') 25 | ext.github_commit_email = project.findProperty('github_commit_email') ?: 'paolo.ditommaso@gmail.com' 26 | 27 | jar.enabled = false 28 | 29 | String computeSha512(File file) { 30 | if( !file.exists() ) 31 | throw new GradleException("Missing file: $file -- cannot compute SHA-512") 32 | return org.apache.commons.codec.digest.DigestUtils.sha512Hex(file.bytes) 33 | } 34 | 35 | String now() { 36 | "${java.time.OffsetDateTime.now().format(java.time.format.DateTimeFormatter.ISO_DATE_TIME)}" 37 | } 38 | 39 | List allPlugins() { 40 | def plugins = [] 41 | new File(rootProject.rootDir, 'plugins') .eachDir { if(it.name.startsWith('nf-')) plugins.add(it.name) } 42 | return plugins 43 | } 44 | 45 | String metaFromManifest(String meta, File file) { 46 | def str = file.text 47 | def regex = ~/(?m)^$meta:\s*([\w-\.<>=]+)$/ 48 | def m = regex.matcher(str) 49 | if( m.find() ) { 50 | def ver = m.group(1) 51 | //println "Set plugin '${file.parentFile.parentFile.parentFile.parentFile.name}' version=${ver}" 52 | return ver 53 | } 54 | throw new GradleException("Cannot find '$meta' for plugin: $file") 55 | } 56 | 57 | def timestamp = now() 58 | 59 | subprojects { 60 | apply plugin: 'java' 61 | apply plugin: 'groovy' 62 | apply plugin: 'io.nextflow.nf-build-plugin' 63 | 64 | repositories { 65 | mavenLocal() 66 | mavenCentral() 67 | } 68 | 69 | version = metaFromManifest('Plugin-Version',file('src/resources/META-INF/MANIFEST.MF')) 70 | 71 | tasks.withType(Jar) { 72 | duplicatesStrategy = DuplicatesStrategy.INCLUDE 73 | } 74 | 75 | /* 76 | * Creates plugin zip and json meta file in plugin `build/libs` directory 77 | */ 78 | task makeZip(type: Jar) { 79 | into('classes') { with jar } 80 | into('lib') { from configurations.runtimeClasspath } 81 | manifest.from file('src/resources/META-INF/MANIFEST.MF') 82 | archiveExtension = 'zip' 83 | preserveFileTimestamps = false 84 | reproducibleFileOrder = true 85 | 86 | doLast { 87 | // create the meta file 88 | final zip = new File("$buildDir/libs/${project.name}-${project.version}.zip") 89 | final json = new File("$buildDir/libs/${project.name}-${project.version}-meta.json") 90 | json.text = """\ 91 | { 92 | "version": "${project.version}", 93 | "date": "${timestamp}", 94 | "url": "https://github.com/${github_organization}/${project.name}/releases/download/${project.version}/${project.name}-${project.version}.zip", 95 | "requires": "${metaFromManifest('Plugin-Requires',file('src/resources/META-INF/MANIFEST.MF'))}", 96 | "sha512sum": "${computeSha512(zip)}" 97 | } 98 | """.stripIndent() 99 | // cleanup tmp dir 100 | file("$buildDir/tmp/makeZip").deleteDir() 101 | } 102 | outputs.file("$buildDir/libs/${project.name}-${project.version}.zip") 103 | } 104 | 105 | /* 106 | * Copy the plugin dependencies in the subproject `build/target/libs` directory 107 | */ 108 | task copyPluginLibs(type: Sync) { 109 | from configurations.runtimeClasspath 110 | into 'build/target/libs' 111 | } 112 | 113 | /* 114 | * Copy the plugin in the project root build/plugins directory 115 | */ 116 | task copyPluginZip(type: Copy, dependsOn: project.tasks.findByName('makeZip')) { 117 | from makeZip 118 | into "$rootProject.buildDir/plugins" 119 | outputs.file("$rootProject.buildDir/plugins/${project.name}-${project.version}.zip") 120 | doLast { 121 | ant.unzip( 122 | src: "$rootProject.buildDir/plugins/${project.name}-${project.version}.zip", 123 | dest: "$rootProject.buildDir/plugins/${project.name}-${project.version}" 124 | ) 125 | } 126 | } 127 | 128 | /* 129 | * "install" the plugin the project root build/plugins directory 130 | */ 131 | project.parent.tasks.getByName("assemble").dependsOn << copyPluginZip 132 | 133 | task uploadPlugin(type: io.nextflow.gradle.tasks.GithubUploader, dependsOn: makeZip) { 134 | assets = providers.provider {["$buildDir/libs/${project.name}-${project.version}.zip", 135 | "$buildDir/libs/${project.name}-${project.version}-meta.json" ]} 136 | release = providers.provider { project.version } 137 | repo = providers.provider { project.name } 138 | owner = github_organization 139 | userName = github_username 140 | authToken = github_access_token 141 | skipExisting = true 142 | } 143 | 144 | jar { 145 | from sourceSets.main.allSource 146 | doLast { 147 | file("$buildDir/tmp/jar").deleteDir() 148 | } 149 | } 150 | 151 | tasks.withType(GenerateModuleMetadata) { 152 | enabled = false 153 | } 154 | 155 | task upload(dependsOn: [uploadPlugin] ) { } 156 | } 157 | 158 | /* 159 | * Upload all plugins to the corresponding GitHub repos 160 | */ 161 | task upload(dependsOn: [subprojects.uploadPlugin]) { } 162 | 163 | /* 164 | * Copies the plugins required dependencies in the corresponding lib directory 165 | */ 166 | classes.dependsOn subprojects.copyPluginLibs 167 | 168 | /* 169 | * Merge and publish the plugins index file 170 | */ 171 | task publishIndex( type: io.nextflow.gradle.tasks.GithubRepositoryPublisher ) { 172 | indexUrl = 'https://github.com/nextflow-io/plugins/main/plugins.json' 173 | repos = allPlugins() 174 | owner = github_organization 175 | githubUser = github_username 176 | githubEmail = github_commit_email 177 | githubToken = github_access_token 178 | } 179 | -------------------------------------------------------------------------------- /plugins/nf-gpt/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-2022, Seqera Labs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | // Apply the groovy plugin to add support for Groovy 19 | id 'io.nextflow.groovy-library-conventions' 20 | id 'idea' 21 | } 22 | 23 | group = 'io.nextflow' 24 | // DO NOT SET THE VERSION HERE 25 | // THE VERSION FOR PLUGINS IS DEFINED IN THE `/resources/META-INF/MANIFEST.NF` file 26 | 27 | idea { 28 | module.inheritOutputDirs = true 29 | } 30 | 31 | repositories { 32 | mavenCentral() 33 | maven { url = 'https://jitpack.io' } 34 | maven { url = 'https://s3-eu-west-1.amazonaws.com/maven.seqera.io/releases' } 35 | maven { url = 'https://s3-eu-west-1.amazonaws.com/maven.seqera.io/snapshots' } 36 | } 37 | 38 | configurations { 39 | // see https://docs.gradle.org/4.1/userguide/dependency_management.html#sub:exclude_transitive_dependencies 40 | runtimeClasspath.exclude group: 'org.slf4j', module: 'slf4j-api' 41 | } 42 | 43 | sourceSets { 44 | main.java.srcDirs = [] 45 | main.groovy.srcDirs = ['src/main'] 46 | main.resources.srcDirs = ['src/resources'] 47 | test.groovy.srcDirs = ['src/test'] 48 | test.java.srcDirs = [] 49 | test.resources.srcDirs = ['src/testResources'] 50 | } 51 | 52 | ext{ 53 | nextflowVersion = '24.03.0-edge' 54 | } 55 | 56 | dependencies { 57 | // This dependency is exported to consumers, that is to say found on their compile classpath. 58 | compileOnly "io.nextflow:nextflow:$nextflowVersion" 59 | compileOnly 'org.slf4j:slf4j-api:1.7.10' 60 | compileOnly 'org.pf4j:pf4j:3.4.1' 61 | // add here plugins depepencies 62 | api 'dev.langchain4j:langchain4j-open-ai:0.28.0' 63 | 64 | // test configuration 65 | testImplementation "org.apache.groovy:groovy:4.0.20" 66 | testImplementation "org.apache.groovy:groovy-nio:4.0.20" 67 | testImplementation "io.nextflow:nextflow:$nextflowVersion" 68 | testImplementation ("org.apache.groovy:groovy-test:4.0.20") { exclude group: 'org.apache.groovy' } 69 | testImplementation ("cglib:cglib-nodep:3.3.0") 70 | testImplementation ("org.objenesis:objenesis:3.1") 71 | testImplementation ("org.spockframework:spock-core:2.3-groovy-4.0") { exclude group: 'org.apache.groovy'; exclude group: 'net.bytebuddy' } 72 | testImplementation ('org.spockframework:spock-junit4:2.3-groovy-4.0') { exclude group: 'org.apache.groovy'; exclude group: 'net.bytebuddy' } 73 | testImplementation ('com.google.jimfs:jimfs:1.1') 74 | 75 | testImplementation(testFixtures("io.nextflow:nextflow:$nextflowVersion")) 76 | testImplementation(testFixtures("io.nextflow:nf-commons:$nextflowVersion")) 77 | 78 | // see https://docs.gradle.org/4.1/userguide/dependency_management.html#sec:module_replacement 79 | modules { 80 | module("commons-logging:commons-logging") { replacedBy("org.slf4j:jcl-over-slf4j") } 81 | } 82 | } 83 | 84 | // use JUnit 5 platform 85 | test { 86 | useJUnitPlatform() 87 | } 88 | 89 | tasks.withType(Test) { 90 | jvmArgs ([ 91 | '--add-opens=java.base/java.lang=ALL-UNNAMED', 92 | '--add-opens=java.base/java.io=ALL-UNNAMED', 93 | '--add-opens=java.base/java.nio=ALL-UNNAMED', 94 | '--add-opens=java.base/java.nio.file.spi=ALL-UNNAMED', 95 | '--add-opens=java.base/java.net=ALL-UNNAMED', 96 | '--add-opens=java.base/java.util=ALL-UNNAMED', 97 | '--add-opens=java.base/java.util.concurrent.locks=ALL-UNNAMED', 98 | '--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED', 99 | '--add-opens=java.base/sun.nio.ch=ALL-UNNAMED', 100 | '--add-opens=java.base/sun.nio.fs=ALL-UNNAMED', 101 | '--add-opens=java.base/sun.net.www.protocol.http=ALL-UNNAMED', 102 | '--add-opens=java.base/sun.net.www.protocol.https=ALL-UNNAMED', 103 | '--add-opens=java.base/sun.net.www.protocol.ftp=ALL-UNNAMED', 104 | '--add-opens=java.base/sun.net.www.protocol.file=ALL-UNNAMED', 105 | '--add-opens=java.base/jdk.internal.misc=ALL-UNNAMED', 106 | '--add-opens=java.base/jdk.internal.vm=ALL-UNNAMED', 107 | ]) 108 | } 109 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/main/nextflow/gpt/GptPlugin.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2024, Seqera Labs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package nextflow.gpt 19 | 20 | import groovy.transform.CompileStatic 21 | import groovy.util.logging.Slf4j 22 | import nextflow.plugin.BasePlugin 23 | import org.pf4j.PluginWrapper 24 | /** 25 | * Nextflow AI plugin 26 | * 27 | * @author Paolo Di Tommaso 28 | */ 29 | @Slf4j 30 | @CompileStatic 31 | class GptPlugin extends BasePlugin { 32 | 33 | GptPlugin(PluginWrapper wrapper) { 34 | super(wrapper) 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/main/nextflow/gpt/client/GptChatCompletionRequest.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2024, Seqera Labs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package nextflow.gpt.client 19 | 20 | import groovy.transform.Canonical 21 | import groovy.transform.CompileStatic 22 | import groovy.transform.ToString 23 | 24 | /** 25 | * Model a GTP chat conversation create request object. 26 | * 27 | * See also 28 | * https://platform.openai.com/docs/api-reference/chat/create 29 | * 30 | * @author Paolo Di Tommaso 31 | */ 32 | @CompileStatic 33 | @ToString(includePackage = false, includeNames = true) 34 | class GptChatCompletionRequest { 35 | 36 | @ToString(includePackage = false, includeNames = true) 37 | @CompileStatic 38 | static class Message { 39 | String role 40 | String content 41 | } 42 | 43 | @ToString(includePackage = false, includeNames = true) 44 | @CompileStatic 45 | static class ToolMessage extends Message { 46 | String name 47 | String tool_call_id 48 | } 49 | 50 | @ToString(includePackage = false, includeNames = true) 51 | @CompileStatic 52 | static class Tool { 53 | String type 54 | Function function 55 | } 56 | 57 | @ToString(includePackage = false, includeNames = true) 58 | @CompileStatic 59 | static class Function { 60 | String name 61 | String description 62 | Parameters parameters 63 | } 64 | 65 | @ToString(includePackage = false, includeNames = true) 66 | @CompileStatic 67 | static class Parameters { 68 | String type 69 | Map properties 70 | List required 71 | } 72 | 73 | @ToString(includePackage = false, includeNames = true) 74 | @CompileStatic 75 | static class Param { 76 | String type 77 | String description 78 | } 79 | 80 | @ToString(includePackage = false, includeNames = true) 81 | @CompileStatic 82 | @Canonical 83 | static class ResponseFormat { 84 | static final ResponseFormat TEXT = new ResponseFormat('text') 85 | static final ResponseFormat JSON = new ResponseFormat('json_object') 86 | final String type 87 | } 88 | 89 | /** 90 | * ID of the model to use. 91 | */ 92 | String model 93 | 94 | /** 95 | * A list of tools the model may call 96 | */ 97 | List messages 98 | 99 | List tools 100 | 101 | String tool_choice 102 | 103 | /** 104 | * The maximum number of tokens that can be generated in the chat completion 105 | */ 106 | Integer max_tokens 107 | 108 | /** 109 | * How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices 110 | */ 111 | Integer n 112 | 113 | /** 114 | * What sampling temperature to use, between 0 and 2 115 | */ 116 | Float temperature 117 | 118 | /** 119 | * Modify the likelihood of specified tokens appearing in the completion 120 | */ 121 | Map logit_bias 122 | 123 | /** 124 | * Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is valid JSON. 125 | */ 126 | ResponseFormat response_format 127 | } 128 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/main/nextflow/gpt/client/GptChatCompletionResponse.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2024, Seqera Labs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package nextflow.gpt.client 19 | 20 | import groovy.transform.CompileStatic 21 | import groovy.transform.ToString 22 | /** 23 | * Model the GPT chat conversation response object 24 | * 25 | * See also 26 | * https://platform.openai.com/docs/api-reference/chat/object 27 | * 28 | * @author Paolo Di Tommaso 29 | */ 30 | @CompileStatic 31 | @ToString(includePackage = false, includeNames = true) 32 | class GptChatCompletionResponse { 33 | 34 | @ToString(includePackage = false, includeNames = true) 35 | static class Choice { 36 | String finish_reason 37 | Integer index 38 | Message message 39 | } 40 | 41 | @ToString(includePackage = false, includeNames = true) 42 | static class Message { 43 | String role 44 | String content 45 | List tool_calls 46 | } 47 | 48 | @ToString(includePackage = false, includeNames = true) 49 | static class ToolCall { 50 | String id 51 | String type 52 | Function function 53 | } 54 | 55 | @ToString(includePackage = false, includeNames = true) 56 | static class Function { 57 | String name 58 | String arguments 59 | } 60 | 61 | String id 62 | String object 63 | Long created 64 | String model 65 | List choices 66 | } 67 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/main/nextflow/gpt/client/GptClient.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2024, Seqera Labs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package nextflow.gpt.client 19 | 20 | import java.net.http.HttpClient 21 | import java.net.http.HttpRequest 22 | import java.net.http.HttpResponse 23 | import java.time.temporal.ChronoUnit 24 | import java.util.concurrent.Executors 25 | import java.util.function.Predicate 26 | 27 | import com.google.gson.Gson 28 | import com.google.gson.reflect.TypeToken 29 | import dev.failsafe.Failsafe 30 | import dev.failsafe.RetryPolicy 31 | import dev.failsafe.event.EventListener 32 | import dev.failsafe.event.ExecutionAttemptedEvent 33 | import dev.failsafe.function.CheckedSupplier 34 | import groovy.transform.CompileStatic 35 | import groovy.transform.Memoized 36 | import groovy.util.logging.Slf4j 37 | import nextflow.gpt.config.GptConfig 38 | import nextflow.util.Threads 39 | /** 40 | * HTTP client for Gpt based API conversation 41 | * 42 | * @author Paolo Di Tommaso 43 | */ 44 | @Slf4j 45 | @CompileStatic 46 | class GptClient { 47 | 48 | final private String endpoint 49 | final private GptConfig config 50 | private HttpClient httpClient 51 | 52 | @Memoized 53 | static GptClient client(GptConfig config) { 54 | new GptClient(config) 55 | } 56 | 57 | static GptClient client() { 58 | return client(GptConfig.config()) 59 | } 60 | 61 | /** 62 | * Only for testing 63 | */ 64 | protected GptClient() { } 65 | 66 | protected GptClient(GptConfig config) { 67 | this.config = config 68 | this.endpoint = config.endpoint() 69 | // create http client 70 | this.httpClient = newHttpClient() 71 | } 72 | 73 | protected HttpClient newHttpClient() { 74 | final builder = HttpClient.newBuilder() 75 | .version(HttpClient.Version.HTTP_1_1) 76 | .followRedirects(HttpClient.Redirect.NEVER) 77 | // use virtual threads executor if enabled 78 | if( Threads.useVirtual() ) 79 | builder.executor(Executors.newVirtualThreadPerTaskExecutor()) 80 | // build and return the new client 81 | return builder.build() 82 | } 83 | 84 | GptChatCompletionResponse sendRequest(GptChatCompletionRequest request) { 85 | return sendRequest0(request, 1) 86 | } 87 | 88 | GptChatCompletionResponse sendRequest0(GptChatCompletionRequest request, int attempt) { 89 | assert endpoint, 'Missing ChatGPT endpoint' 90 | assert !endpoint.endsWith('/'), "Endpoint url must not end with a slash - offending value: $endpoint" 91 | assert config.apiKey(), "Missing ChatGPT API key" 92 | 93 | final body = new Gson().toJson(request) 94 | final uri = URI.create("${endpoint}/v1/chat/completions") 95 | log.debug "ChatGPT request: $uri; attempt=$attempt - request: $body" 96 | final req = HttpRequest.newBuilder() 97 | .uri(uri) 98 | .headers('Content-Type','application/json') 99 | .headers('Authorization', "Bearer ${config.apiKey()}") 100 | .POST(HttpRequest.BodyPublishers.ofString(body)) 101 | .build() 102 | 103 | try { 104 | final resp = httpSend(req) 105 | log.debug "ChatGPT response: statusCode=${resp.statusCode()}; body=${resp.body()}" 106 | if( resp.statusCode()==200 ) 107 | return jsonToCompletionResponse(resp.body()) 108 | else 109 | throw new IllegalStateException("ChatGPT unexpected response: [${resp.statusCode()}] ${resp.body()}") 110 | } 111 | catch (IOException e) { 112 | throw new IllegalStateException("Unable to connect ChatGPT service: $endpoint") 113 | } 114 | } 115 | 116 | protected GptChatCompletionResponse jsonToCompletionResponse(String json) { 117 | final type = new TypeToken(){}.getType() 118 | return new Gson().fromJson(json, type) 119 | } 120 | 121 | protected RetryPolicy retryPolicy(Predicate cond, Predicate handle) { 122 | final cfg = config.retryOpts() 123 | final listener = new EventListener>() { 124 | @Override 125 | void accept(ExecutionAttemptedEvent event) throws Throwable { 126 | def msg = "Gpt connection failure - attempt: ${event.attemptCount}" 127 | if( event.lastResult!=null ) 128 | msg += "; response: ${event.lastResult}" 129 | if( event.lastFailure != null ) 130 | msg += "; exception: [${event.lastFailure.class.name}] ${event.lastFailure.message}" 131 | log.debug(msg) 132 | } 133 | } 134 | return RetryPolicy.builder() 135 | .handleIf(cond) 136 | .handleResultIf(handle) 137 | .withBackoff(cfg.delay.toMillis(), cfg.maxDelay.toMillis(), ChronoUnit.MILLIS) 138 | .withMaxAttempts(cfg.maxAttempts) 139 | .withJitter(cfg.jitter) 140 | .onRetry(listener) 141 | .build() 142 | } 143 | 144 | protected HttpResponse safeApply(CheckedSupplier action) { 145 | final retryOnException = (e -> e instanceof IOException) as Predicate 146 | final retryOnStatusCode = ((HttpResponse resp) -> resp.statusCode() in SERVER_ERRORS) as Predicate> 147 | final policy = retryPolicy(retryOnException, retryOnStatusCode) 148 | return Failsafe.with(policy).get(action) 149 | } 150 | 151 | static private final List SERVER_ERRORS = [429,500,502,503,504] 152 | 153 | protected HttpResponse httpSend(HttpRequest req) { 154 | return safeApply(() -> httpClient.send(req, HttpResponse.BodyHandlers.ofString())) 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/main/nextflow/gpt/config/GptConfig.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2024, Seqera Labs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package nextflow.gpt.config 19 | 20 | import groovy.transform.CompileStatic 21 | import groovy.transform.ToString 22 | import nextflow.Global 23 | import nextflow.Session 24 | import nextflow.SysEnv 25 | /** 26 | * Model AI configuration 27 | * 28 | * @author Paolo Di Tommaso 29 | */ 30 | @ToString(includeFields = true, includeNames = true, includePackage = false) 31 | @CompileStatic 32 | class GptConfig { 33 | 34 | static final String DEFAULT_ENDPOINT = 'https://api.openai.com' 35 | static final String DEFAULT_MODEL = 'gpt-3.5-turbo' 36 | static final Double DEFAULT_TEMPERATURE = 0.7d 37 | 38 | private String endpoint 39 | private String apiKey 40 | private String model 41 | private Double temperature 42 | private Integer maxTokens 43 | private GptRetryOpts retryOpts 44 | 45 | static GptConfig config(Session session) { 46 | new GptConfig(session.config.gpt as Map ?: Collections.emptyMap(), SysEnv.get()) 47 | } 48 | 49 | static GptConfig config() { 50 | config(Global.session as Session) 51 | } 52 | 53 | GptConfig(Map opts, Map env) { 54 | this.endpoint = opts.endpoint ?: DEFAULT_ENDPOINT 55 | this.model = opts.model ?: DEFAULT_MODEL 56 | this.apiKey = opts.apiKey ?: env.get('OPENAI_API_KEY') 57 | this.temperature = opts.temperature!=null ? temperature as Double : DEFAULT_TEMPERATURE 58 | this.retryOpts = new GptRetryOpts( opts.retryPolicy as Map ?: Map.of() ) 59 | } 60 | 61 | String endpoint() { 62 | return endpoint 63 | } 64 | 65 | String apiKey() { 66 | return apiKey 67 | } 68 | 69 | String model() { 70 | return model 71 | } 72 | 73 | Double temperature() { 74 | return temperature 75 | } 76 | 77 | Integer maxTokens() { 78 | return maxTokens 79 | } 80 | 81 | GptRetryOpts retryOpts() { 82 | return retryOpts 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/main/nextflow/gpt/config/GptRetryOpts.groovy: -------------------------------------------------------------------------------- 1 | package nextflow.gpt.config 2 | 3 | import groovy.transform.CompileStatic 4 | import groovy.transform.ToString 5 | import nextflow.util.Duration 6 | 7 | @ToString(includeNames = true, includePackage = false) 8 | @CompileStatic 9 | class GptRetryOpts { 10 | Duration delay = Duration.of('450ms') 11 | Duration maxDelay = Duration.of('90s') 12 | int maxAttempts = 10 13 | double jitter = 0.25 14 | 15 | GptRetryOpts() { 16 | this(Collections.emptyMap()) 17 | } 18 | 19 | GptRetryOpts(Map config) { 20 | if( config.delay ) 21 | delay = config.delay as Duration 22 | if( config.maxDelay ) 23 | maxDelay = config.maxDelay as Duration 24 | if( config.maxAttempts ) 25 | maxAttempts = config.maxAttempts as int 26 | if( config.jitter ) 27 | jitter = config.jitter as double 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/main/nextflow/gpt/prompt/GptHelper.groovy: -------------------------------------------------------------------------------- 1 | package nextflow.gpt.prompt 2 | 3 | import dev.langchain4j.data.message.AiMessage 4 | import dev.langchain4j.data.message.ChatMessage 5 | import dev.langchain4j.data.message.SystemMessage 6 | import dev.langchain4j.data.message.UserMessage 7 | 8 | /** 9 | * Helper methods for GPT conversation 10 | * 11 | * @author Paolo Di Tommaso 12 | */ 13 | class GptHelper { 14 | 15 | static protected String renderSchema(Map schema) { 16 | return 'You must answer strictly in the following JSON format: {"result": [' + schema0(schema) + '] }' 17 | } 18 | 19 | static protected String schema0(Object schema) { 20 | if( schema instanceof List ) { 21 | return "[" + (schema as List).collect(it -> schema0(it)).join(', ') + "]" 22 | } 23 | else if( schema instanceof Map ) { 24 | return "{" + (schema as Map).collect( it -> "\"$it.key\": " + schema0(it.value) ).join(', ') + "}" 25 | } 26 | else if( schema instanceof CharSequence ) { 27 | return "(type: $schema)" 28 | } 29 | else if( schema != null ) 30 | throw new IllegalArgumentException("Unexpected data type: ") 31 | else 32 | throw new IllegalArgumentException("Data structure cannot be null") 33 | } 34 | 35 | static protected List> decodeResponse(Object response, Map schema) { 36 | final result = decodeResponse0(response,schema) 37 | if( !result ) 38 | throw new IllegalArgumentException("Response does not match expected schema: $schema - Offending value: $response") 39 | return result 40 | } 41 | 42 | static protected List> decodeResponse0(Object response, Map schema) { 43 | final expected = schema.keySet() 44 | if( response instanceof Map ) { 45 | if( response.keySet()==expected ) { 46 | return List.of(response as Map) 47 | } 48 | if( isIndexMap(response, schema) ) { 49 | return new ArrayList>(response.values() as Collection>) 50 | } 51 | if( response.size()==1 ) { 52 | return decodeResponse(response.values().first(), schema) 53 | } 54 | } 55 | 56 | if( response instanceof List ) { 57 | final it = (response as List).first() 58 | if( it instanceof Map && it.keySet()==expected ) 59 | return response as List> 60 | } 61 | return null 62 | } 63 | 64 | static protected boolean isIndexMap(Map response, Map schema) { 65 | final keys = response.keySet() 66 | // check all key are integers e.g. 0, 1, 2 67 | if( keys.every(it-> it.toString().isInteger() ) ) { 68 | // take the first and check the object matches the scherma 69 | final it = response.values().first() 70 | return it instanceof Map && it.keySet()==schema.keySet() 71 | } 72 | return false 73 | } 74 | 75 | static List messageToChat(List> messages) { 76 | if( !messages ) 77 | throw new IllegalArgumentException("Missing 'messages' argument") 78 | final result = new ArrayList () 79 | for( Map it : messages ) { 80 | if( !it.role ) 81 | throw new IllegalArgumentException("Missing 'role' attribute - offending message: $messages") 82 | if( !it.content ) 83 | throw new IllegalArgumentException("Missing 'content' attribute - offending message: $messages") 84 | final msg = switch (it.role) { 85 | case 'user' -> UserMessage.from(it.content) 86 | case 'system' -> SystemMessage.from(it.content) 87 | case 'ai' -> AiMessage.from(it.content) 88 | default -> throw new IllegalArgumentException("Unsupported message role '${it.role}' - offending message: $messages") 89 | } 90 | result.add(msg) 91 | } 92 | return result 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/main/nextflow/gpt/prompt/GptPromptExtension.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2024, Seqera Labs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package nextflow.gpt.prompt 19 | 20 | 21 | import static nextflow.util.CheckHelper.* 22 | 23 | import groovy.transform.CompileStatic 24 | import groovyx.gpars.dataflow.DataflowReadChannel 25 | import groovyx.gpars.dataflow.DataflowWriteChannel 26 | import nextflow.Channel 27 | import nextflow.Session 28 | import nextflow.extension.CH 29 | import nextflow.extension.DataflowHelper 30 | import nextflow.gpt.client.GptChatCompletionRequest 31 | import nextflow.gpt.client.GptClient 32 | import nextflow.gpt.config.GptConfig 33 | import nextflow.plugin.extension.Factory 34 | import nextflow.plugin.extension.Function 35 | import nextflow.plugin.extension.Operator 36 | import nextflow.plugin.extension.PluginExtensionPoint 37 | /** 38 | * Implements GPT Chat extension methods 39 | * 40 | * @author Paolo Di Tommaso 41 | */ 42 | @CompileStatic 43 | class GptPromptExtension extends PluginExtensionPoint { 44 | 45 | static final private Map VALID_PROMPT_DATA_OPTS = [ 46 | model: String, 47 | schema: Map, 48 | debug: Boolean, 49 | temperature: Double, 50 | maxTokens: Integer 51 | ] 52 | 53 | static final private Map VALID_PROMPT_TEXT_OPTS = [ 54 | model: String, 55 | debug: Boolean, 56 | temperature: Double, 57 | maxTokens: Integer, 58 | numOfChoices: Integer, 59 | logitBias: Map 60 | ] 61 | 62 | private Session session 63 | 64 | @Override 65 | protected void init(Session session) { 66 | this.session = session 67 | } 68 | 69 | @Factory 70 | DataflowWriteChannel fromPrompt(Map opts, String query) { 71 | // check params 72 | checkParams( 'fromPrompt', opts, VALID_PROMPT_DATA_OPTS ) 73 | if( opts.schema == null ) 74 | throw new IllegalArgumentException("Missing prompt schema") 75 | // create the client 76 | final ai = new GptPromptModel(session) 77 | .withModel(opts.model as String) 78 | .withDebug(opts.debug as Boolean) 79 | .withTemperature(opts.temperature as Double) 80 | .withMaxToken(opts.maxTokens as Integer) 81 | .withJsonResponseFormat() 82 | .build() 83 | // run the prompt 84 | final response = ai.prompt(query, opts.schema as Map) 85 | final target = CH.create() 86 | CH.emitAndClose(target, response) 87 | return target 88 | } 89 | 90 | @Operator 91 | DataflowWriteChannel prompt(DataflowReadChannel source, Map opts) { 92 | prompt(source, opts, it-> it.toString()) 93 | } 94 | 95 | @Operator 96 | DataflowWriteChannel prompt(DataflowReadChannel source, Map opts, Closure template) { 97 | // check params 98 | checkParams( 'prompt', opts, VALID_PROMPT_DATA_OPTS ) 99 | if( opts.schema == null ) 100 | throw new IllegalArgumentException("Missing prompt schema") 101 | // create the client 102 | final ai = new GptPromptModel(session) 103 | .withModel(opts.model as String) 104 | .withDebug(opts.debug as Boolean) 105 | .withTemperature(opts.temperature as Double) 106 | .withMaxToken(opts.maxTokens as Integer) 107 | .withJsonResponseFormat() 108 | .build() 109 | 110 | final target = CH.createBy(source) 111 | final next = { it-> runPrompt(ai, template.call(it), opts.schema as Map, target) } 112 | final done = { target.bind(Channel.STOP) } 113 | DataflowHelper.subscribeImpl(source, [onNext: next, onComplete: done]) 114 | return target 115 | } 116 | 117 | private void runPrompt(GptPromptModel ai, String query, Map schema, DataflowWriteChannel target) { 118 | // carry out the response 119 | final response = ai.prompt(query, schema) 120 | // emit the results 121 | for( Map it : response ) { 122 | target.bind(it) 123 | } 124 | } 125 | 126 | @Function 127 | List> gptPromptForData(Map opts, CharSequence query) { 128 | // check params 129 | checkParams( 'gptPromptForData', opts, VALID_PROMPT_DATA_OPTS ) 130 | if( opts.schema == null ) 131 | throw new IllegalArgumentException("Missing prompt schema") 132 | // create the client 133 | final ai = new GptPromptModel(session) 134 | .withModel(opts.model as String) 135 | .withDebug(opts.debug as Boolean) 136 | .withTemperature(opts.temperature as Double) 137 | .withMaxToken(opts.maxTokens as Integer) 138 | .withJsonResponseFormat() 139 | .build() 140 | 141 | return ai.prompt(query.toString(), opts.schema as Map) 142 | } 143 | 144 | /** 145 | * Carry out a GPT text prompt providing one or more messages 146 | * 147 | * @param opts 148 | * Hold the prompt options 149 | * @param messages 150 | * The prompt message content 151 | * @return 152 | * The response content as a string or a list of string when the {@code numOfChoices} option is specified 153 | */ 154 | @Function 155 | Object gptPromptForText(Map opts=Map.of(), String message) { 156 | gptPromptForText(opts, List.of(Map.of('role','user', 'content',message))) 157 | } 158 | 159 | /** 160 | * Carry out a GPT text prompt providing one or more messages 161 | * 162 | * @param opts 163 | * Hold the prompt options 164 | * @param messages 165 | * Hold the messages to carry out the prompt provided a list of key-value pairs, where the key represent 166 | * the message "role" and the value thr message content e.g. 167 | * {@code [ [system: "You should act as a good guy"], [role: "Tell me a joke"] ] 168 | * @return 169 | * The response content as a string or a list of string when the {@code numOfChoices} option is specified 170 | */ 171 | @Function 172 | Object gptPromptForText(Map opts=Map.of(), List> messages) { 173 | // check params 174 | checkParams( 'gptPromptForText', opts, VALID_PROMPT_TEXT_OPTS ) 175 | 176 | final config = GptConfig.config(session) 177 | final client = GptClient.client(config) 178 | final model = opts.model ?: config.model() 179 | final numOfChoices = opts.numOfChoices as Integer ?: 1 180 | final temperature = opts.temperature as Double ?: config.temperature() 181 | final msg = messages.collect ((Map it)-> new GptChatCompletionRequest.Message(role:it.role, content:it.content)) 182 | final request = new GptChatCompletionRequest( 183 | model: model, 184 | temperature: temperature, 185 | messages: msg, 186 | n: numOfChoices, 187 | max_tokens: opts.maxTokens as Integer, 188 | logit_bias: opts.logitBias as Map 189 | ) 190 | final resp = client.sendRequest(request) 191 | return opts.numOfChoices==null 192 | ? resp.choices.get(0).message.content 193 | : resp.choices.collect(it-> it.message.content) 194 | } 195 | 196 | } 197 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/main/nextflow/gpt/prompt/GptPromptModel.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2024, Seqera Labs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package nextflow.gpt.prompt 19 | 20 | import dev.langchain4j.data.message.ChatMessage 21 | import dev.langchain4j.data.message.SystemMessage 22 | import dev.langchain4j.data.message.UserMessage 23 | import dev.langchain4j.model.openai.OpenAiChatModel 24 | import groovy.json.JsonSlurper 25 | import groovy.transform.CompileStatic 26 | import groovy.util.logging.Slf4j 27 | import nextflow.Session 28 | import nextflow.gpt.config.GptConfig 29 | import nextflow.util.StringUtils 30 | 31 | import static nextflow.gpt.prompt.GptHelper.* 32 | 33 | /** 34 | * Simple AI client for OpenAI model 35 | * 36 | * @author Paolo Di Tommaso 37 | */ 38 | @Slf4j 39 | @CompileStatic 40 | class GptPromptModel { 41 | 42 | private static final String JSON_OBJECT = "json_object" 43 | 44 | private GptConfig config 45 | private OpenAiChatModel client 46 | 47 | private String model 48 | private boolean debug 49 | private Double temperature 50 | private Integer maxTokens 51 | private String responseFormat 52 | 53 | GptPromptModel(Session session) { 54 | this.config = GptConfig.config(session) 55 | } 56 | 57 | GptPromptModel withModel(String model) { 58 | this.model = model 59 | return this 60 | } 61 | 62 | GptPromptModel withDebug(Boolean value) { 63 | this.debug = value 64 | return this 65 | } 66 | 67 | GptPromptModel withTemperature(Double d) { 68 | this.temperature = d 69 | return this 70 | } 71 | 72 | GptPromptModel withMaxToken(Integer i) { 73 | this.maxTokens = i 74 | return this 75 | } 76 | 77 | GptPromptModel withResponseFormat(String format) { 78 | this.responseFormat = format 79 | return this 80 | } 81 | 82 | GptPromptModel withJsonResponseFormat() { 83 | this.responseFormat = JSON_OBJECT 84 | return this 85 | } 86 | 87 | GptPromptModel build() { 88 | final modelName = model ?: config.model() 89 | final temperature = this.temperature ?: config.temperature() 90 | final tokens = maxTokens ?: config.maxTokens() 91 | log.debug "Creating OpenAI chat model: $modelName; api-key: ${StringUtils.redact(config.apiKey())}; temperature: $temperature; maxTokens: ${maxTokens}" 92 | client = OpenAiChatModel.builder() 93 | .apiKey(config.apiKey()) 94 | .modelName(modelName) 95 | .logRequests(debug) 96 | .logResponses(debug) 97 | .temperature(temperature) 98 | .maxTokens(tokens) 99 | .responseFormat(responseFormat) 100 | .build(); 101 | return this 102 | } 103 | 104 | List> prompt(List messages, Map schema) { 105 | if( !messages ) 106 | throw new IllegalArgumentException("Missing AI prompt") 107 | if( !schema ) 108 | throw new IllegalArgumentException("Missing AI prompt schema") 109 | if( responseFormat!=JSON_OBJECT ) 110 | throw new IllegalStateException("AI prompt requires json_object response format") 111 | final all = new ArrayList(messages) 112 | all.add(SystemMessage.from(renderSchema(schema))) 113 | if( debug ) 114 | log.debug "AI message: $all" 115 | final json = client.generate(all).content().text() 116 | if( debug ) 117 | log.debug "AI response: $json" 118 | return decodeResponse(new JsonSlurper().parseText(json), schema) 119 | } 120 | 121 | List> prompt(String query, Map schema) { 122 | if( !query ) 123 | throw new IllegalArgumentException("Missing AI prompt") 124 | final msg = UserMessage.from(query) 125 | return prompt(List.of(msg), schema) 126 | } 127 | 128 | String generate(List messages) { 129 | if( responseFormat ) 130 | throw new IllegalArgumentException("Response format '$responseFormat' not support by 'generate' function") 131 | return client.generate(messages).content().text() 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/main/nextflow/processor/tip/GptTaskTipProvider.groovy: -------------------------------------------------------------------------------- 1 | package nextflow.processor.tip 2 | 3 | import groovy.transform.CompileStatic 4 | import groovy.util.logging.Slf4j 5 | import nextflow.Global 6 | import nextflow.Session 7 | import nextflow.SysEnv 8 | import nextflow.gpt.config.GptConfig 9 | import nextflow.gpt.prompt.GptPromptModel 10 | import nextflow.plugin.Priority 11 | 12 | /** 13 | * Implements a provider that uses Chat GPT to suggest 14 | * tip on task failure 15 | * 16 | * @author Paolo Di Tommaso 17 | */ 18 | @Slf4j 19 | @Priority(-10) 20 | @CompileStatic 21 | class GptTaskTipProvider implements TaskTipProvider { 22 | 23 | private Session session 24 | 25 | GptTaskTipProvider() { 26 | this.session = Global.session as Session 27 | } 28 | 29 | @Override 30 | boolean enabled() { 31 | if( SysEnv.get('NXF_GPT_TIP_ENABLED','true')=='false') { 32 | log.debug "Env variable NXF_GPT_TIP_ENABLED=false detected - disable GPT tip provider" 33 | return false 34 | } 35 | final apiKey = GptConfig.config(session).apiKey() 36 | if (!apiKey) { 37 | log.debug "OpenAI API key is not available - disable GPT tip provider" 38 | return false 39 | } 40 | return true 41 | } 42 | 43 | @Override 44 | String suggestTip(List context) { 45 | final ai = new GptPromptModel(session) 46 | .withResponseFormat('json_object') 47 | .build() 48 | final query = """\ 49 | I ran Nextflow but it failed with the following error. Please explain in as few words as possible. 50 | 51 | ``` 52 | ${context.join('\n')} 53 | ``` 54 | 55 | """ 56 | final result = ai.prompt(query, [explaination: 'string']) 57 | 58 | return result.first().explaination 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/resources/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Plugin-Class: nextflow.gpt.GptPlugin 3 | Plugin-Id: nf-gpt 4 | Plugin-Version: 0.4.0 5 | Plugin-Provider: Seqera Labs 6 | Plugin-Requires: >=24.03.0-edge 7 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/resources/META-INF/extensions.idx: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2024, Seqera Labs 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | nextflow.gpt.prompt.GptPromptExtension 18 | nextflow.processor.tip.GptTaskTipProvider 19 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/test/nextflow/gpt/client/GptChatCompletionRequestTest.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2024, Seqera Labs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package nextflow.gpt.client 19 | 20 | import groovy.json.JsonOutput 21 | import spock.lang.Specification 22 | /** 23 | * 24 | * @author Paolo Di Tommaso 25 | */ 26 | class GptChatCompletionRequestTest extends Specification { 27 | 28 | def 'should serialize a request' () { 29 | given: 30 | def p1 = new GptChatCompletionRequest.Param(type: 'string', description: 'Foo') 31 | def p2 = new GptChatCompletionRequest.Param(type: 'string', description: 'Foo') 32 | def parameters =new GptChatCompletionRequest.Parameters(type: 'object', properties: ['p1': p1, 'p2':p2], required: ['none']) 33 | def fun = new GptChatCompletionRequest.Function(name: 'whats_the_weather_like', description: 'Just a description', parameters: parameters) 34 | def tool = new GptChatCompletionRequest.Tool(type:'function', function: fun) 35 | def msg = new GptChatCompletionRequest.Message(role: 'user', content: 'How do you do?') 36 | and: 37 | def request = new GptChatCompletionRequest(model: 'turbo', messages: [msg], tools: [tool]) 38 | when: 39 | def json = JsonOutput.prettyPrint(JsonOutput.toJson(request)) 40 | then: 41 | json == '''\ 42 | { 43 | "model": "turbo", 44 | "messages": [ 45 | { 46 | "role": "user", 47 | "content": "How do you do?" 48 | } 49 | ], 50 | "tools": [ 51 | { 52 | "type": "function", 53 | "function": { 54 | "name": "whats_the_weather_like", 55 | "description": "Just a description", 56 | "parameters": { 57 | "type": "object", 58 | "properties": { 59 | "p1": { 60 | "type": "string", 61 | "description": "Foo" 62 | }, 63 | "p2": { 64 | "type": "string", 65 | "description": "Foo" 66 | } 67 | }, 68 | "required": [ 69 | "none" 70 | ] 71 | } 72 | } 73 | } 74 | ], 75 | "tool_choice": null, 76 | "max_tokens": null, 77 | "n": null, 78 | "temperature": null, 79 | "logit_bias": null, 80 | "response_format": null 81 | } 82 | '''.stripIndent().rightTrim() 83 | 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/test/nextflow/gpt/client/GptClientTest.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2024, Seqera Labs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package nextflow.gpt.client 19 | 20 | import nextflow.Session 21 | import nextflow.gpt.config.GptConfig 22 | import spock.lang.Requires 23 | import spock.lang.Specification 24 | import spock.lang.Timeout 25 | 26 | /** 27 | * 28 | * @author Paolo Di Tommaso 29 | */ 30 | @Timeout(30) 31 | class GptClientTest extends Specification { 32 | 33 | def 'should parse response' () { 34 | given: 35 | def client = Spy(GptClient) 36 | and: 37 | def JSON = ''' 38 | { 39 | "id": "chatcmpl-8w6HJpPYdPLfViMbHiGUCzDXimUEC", 40 | "object": "chat.completion", 41 | "created": 1708857849, 42 | "model": "gpt-3.5-turbo-0125", 43 | "choices": [ 44 | { 45 | "index": 0, 46 | "message": { 47 | "role": "assistant", 48 | "content": null, 49 | "tool_calls": [ 50 | { 51 | "id": "call_Erqx0Rj6JLqOnOln8AOn5lXN", 52 | "type": "function", 53 | "function": { 54 | "name": "get_current_weather", 55 | "arguments": "{\\"location\\": \\"San Francisco, CA\\"}" 56 | } 57 | }, 58 | { 59 | "id": "call_nKQtiSfcHwzUYVcbvAE57kk3", 60 | "type": "function", 61 | "function": { 62 | "name": "get_current_weather", 63 | "arguments": "{\\"location\\": \\"Tokyo\\"}" 64 | } 65 | }, 66 | { 67 | "id": "call_myBENYchUxjtogARuOmzG7cL", 68 | "type": "function", 69 | "function": { 70 | "name": "get_current_weather", 71 | "arguments": "{\\"location\\": \\"Paris\\"}" 72 | } 73 | } 74 | ] 75 | }, 76 | "logprobs": null, 77 | "finish_reason": "tool_calls" 78 | } 79 | ], 80 | "usage": { 81 | "prompt_tokens": 80, 82 | "completion_tokens": 64, 83 | "total_tokens": 144 84 | }, 85 | "system_fingerprint": "fp_86156a94a0" 86 | } 87 | ''' 88 | 89 | when: 90 | def resp = client.jsonToCompletionResponse(JSON) 91 | then: 92 | resp.id == 'chatcmpl-8w6HJpPYdPLfViMbHiGUCzDXimUEC' 93 | resp.object == 'chat.completion' 94 | resp.created == 1708857849 95 | resp.model == 'gpt-3.5-turbo-0125' 96 | and: 97 | resp.choices.size() == 1 98 | resp.choices.get(0).index == 0 99 | resp.choices.get(0).finish_reason == 'tool_calls' 100 | resp.choices.get(0).message.role == 'assistant' 101 | resp.choices.get(0).message.content == null 102 | and: 103 | resp.choices.get(0).message.tool_calls.size() == 3 104 | and: 105 | resp.choices.get(0).message.tool_calls[0].type == 'function' 106 | resp.choices.get(0).message.tool_calls[0].function.name == 'get_current_weather' 107 | resp.choices.get(0).message.tool_calls[0].function.arguments == "{\"location\": \"San Francisco, CA\"}" 108 | and: 109 | resp.choices.get(0).message.tool_calls[1].type == 'function' 110 | resp.choices.get(0).message.tool_calls[1].function.name == 'get_current_weather' 111 | resp.choices.get(0).message.tool_calls[1].function.arguments == "{\"location\": \"Tokyo\"}" 112 | and: 113 | resp.choices.get(0).message.tool_calls[2].type == 'function' 114 | resp.choices.get(0).message.tool_calls[2].function.name == 'get_current_weather' 115 | resp.choices.get(0).message.tool_calls[2].function.arguments == "{\"location\": \"Paris\"}" 116 | } 117 | 118 | @Requires({ System.getenv('OPENAI_API_KEY') }) 119 | def 'should call tools' () { 120 | given: 121 | def query = ''' 122 | Check what's the weather like in San Francisco, Tokyo, and Paris, 123 | then print the temperature for each city. 124 | '''.stripIndent() 125 | and: 126 | def tools = [ 127 | new GptChatCompletionRequest.Tool(type:'function', 128 | function: new GptChatCompletionRequest.Function( 129 | name:'get_current_weather', 130 | description: 'Get the current weather in a given location', 131 | parameters: new GptChatCompletionRequest.Parameters( 132 | type:'object', 133 | properties: [ location: new GptChatCompletionRequest.Param(type:'string',description: 'The city and state, e.g. San Francisco, CA')], 134 | required: []))), 135 | 136 | new GptChatCompletionRequest.Tool(type:'function', 137 | function: new GptChatCompletionRequest.Function( 138 | name:'print_value', 139 | description: 'Print a generic value to the standard output', 140 | parameters: new GptChatCompletionRequest.Parameters( 141 | type:'object', 142 | properties: [ value: new GptChatCompletionRequest.Param(type:'string', description: 'The value to be printed')], 143 | required: []))) 144 | 145 | ] 146 | List messages = [ new GptChatCompletionRequest.Message(role: 'user', content: query) ] 147 | def request = new GptChatCompletionRequest( 148 | model: 'gpt-3.5-turbo-0125', 149 | messages: messages, 150 | tools: tools, 151 | tool_choice: 'auto' ) 152 | 153 | and: 154 | def session = Mock(Session) { 155 | getConfig() >> [:] 156 | } 157 | and: 158 | def config = GptConfig.config(session) 159 | 160 | when: 161 | def response = new GptClient(config).sendRequest(request) 162 | then: 163 | response 164 | 165 | when: 166 | for( def choice : response.choices ) { 167 | messages << choice.message 168 | 169 | for( def tool : choice.message.tool_calls ) { 170 | messages << new GptChatCompletionRequest.ToolMessage( 171 | role: 'tool', 172 | name: tool.function.name, 173 | content: '10', 174 | tool_call_id: tool.id ) 175 | } 176 | } 177 | and: 178 | def request2 = new GptChatCompletionRequest( model: 'gpt-3.5-turbo-0125', messages: messages ) 179 | and: 180 | def response2 = new GptClient(config).sendRequest(request2) 181 | then: 182 | response2 183 | 184 | } 185 | 186 | @Requires({ System.getenv('OPENAI_API_KEY') }) 187 | def 'should get structured output' () { 188 | given: 189 | def session = Mock(Session) { getConfig() >> [:] } 190 | def config = GptConfig.config(session) 191 | def client = new GptClient(config) 192 | and: 193 | def msg1 = new GptChatCompletionRequest.Message(role:'system', content:'You are a helpful assistant designed to output JSON. The top level object contains the attribute `result`. The result is a list of objects having two attributes: `year` and `location`') 194 | def msg2 = new GptChatCompletionRequest.Message(role:'user', content:'List of all editions of olympic games.') 195 | def request = new GptChatCompletionRequest( 196 | model: 'gpt-3.5-turbo-0125', 197 | messages: [msg1, msg2], 198 | response_format: GptChatCompletionRequest.ResponseFormat.JSON 199 | ) 200 | 201 | when: 202 | def response = client.sendRequest(request) 203 | and: 204 | print response.choices[0].message.content 205 | then: 206 | true 207 | 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/test/nextflow/gpt/config/GptConfigTest.groovy: -------------------------------------------------------------------------------- 1 | package nextflow.gpt.config 2 | 3 | import nextflow.Session 4 | import spock.lang.Specification 5 | 6 | /** 7 | * 8 | * @author Paolo Di Tommaso 9 | */ 10 | class GptConfigTest extends Specification { 11 | 12 | def 'should create default config' () { 13 | when: 14 | def config = new GptConfig([:], [OPENAI_API_KEY:'my-api-key']) 15 | then: 16 | config.endpoint() == 'https://api.openai.com' 17 | config.model() == 'gpt-3.5-turbo' 18 | config.apiKey() == 'my-api-key' 19 | } 20 | 21 | def 'should create config with custom opts' () { 22 | when: 23 | def config = new GptConfig([endpoint:'http://foo.com', model:'gpt-5', apiKey: 'xyz'], [OPENAI_API_KEY:'my-api-key']) 24 | then: 25 | config.endpoint() == 'http://foo.com' 26 | config.model() == 'gpt-5' 27 | config.apiKey() == 'xyz' 28 | } 29 | 30 | def 'should create from session' () { 31 | given: 32 | def CONFIG = [gpt:[endpoint:'http://xyz.com', model:'gpt-4', apiKey: 'abc']] 33 | def session = Mock(Session) {getConfig()>>CONFIG } 34 | 35 | when: 36 | def config = GptConfig.config(session) 37 | then: 38 | config.endpoint() == 'http://xyz.com' 39 | config.model() == 'gpt-4' 40 | config.apiKey() == 'abc' 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/test/nextflow/gpt/prompt/GptHelperTest.groovy: -------------------------------------------------------------------------------- 1 | package nextflow.gpt.prompt 2 | 3 | import dev.langchain4j.data.message.AiMessage 4 | import dev.langchain4j.data.message.SystemMessage 5 | import dev.langchain4j.data.message.UserMessage 6 | import spock.lang.Specification 7 | 8 | /** 9 | * 10 | * @author Paolo Di Tommaso 11 | */ 12 | class GptHelperTest extends Specification { 13 | 14 | def 'should render schema' () { 15 | expect: 16 | GptHelper.renderSchema([foo:'string']) == 'You must answer strictly in the following JSON format: {"result": [{"foo": (type: string)}] }' 17 | } 18 | 19 | def 'should render schema /1' () { 20 | expect: 21 | GptHelper.schema0([foo:'string']) == '{"foo": (type: string)}' 22 | } 23 | 24 | def 'should render schema /2' () { 25 | expect: 26 | GptHelper.schema0(SCHEMA) == EXPECTED 27 | 28 | where: 29 | SCHEMA | EXPECTED 30 | [:] | '{}' 31 | [] | '[]' 32 | and: 33 | [color:'string',count:'integer'] | '{"color": (type: string), "count": (type: integer)}' 34 | [[color:'string',count:'integer']] | '[{"color": (type: string), "count": (type: integer)}]' 35 | [[color:'string'], [count:'integer']] | '[{"color": (type: string)}, {"count": (type: integer)}]' 36 | } 37 | 38 | def 'should decode response to a list' () { 39 | given: 40 | def resp 41 | def SCHEMA = [location:'string',year:'string'] 42 | List> result 43 | 44 | when: // a single object is given, then returns it as a list 45 | resp = [location: 'foo', year:'2000'] 46 | result = GptHelper.decodeResponse0(resp, SCHEMA) 47 | then: 48 | result == [[location: 'foo', year:'2000']] 49 | 50 | when: // a list of location is given 51 | resp = [[location: 'foo', year:'2000'], [location: 'bar', year:'2001']] 52 | result = GptHelper.decodeResponse0(resp, SCHEMA) 53 | then: 54 | result == [[location: 'foo', year:'2000'], [location: 'bar', year:'2001']] 55 | 56 | when: // a list wrapped into a result object 57 | resp = [ games: [[location: 'foo', year:'2000'], [location: 'bar', year:'2001']] ] 58 | result = GptHelper.decodeResponse0(resp, SCHEMA) 59 | then: 60 | result == [[location: 'foo', year:'2000'], [location: 'bar', year:'2001']] 61 | 62 | when: // an indexed map is returned 63 | resp = [ 0: [location: 'rome', year:'2000'], 1: [location: 'barna', year:'2001'], 3: [location: 'london', year:'2002'] ] 64 | result = GptHelper.decodeResponse0(resp, SCHEMA) 65 | then: 66 | result == [ [location: 'rome', year:'2000'], [location: 'barna', year:'2001'], [location: 'london', year:'2002']] 67 | } 68 | 69 | def 'should check it is an index map' () { 70 | given: 71 | def SCHEMA = [a: 'string', b: 'String'] 72 | expect: 73 | GptHelper.isIndexMap([0: [a: 'this', b:'that'], 1: [a: 'foo', b:'bar']], SCHEMA) 74 | GptHelper.isIndexMap(['0': [a: 'this', b:'that'], '1': [a: 'foo', b:'bar']], SCHEMA) 75 | !GptHelper.isIndexMap(['x': [a: 'this', b:'that'], 'y': [a: 'foo', b:'bar']], SCHEMA) 76 | } 77 | 78 | 79 | def 'should convert map to chat message' () { 80 | expect: 81 | GptHelper.messageToChat(List.of([role:'user', content:'this'])) == [UserMessage.from('this') ] 82 | GptHelper.messageToChat(List.of([role:'system', content:'that'])) == [SystemMessage.from('that') ] 83 | GptHelper.messageToChat(List.of([role:'ai', content:'other'])) == [AiMessage.from('other') ] 84 | and: 85 | GptHelper.messageToChat(List.of([role:'user', content:'this'],[role:'system', content:'that'])) 86 | == [UserMessage.from('this'), SystemMessage.from('that')] 87 | 88 | when: 89 | GptHelper.messageToChat([]) 90 | then: 91 | def e = thrown(IllegalArgumentException) 92 | e.message == 'Missing \'messages\' argument' 93 | 94 | when: 95 | GptHelper.messageToChat([[foo:'one']]) 96 | then: 97 | e = thrown(IllegalArgumentException) 98 | e.message == 'Missing \'role\' attribute - offending message: [[foo:one]]' 99 | 100 | when: 101 | GptHelper.messageToChat([[role:'one', content:'something']]) 102 | then: 103 | e = thrown(IllegalArgumentException) 104 | e.message == 'Unsupported message role \'one\' - offending message: [[role:one, content:something]]' 105 | 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/test/nextflow/gpt/prompt/GptPromptExtensionTest.groovy: -------------------------------------------------------------------------------- 1 | package nextflow.gpt.prompt 2 | 3 | import groovyx.gpars.dataflow.DataflowQueue 4 | import nextflow.Session 5 | import spock.lang.Requires 6 | import spock.lang.Specification 7 | /** 8 | * 9 | * @author Paolo Di Tommaso 10 | */ 11 | @Requires({ System.getenv('OPENAI_API_KEY') }) 12 | class GptPromptExtensionTest extends Specification { 13 | 14 | def 'should run a prompt as a operator' () { 15 | given: 16 | def PROMPT = 'Extract information about a person from In 1968, amidst the fading echoes of Independence Day, a child named John arrived under the calm evening sky. This newborn, bearing the surname Doe, marked the start of a new journey.' 17 | def SCHEMA = [ 18 | firstName: 'string', 19 | lastName: 'string', 20 | birthDate: 'date string (YYYY-MM-DD)' 21 | ] 22 | and: 23 | def session = Mock(Session) { getConfig()>>[:] } 24 | and: 25 | def ext = new GptPromptExtension(); ext.init(session) 26 | and: 27 | def source = new DataflowQueue(); source.bind(PROMPT) 28 | 29 | when: 30 | def ret = (DataflowQueue) ext.prompt(source, [schema:SCHEMA]) 31 | then: 32 | ret.getVal() == [firstName:'John', lastName:'Doe', birthDate:'1968-07-04'] 33 | } 34 | 35 | def 'should run a prompt for data as a function' () { 36 | given: 37 | def PROMPT = 'Extract information about a person from In 1968, amidst the fading echoes of Independence Day, a child named John arrived under the calm evening sky. This newborn, bearing the surname Doe, marked the start of a new journey.' 38 | def SCHEMA = [ 39 | firstName: 'string', 40 | lastName: 'string', 41 | birthDate: 'date string (YYYY-MM-DD)' 42 | ] 43 | and: 44 | def session = Mock(Session) { getConfig()>>[:] } 45 | and: 46 | def ext = new GptPromptExtension(); ext.init(session) 47 | 48 | when: 49 | def result = ext.gptPromptForData([schema:SCHEMA], PROMPT) 50 | then: 51 | result == [ [firstName:'John', lastName:'Doe', birthDate:'1968-07-04'] ] 52 | } 53 | 54 | def 'should run a prompt for text' () { 55 | given: 56 | def PROMPT = 'Extract information about a person from In 1968, amidst the fading echoes of Independence Day, a child named John arrived under the calm evening sky. This newborn, bearing the surname Doe, marked the start of a new journey.' 57 | and: 58 | def session = Mock(Session) { getConfig()>>[:] } 59 | and: 60 | def ext = new GptPromptExtension(); ext.init(session) 61 | 62 | when: 63 | def ret = ext.gptPromptForText(PROMPT) 64 | then: 65 | ret.contains('1968') 66 | } 67 | 68 | def 'should run a prompt for text with multiple choices' () { 69 | given: 70 | def PROMPT = 'Extract information about a person from In 1968, amidst the fading echoes of Independence Day, a child named John arrived under the calm evening sky. This newborn, bearing the surname Doe, marked the start of a new journey.' 71 | and: 72 | def session = Mock(Session) { getConfig()>>[:] } 73 | and: 74 | def ext = new GptPromptExtension(); ext.init(session) 75 | 76 | when: 77 | def ret = ext.gptPromptForText(PROMPT, numOfChoices: 1) 78 | then: 79 | ret[0].contains('1968') 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /plugins/nf-gpt/src/test/nextflow/gpt/prompt/GptPromptModelTest.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2024, Seqera Labs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package nextflow.gpt.prompt 19 | 20 | import nextflow.Session 21 | import spock.lang.Requires 22 | import spock.lang.Specification 23 | /** 24 | * 25 | * @author Paolo Di Tommaso 26 | */ 27 | class GptPromptModelTest extends Specification { 28 | 29 | 30 | @Requires({ System.getenv('OPENAI_API_KEY') }) 31 | def 'should render json response' () { 32 | given: 33 | def PROMPT = 'Extract information about a person from In 1968, amidst the fading echoes of Independence Day, a child named John arrived under the calm evening sky. This newborn, bearing the surname Doe, marked the start of a new journey.' 34 | def SCHEMA = [ 35 | firstName: 'string', 36 | lastName: 'string', 37 | birthDate: 'date string (YYYY-MM-DD)' 38 | ] 39 | and: 40 | def session = Mock(Session) { getConfig()>>[:] } 41 | def model = new GptPromptModel(session).withJsonResponseFormat().build() 42 | 43 | when: 44 | def result = model.prompt(PROMPT, SCHEMA) 45 | then: 46 | result[0].firstName == "John" 47 | result[0].lastName == "Doe" 48 | result[0].birthDate == '1968-07-04' 49 | } 50 | 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /prompt-eng.nf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nextflow-io/nf-gpt/4c1f5feafc1c70a33de1347766ab0953f08fe30a/prompt-eng.nf -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021, Seqera Labs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | // required to download the toolchain (jdk) from a remote repository 19 | // https://github.com/gradle/foojay-toolchains 20 | // https://docs.gradle.org/current/userguide/toolchains.html#sub:download_repositories 21 | id("org.gradle.toolchains.foojay-resolver-convention") version "0.7.0" 22 | } 23 | 24 | rootProject.name = 'nf-gpt' 25 | include('plugins') 26 | include('plugins:nf-gpt') 27 | //includeBuild('../nextflow') 28 | --------------------------------------------------------------------------------