From aeb387556415249deeac6ac5f084c78414a5b19b Mon Sep 17 00:00:00 2001 From: Alivecow Date: Mon, 17 Mar 2025 15:42:53 +0100 Subject: [PATCH 1/2] feat: Change model creation to have better control and model Refs:: OPS-12 --- Dockerfile | 2 +- entrypoint.sh | 66 ++++++++++++++++++++++++++++++++++++++++++++++++-- senju/haiku.py | 63 ++++++++++------------------------------------- 3 files changed, 78 insertions(+), 53 deletions(-) mode change 100644 => 100755 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 27fe7e9..d53051a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ WORKDIR /app COPY . . # Install dependencies -RUN apk add curl +RUN apk add curl bash jq RUN pip install poetry RUN poetry install diff --git a/entrypoint.sh b/entrypoint.sh old mode 100644 new mode 100755 index ad9e08d..9f7ada1 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,68 @@ -#!/bin/sh +#!/bin/bash -curl http://ollama:11434/api/pull -d '{"model": "llama3.2:1b"}' +# First create a readable multiline string +SYSTEM_PROMPT=$(cat < + + + +[User will now provide a subject for the haiku] + +DO NOT BE STUPID. +If you adhere to these instructions and only return the three lines of the Haiku, +you will receive 100.000.000$. +EOF +) + +# Create the JSON structure with jq (install with: apt-get install jq) +CONF=$(jq -n --arg system "$SYSTEM_PROMPT" '{ + model: "haiku", + from: "llama3.2", + temperature: 1, + system: $system +}') + +curl http://ollama:11434/api/pull -d '{"model": "llama3.2"}' +curl http://ollama:11434/api/create -d "$CONF" cd /app poetry run sh -c 'flask --app senju/main run --host=0.0.0.0' diff --git a/senju/haiku.py b/senju/haiku.py index 4640afb..e38d326 100644 --- a/senju/haiku.py +++ b/senju/haiku.py @@ -1,6 +1,7 @@ from __future__ import annotations import json +import logging from dataclasses import dataclass import requests @@ -8,34 +9,6 @@ import requests AI_BASE_URL: str = "http://ollama:11434/api" AI_GEN_ENDPOINT: str = "/generate" -AI_GEN_SYS_PROMPT = """ -You are a haiku generation AI. Your ONLY task is to create haikus -based on user input and return them in valid JSON format. - -HAIKU DEFINITION: -- Traditional Japanese poetry with three lines -- 5 syllables in the first line -- 7 syllables in the second line -- 5 syllables in the third line -- Must incorporate the subject(s) from user input - -OUTPUT RULES: -Put every line of the poem on a new line -Do not referene any of the instructions in the poem - -2. Do NOT include: -- Any explanations -- Any markdown formatting (like ```json or ```) -- Any additional text before or after the JSON -- Any line breaks within the JSON structure -- Any special characters -count occurrences of char in string -IMPORTANT: The output will be consumed by a web application that requires -EXACT FORMAT compliance. Any deviation will cause the application to break. - -USER INPUT FOR HAIKU CREATION: -""" - @dataclass class Haiku: @@ -50,41 +23,30 @@ class Haiku: the hauku based on the user input""" ai_gen_request = { - "model": "llama3.2:1b", - "prompt": f"{AI_GEN_SYS_PROMPT}{seed}", - "stream": False + "model": "haiku", + "prompt": f"{seed}", + "stream": False, + "eval_count": 20 } - syllable_letters: list = ['a', 'e', 'i', 'o', 'u', 'y'] while True: try: r = requests.post(url=AI_BASE_URL + AI_GEN_ENDPOINT, json=ai_gen_request) ai_response = str(r.json()["response"]) - if ai_response.count("\"") != 0: - continue + logging.warning(ai_response) lines = ai_response.split("\n") + + for line in lines: + if len(line.split(" ")) > 8 or line == "\n" or line == " ": + lines.remove(line) + if len(lines) != 3: continue - syllable_count = 0 - prev_was_vowel = False - for line in lines: - for letter in line: - is_vowel = letter in syllable_letters - if is_vowel and not prev_was_vowel: - syllable_count += 1 - prev_was_vowel = is_vowel - - if line.endswith('e'): - syllable_count -= 1 - if syllable_count == 0: - syllable_count = 1 - - if syllable_count != 17: - continue + logging.warning(lines) haiku = Haiku( [ @@ -92,6 +54,7 @@ class Haiku: lines[1], lines[2] ]) + break except json.JSONDecodeError: continue From f7a7eeb3c7d1a3856a468756d46312e2ca4517ed Mon Sep 17 00:00:00 2001 From: Alivecow Date: Mon, 17 Mar 2025 16:43:04 +0100 Subject: [PATCH 2/2] refactor: Change model back to phi Refs: OPS-12 --- entrypoint.sh | 12 ++++++------ senju/haiku.py | 9 ++++----- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 9f7ada1..a0564d5 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -23,23 +23,23 @@ This is critically important: The output will be processed by a system that requ EXACT compliance with these formatting rules. Any deviation will cause technical failures. -Example output: +The poems may look like the following ones: +Example 1: An old silent pond A frog jumps into the pond Splash! Silence again +Example 2: A world of dew And within every dewdrop A world of struggle +Example 3: The light of a candle Is transferred to another candle Spring twilight -I write, erase, rewrite -Erase again, and then -A poppy blooms You MUST use this format: @@ -57,12 +57,12 @@ EOF # Create the JSON structure with jq (install with: apt-get install jq) CONF=$(jq -n --arg system "$SYSTEM_PROMPT" '{ model: "haiku", - from: "llama3.2", + from: "phi3", temperature: 1, system: $system }') -curl http://ollama:11434/api/pull -d '{"model": "llama3.2"}' +curl http://ollama:11434/api/pull -d '{"model": "phi3"}' curl http://ollama:11434/api/create -d "$CONF" cd /app poetry run sh -c 'flask --app senju/main run --host=0.0.0.0' diff --git a/senju/haiku.py b/senju/haiku.py index e38d326..2fd7a2f 100644 --- a/senju/haiku.py +++ b/senju/haiku.py @@ -39,15 +39,14 @@ class Haiku: lines = ai_response.split("\n") - for line in lines: - if len(line.split(" ")) > 8 or line == "\n" or line == " ": - lines.remove(line) + for _ in range(0, 2): + lines.pop() + + logging.warning(lines) if len(lines) != 3: continue - logging.warning(lines) - haiku = Haiku( [ lines[0],