Skip to content

Commit

Permalink
📦 🦜🪺🐚 Parakeet4Shell v0.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
k33g committed May 12, 2024
1 parent 225e579 commit d9da5e1
Show file tree
Hide file tree
Showing 14 changed files with 645 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .devcontainer.json
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"
}
}
8 changes: 8 additions & 0 deletions .vscode/extensions.json
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",
]
}
13 changes: 13 additions & 0 deletions .vscode/settings.json
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",

}
122 changes: 120 additions & 2 deletions README.md
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.
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
context.save
33 changes: 33 additions & 0 deletions examples/01-generate-completion.sh
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}"
50 changes: 50 additions & 0 deletions examples/02-generate-stream-completion.sh
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




66 changes: 66 additions & 0 deletions examples/03-use-context.sh
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
46 changes: 46 additions & 0 deletions examples/04-chat-completion.sh
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}"



Loading

0 comments on commit d9da5e1

Please sign in to comment.