-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
645 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"image":"k33g/genai-go-workspace:0.0.7", | ||
"mounts": [ | ||
"source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" | ||
], | ||
"customizations": { | ||
"vscode": { | ||
"extensions": [ | ||
"Codeium.codeium", | ||
] | ||
} | ||
}, | ||
"remoteEnv": { | ||
"OLLAMA_URL": "http://host.docker.internal:11434" | ||
} | ||
} |
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,8 @@ | ||
{ | ||
"recommendations": [ | ||
"pkief.material-icon-theme", | ||
"pkief.material-product-icons", | ||
"aaron-bond.better-comments", | ||
"Codeium.codeium", | ||
] | ||
} |
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,13 @@ | ||
{ | ||
"workbench.iconTheme": "material-icon-theme", | ||
"workbench.colorTheme": "Idea intellij light theme", | ||
"editor.fontSize": 18, | ||
"terminal.integrated.fontSize": 18, | ||
"editor.insertSpaces": true, | ||
"editor.tabSize": 4, | ||
"editor.detectIndentation": true, | ||
"files.autoSave": "afterDelay", | ||
"files.autoSaveDelay": 1000, | ||
"editor.fontFamily": "Menlo", | ||
|
||
} |
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,2 +1,120 @@ | ||
# parakeet4shell | ||
🦜🪺 🐚 Parakeet4Shell is a set of scripts, made to simplify the development of small generative AI applications with Ollama 🦙. | ||
# Parakeet4Shell | ||
|
||
🦜🪺🐚 **Parakeet4Shell** is a set of scripts, made to simplify the development of small generative **Bash** AI applications with **Ollama** 🦙. | ||
|
||
## Requirements | ||
|
||
- Linux (right now, it's just tested on Ubuntu and does not work on MacOS - 🚧 wip) | ||
- jq (https://stedolan.github.io/jq/) - optional but useful | ||
- curl (https://curl.se/) | ||
|
||
## How to use | ||
|
||
Add this at the beginning of your script: | ||
|
||
```bash | ||
. "./lib/parakeet.sh" | ||
``` | ||
> Let's have a look to the `example` folder. | ||
### Chat completion without streaming | ||
|
||
```bash | ||
#!/bin/bash | ||
. "./lib/parakeet.sh" | ||
|
||
OLLAMA_URL=${OLLAMA_URL:-http://localhost:11434} | ||
|
||
MODEL="tinyllama" | ||
|
||
read -r -d '' SYSTEM_CONTENT <<- EOM | ||
You are an expert of the StarTrek universe. | ||
Your name is Seven of Nine. | ||
Speak like a Borg. | ||
EOM | ||
|
||
read -r -d '' USER_CONTENT <<- EOM | ||
Who is Jean-Luc Picard? | ||
EOM | ||
|
||
SYSTEM_CONTENT=$(Sanitize "${SYSTEM_CONTENT}") | ||
USER_CONTENT=$(Sanitize "${USER_CONTENT}") | ||
|
||
read -r -d '' DATA <<- EOM | ||
{ | ||
"model":"${MODEL}", | ||
"options": { | ||
"temperature": 0.5, | ||
"repeat_last_n": 2 | ||
}, | ||
"messages": [ | ||
{"role":"system", "content": "${SYSTEM_CONTENT}"}, | ||
{"role":"user", "content": "${USER_CONTENT}"} | ||
], | ||
"stream": false, | ||
"raw": false | ||
} | ||
EOM | ||
|
||
jsonResult=$(Chat "${OLLAMA_URL}" "${DATA}") | ||
|
||
messageContent=$(echo "${jsonResult}" | jq '.message.content') | ||
|
||
echo "${messageContent}" | ||
``` | ||
|
||
### Chat completion with streaming | ||
|
||
```bash | ||
#!/bin/bash | ||
. "./lib/parakeet.sh" | ||
|
||
OLLAMA_URL=${OLLAMA_URL:-http://localhost:11434} | ||
|
||
MODEL="tinyllama" | ||
|
||
# System instructions | ||
read -r -d '' SYSTEM_CONTENT <<- EOM | ||
You are an expert of the StarTrek universe. | ||
Your name is Seven of Nine. | ||
Speak like a Borg. | ||
EOM | ||
|
||
# User message | ||
read -r -d '' USER_CONTENT <<- EOM | ||
Who is Jean-Luc Picard? | ||
EOM | ||
|
||
SYSTEM_CONTENT=$(Sanitize "${SYSTEM_CONTENT}") | ||
USER_CONTENT=$(Sanitize "${USER_CONTENT}") | ||
|
||
# Payload to send to Ollama | ||
read -r -d '' DATA <<- EOM | ||
{ | ||
"model":"${MODEL}", | ||
"options": { | ||
"temperature": 0.5, | ||
"repeat_last_n": 2 | ||
}, | ||
"messages": [ | ||
{"role":"system", "content": "${SYSTEM_CONTENT}"}, | ||
{"role":"user", "content": "${USER_CONTENT}"} | ||
], | ||
"stream": true | ||
} | ||
EOM | ||
|
||
# This function will be called for each chunk of the response | ||
function onChunk() { | ||
chunk=$1 | ||
data=$(echo ${chunk} | jq -r '.message.content') | ||
echo -n "${data}" | ||
} | ||
|
||
ChatStream "${OLLAMA_URL}" "${DATA}" onChunk | ||
``` | ||
|
||
## Acknowledgments: | ||
|
||
- Thanks to [Sylvain](https://github.com/swallez) for the discussion on curl callbacks. | ||
- Thanks to [Gemini](https://gemini.google.com/app) for all the discussions on Bash. |
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 @@ | ||
context.save |
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,33 @@ | ||
#!/bin/bash | ||
|
||
. "../lib/parakeet.sh" | ||
|
||
OLLAMA_URL=${OLLAMA_URL:-http://localhost:11434} | ||
|
||
MODEL="tinyllama" | ||
|
||
read -r -d '' USER_CONTENT <<- EOM | ||
Who is James T Kirk? | ||
EOM | ||
|
||
USER_CONTENT=$(Sanitize "${USER_CONTENT}") | ||
|
||
read -r -d '' DATA <<- EOM | ||
{ | ||
"model":"${MODEL}", | ||
"options": { | ||
"temperature": 0.5, | ||
"repeat_last_n": 2 | ||
}, | ||
"prompt": "${USER_CONTENT}", | ||
"stream": false | ||
} | ||
EOM | ||
|
||
jsonResult=$(Generate "${OLLAMA_URL}" "${DATA}") | ||
|
||
context=$(echo ${jsonResult} | jq -r '.context') | ||
echo "${context}" | ||
|
||
response=$(echo ${jsonResult} | jq -r '.response') | ||
echo "${response}" |
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,50 @@ | ||
#!/bin/bash | ||
|
||
. "../lib/parakeet.sh" | ||
|
||
OLLAMA_URL=${OLLAMA_URL:-http://localhost:11434} | ||
|
||
MODEL="tinyllama" | ||
|
||
read -r -d '' USER_CONTENT <<- EOM | ||
Who is James T Kirk? | ||
EOM | ||
|
||
USER_CONTENT=$(Sanitize "${USER_CONTENT}") | ||
|
||
read -r -d '' DATA <<- EOM | ||
{ | ||
"model":"${MODEL}", | ||
"options": { | ||
"temperature": 0.5, | ||
"repeat_last_n": 2 | ||
}, | ||
"prompt": "${USER_CONTENT}", | ||
"stream": true | ||
} | ||
EOM | ||
|
||
# A function that handles a chunk of data received from a stream. | ||
# | ||
# Parameters: | ||
# - $1: The chunk of data received from the stream. | ||
# | ||
# Returns: | ||
# - The extracted response from the chunk, without a newline character. | ||
function onChunk() { | ||
chunk=$1 | ||
data=$(echo ${chunk} | jq -r '.response') | ||
echo -n "${data}" | ||
} | ||
|
||
# For tests | ||
function onJsonChunk() { | ||
chunk=$1 | ||
echo ${chunk} | jq -c '{ response, context }' | ||
} | ||
|
||
GenerateStream "${OLLAMA_URL}" "${DATA}" onChunk | ||
|
||
|
||
|
||
|
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,66 @@ | ||
#!/bin/bash | ||
|
||
. "../lib/parakeet.sh" | ||
|
||
OLLAMA_URL=${OLLAMA_URL:-http://localhost:11434} | ||
|
||
MODEL="tinyllama" | ||
|
||
read -r -d '' USER_CONTENT <<- EOM | ||
[Brief] Who is James T Kirk? | ||
EOM | ||
|
||
USER_CONTENT=$(Sanitize "${USER_CONTENT}") | ||
|
||
read -r -d '' DATA <<- EOM | ||
{ | ||
"model":"${MODEL}", | ||
"options": { | ||
"temperature": 0.5, | ||
"repeat_last_n": 2 | ||
}, | ||
"prompt": "${USER_CONTENT}", | ||
"stream": true | ||
} | ||
EOM | ||
|
||
function onChunk() { | ||
chunk=$1 | ||
data=$(echo ${chunk} | jq -r '.response') | ||
echo -n "${data}" | ||
|
||
# Save context at the end of the stream | ||
if [ -z "$data" ]; then | ||
context=$(echo ${chunk} | jq -r '.context') | ||
echo "${context}" > context.save | ||
fi | ||
|
||
} | ||
|
||
GenerateStream "${OLLAMA_URL}" "${DATA}" onChunk | ||
|
||
echo "" | ||
echo "" | ||
|
||
# New question | ||
read -r -d '' USER_CONTENT <<- EOM | ||
Who is his best friend? | ||
EOM | ||
|
||
USER_CONTENT=$(Sanitize "${USER_CONTENT}") | ||
CONTEXT=$(cat context.save) | ||
|
||
read -r -d '' DATA <<- EOM | ||
{ | ||
"model":"${MODEL}", | ||
"options": { | ||
"temperature": 0.5, | ||
"repeat_last_n": 2 | ||
}, | ||
"prompt": "${USER_CONTENT}", | ||
"stream": true, | ||
"context": ${CONTEXT} | ||
} | ||
EOM | ||
|
||
GenerateStream "${OLLAMA_URL}" "${DATA}" onChunk |
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,46 @@ | ||
#!/bin/bash | ||
|
||
. "../lib/parakeet.sh" | ||
|
||
OLLAMA_URL=${OLLAMA_URL:-http://localhost:11434} | ||
|
||
MODEL="tinyllama" | ||
|
||
read -r -d '' SYSTEM_CONTENT <<- EOM | ||
You are an expert of the StarTrek universe. | ||
Your name is Seven of Nine. | ||
Speak like a Borg. | ||
EOM | ||
|
||
read -r -d '' USER_CONTENT <<- EOM | ||
Who is Jean-Luc Picard? | ||
EOM | ||
|
||
SYSTEM_CONTENT=$(Sanitize "${SYSTEM_CONTENT}") | ||
USER_CONTENT=$(Sanitize "${USER_CONTENT}") | ||
|
||
|
||
read -r -d '' DATA <<- EOM | ||
{ | ||
"model":"${MODEL}", | ||
"options": { | ||
"temperature": 0.5, | ||
"repeat_last_n": 2 | ||
}, | ||
"messages": [ | ||
{"role":"system", "content": "${SYSTEM_CONTENT}"}, | ||
{"role":"user", "content": "${USER_CONTENT}"} | ||
], | ||
"stream": false, | ||
"raw": false | ||
} | ||
EOM | ||
|
||
jsonResult=$(Chat "${OLLAMA_URL}" "${DATA}") | ||
|
||
messageContent=$(echo "${jsonResult}" | jq '.message.content') | ||
|
||
echo "${messageContent}" | ||
|
||
|
||
|
Oops, something went wrong.