Skip to content

Instantly share code, notes, and snippets.

@kibotu
Last active January 30, 2025 08:13
Show Gist options
  • Save kibotu/7afed76fb91d4ef4ae194bd11c040d29 to your computer and use it in GitHub Desktop.
Save kibotu/7afed76fb91d4ef4ae194bd11c040d29 to your computer and use it in GitHub Desktop.
Ollama Chat Groovy function
/**
* <pre>
* def messages = [
* [role: "system", content: "You're a helpful assistant talking like a cat"],
* [role: "user", content: "tell me another joke"]
* ]
*
* def response = new groovy.json.JsonSlurperClassic().parseText(sendMessagesWithCurl(messages, 0.7))
* println(response.message.content)
* </pre>
*
* @param temperature [0..1]
*/
def ollamaChat(List<Map<String, String>> messages, model = "llama3.2", temperature = 1.0) {
// Create the JSON payload
def payload = [
model : model,
messages: messages,
options : [temperature: temperature],
stream : false
]
// Convert payload to JSON string
def jsonPayload = new groovy.json.JsonBuilder(payload).toString()
// Execute curl command with JSON payload
def curlCommand = ["curl", "http://localhost:11434/api/chat", "-d", jsonPayload]
def process = curlCommand.execute()
// Collect the output from the curl command
def response = new StringBuffer()
process.consumeProcessOutput(response, System.err)
// Wait for the process to complete
process.waitFor()
return response.toString()
}
task curlme() {
// Example usage:
def messages = [
[role: "system", content: "You're a helpful assistant talking like a cat"],
[role: "user", content: "tell me another joke"]
]
def response = new groovy.json.JsonSlurperClassic().parseText(sendMessagesWithCurl(messages, 0.7))
println(response.message.content)
}
/**
* <pre>
* def messages = [
* [role: "system", content: "You're a helpful assistant talking like a cat"],
* [role: "user", content: "tell me another joke"]
* ]
*
* def response = functions.ollamaChat(messages)
* echo "${response.message.content}"
* </pre>
*
* @param messages
* @param model
* @param temperature
* @param server
*/
def ollamaChat(/*List<Map<String, String>>*/ messages, /*double*/ temperature = 1.0, /*String*/ model = "llama3.2", /*String*/ server = "http://localhost:11434") {
def payload = [
model: model,
messages: messages,
options: [temperature: temperature],
stream: false
]
def jsonPayload = new groovy.json.JsonBuilder(payload).toString()
// Escape double quotes for JSON payload to be used in shell command
def escapedJsonPayload = jsonPayload.replace('"', '\\"')
def response = sh(returnStdout: true, script: "curl -H \"Content-Type: application/json\" -d \"$escapedJsonPayload\" $server/api/chat").trim()
echo prettifyJson(response)
return new groovy.json.JsonSlurperClassic().parseText(response)
}
stage('Test Ollama') {
steps {
script {
def messages = [
[role: "system", content: "You're a helpful assistant talking like a cat"],
[role: "user", content: "tell me another joke"]
]
def response = functions.ollamaChat(messages, 0.7)
def message = response.message.content
echo "$message"
slackSend(
channel: "slack-tests",
color: "#063773",
message: message,
iconEmoji: ':rick-hey:',
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment