-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This version refactor the plugin introducing two new extension functions - gptPromptForText: submit a gpt prompt and return the response as a text - gptPromptForData: submit a gpt prompt and return the reponse a structured data matching the specified schema Signed-off-by: Paolo Di Tommaso <[email protected]>
- Loading branch information
1 parent
6b3584b
commit ddd85e7
Showing
23 changed files
with
1,217 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
include { prompt } from 'plugin/nf-gpt' | ||
include { gptPromptForText } from 'plugin/nf-gpt' | ||
|
||
def text = ''' | ||
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. | ||
''' | ||
/* | ||
* This example show how to use the `gptPromptForText` function in the map operator | ||
*/ | ||
|
||
channel | ||
.of(text) | ||
.prompt(schema: [firstName: 'string', lastName: 'string', birthDate: 'date (YYYY-MM-DD)']) | ||
.of('Tell me joke') | ||
.map { gptPromptForText(it) } | ||
.view() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
include { prompt } from 'plugin/nf-gpt' | ||
include { gptPromptForText } from 'plugin/nf-gpt' | ||
|
||
def query = ''' | ||
Who won most gold medals in swimming and Athletics categories during Barcelona 1992 and London 2012 olympic games?" | ||
''' | ||
/* | ||
* This example show how to use the `gptPromptForText` function in a process | ||
*/ | ||
|
||
channel .of(query) | ||
.prompt(schema: [athlete: 'string', numberOfMedals: 'number', location:'string', sport:'string']) | ||
.view() | ||
process prompt { | ||
input: | ||
val query | ||
output: | ||
val response | ||
exec: | ||
response = gptPromptForText(query) | ||
} | ||
|
||
workflow { | ||
prompt('Tell me a joke') | view | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
include { prompt } from 'plugin/nf-gpt' | ||
include { gptPromptForData } from 'plugin/nf-gpt' | ||
|
||
/** | ||
* This example show how to perform a GPT prompt and map the response to a structured object | ||
*/ | ||
|
||
def text = ''' | ||
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. | ||
''' | ||
|
||
channel | ||
.fromList(['Barcelona, 1992', 'London, 2012']) | ||
.combine(['Swimming', 'Athletics']) | ||
.prompt(schema: [athlete: 'string', numberOfMedals: 'number', location: 'string', sport: 'string']) { edition, sport -> | ||
"Who won most gold medals in $sport category during $edition olympic games?" | ||
} | ||
.view() | ||
.of(text) | ||
.flatMap { gptPromptForData(it, schema: [firstName: 'string', lastName: 'string', birthDate: 'date (YYYY-MM-DD)']) } | ||
.view() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
include { gptPromptForData } from 'plugin/nf-gpt' | ||
|
||
/** | ||
* This example show how to perform a GPT prompt and map the response to a structured object | ||
*/ | ||
|
||
|
||
def query = ''' | ||
Who won most gold medals in swimming and Athletics categories during Barcelona 1992 and London 2012 olympic games?" | ||
''' | ||
|
||
def RECORD = [athlete: 'string', numberOfMedals: 'number', location:'string', sport:'string'] | ||
|
||
channel .of(query) | ||
.flatMap { gptPromptForData(it, schema:RECORD, temperature: 2d) } | ||
.view() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
include { gptPromptForData } from 'plugin/nf-gpt' | ||
|
||
/** | ||
* This example show how to perform multiple GPT prompts using combine and flatMap operators | ||
*/ | ||
|
||
|
||
channel | ||
.fromList(['Barcelona, 1992', 'London, 2012']) | ||
.combine(['Swimming', 'Athletics']) | ||
.flatMap { edition, sport -> | ||
gptPromptForData( | ||
"Who won most gold medals in $sport category during $edition olympic games?", | ||
schema: [athlete: 'string', numberOfMedals: 'number', location: 'string', sport: 'string']) | ||
} | ||
.view() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
plugins/nf-gpt/src/main/nextflow/gpt/client/GptChatCompletionRequest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright 2013-2024, Seqera Labs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package nextflow.gpt.client | ||
|
||
import groovy.transform.Canonical | ||
import groovy.transform.CompileStatic | ||
import groovy.transform.ToString | ||
|
||
/** | ||
* Model a GTP chat conversation create request object. | ||
* | ||
* See also | ||
* https://platform.openai.com/docs/api-reference/chat/create | ||
* | ||
* @author Paolo Di Tommaso <[email protected]> | ||
*/ | ||
@CompileStatic | ||
@ToString(includePackage = false, includeNames = true) | ||
class GptChatCompletionRequest { | ||
|
||
@ToString(includePackage = false, includeNames = true) | ||
@CompileStatic | ||
static class Message { | ||
String role | ||
String content | ||
} | ||
|
||
@ToString(includePackage = false, includeNames = true) | ||
@CompileStatic | ||
static class ToolMessage extends Message { | ||
String name | ||
String tool_call_id | ||
} | ||
|
||
@ToString(includePackage = false, includeNames = true) | ||
@CompileStatic | ||
static class Tool { | ||
String type | ||
Function function | ||
} | ||
|
||
@ToString(includePackage = false, includeNames = true) | ||
@CompileStatic | ||
static class Function { | ||
String name | ||
String description | ||
Parameters parameters | ||
} | ||
|
||
@ToString(includePackage = false, includeNames = true) | ||
@CompileStatic | ||
static class Parameters { | ||
String type | ||
Map<String,Param> properties | ||
List<String> required | ||
} | ||
|
||
@ToString(includePackage = false, includeNames = true) | ||
@CompileStatic | ||
static class Param { | ||
String type | ||
String description | ||
} | ||
|
||
@ToString(includePackage = false, includeNames = true) | ||
@CompileStatic | ||
@Canonical | ||
static class ResponseFormat { | ||
static final ResponseFormat TEXT = new ResponseFormat('text') | ||
static final ResponseFormat JSON = new ResponseFormat('json_object') | ||
final String type | ||
} | ||
|
||
/** | ||
* ID of the model to use. | ||
*/ | ||
String model | ||
|
||
/** | ||
* A list of tools the model may call | ||
*/ | ||
List<?> messages | ||
|
||
List<Tool> tools | ||
|
||
String tool_choice | ||
|
||
/** | ||
* The maximum number of tokens that can be generated in the chat completion | ||
*/ | ||
Integer max_tokens | ||
|
||
/** | ||
* 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 | ||
*/ | ||
Integer n | ||
|
||
/** | ||
* What sampling temperature to use, between 0 and 2 | ||
*/ | ||
Float temperature | ||
|
||
/** | ||
* Modify the likelihood of specified tokens appearing in the completion | ||
*/ | ||
Map logit_bias | ||
|
||
/** | ||
* Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is valid JSON. | ||
*/ | ||
ResponseFormat response_format | ||
} |
Oops, something went wrong.