|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "27b7ce09", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "print(\"Hello, World!\")" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "id": "a732832e", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "! pip install ollama\n", |
| 21 | + "! pip install pandas\n", |
| 22 | + "! pip install matplotlib\n", |
| 23 | + "! ollama pull gemma3:4b " |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "id": "0708e9c7", |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "from ollama import chat\n", |
| 34 | + "from ollama import ChatResponse\n", |
| 35 | + "\n", |
| 36 | + "response: ChatResponse = chat(model='gemma3:4b', messages=[\n", |
| 37 | + " {\n", |
| 38 | + " 'role': 'user',\n", |
| 39 | + " 'content': 'In which year the programming language Java was introduced?',\n", |
| 40 | + " }\n", |
| 41 | + "],\n", |
| 42 | + " options={\"temperature\":0.7}\n", |
| 43 | + ")\n", |
| 44 | + "print(response['message']['content'])\n" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "id": "09ccf29a", |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "!pip install llama-index llama-index-llms-ollama llama-index-embeddings-ollama pymupdf" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": null, |
| 60 | + "id": "5f9270d4", |
| 61 | + "metadata": {}, |
| 62 | + "outputs": [], |
| 63 | + "source": [ |
| 64 | + "import glob\n", |
| 65 | + "from pathlib import Path\n", |
| 66 | + "\n", |
| 67 | + "from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings\n", |
| 68 | + "from llama_index.llms.ollama import Ollama\n", |
| 69 | + "from llama_index.embeddings.ollama import OllamaEmbedding\n", |
| 70 | + "\n", |
| 71 | + "# --- 1) Locate the source code ---\n", |
| 72 | + "resources_dir = Path(\"../src/main/java/org/example/\")\n", |
| 73 | + "java_paths = sorted(glob.glob(str(resources_dir / \"*.java\")))\n", |
| 74 | + "if not java_paths:\n", |
| 75 | + " raise FileNotFoundError(\"No Java files found in ./input. Please place one there.\")\n", |
| 76 | + "\n", |
| 77 | + "# --- 2) Configure Ollama LLM and embedding model ---\n", |
| 78 | + "Settings.llm = Ollama(model=\"gemma3:4b\", request_timeout=120.0)\n", |
| 79 | + "Settings.embed_model = OllamaEmbedding(model_name=\"embeddinggemma\")\n", |
| 80 | + "\n", |
| 81 | + "# --- 3) Load and index the Java code with embeddings ---\n", |
| 82 | + "docs = SimpleDirectoryReader(input_files=[pdf_paths[0]]).load_data()\n", |
| 83 | + "index = VectorStoreIndex.from_documents(docs)\n", |
| 84 | + "\n", |
| 85 | + "# --- 4) Run a query ---\n", |
| 86 | + "query_engine = index.as_query_engine(similarity_top_k=3)\n", |
| 87 | + "prompt = \"Build three test cases to improve the mutation score of Main.java.\"\n", |
| 88 | + "response = query_engine.query(prompt)\n", |
| 89 | + "\n", |
| 90 | + "print(f\"Queried file: {Path(pdf_paths[0]).name}\")\n", |
| 91 | + "print(\"\\n=== Response ===\\n\")\n", |
| 92 | + "print(str(response))\n", |
| 93 | + "\n", |
| 94 | + "# --- 5) Append the test cases to the original file ---\n", |
| 95 | + "\n", |
| 96 | + "# Path to your test file\n", |
| 97 | + "test_file_path = Path(\"../src/test/java/org/example/MainTest.java\")\n", |
| 98 | + "\n", |
| 99 | + "# Read the existing test file\n", |
| 100 | + "with open(test_file_path, \"r\", encoding=\"utf-8\") as f:\n", |
| 101 | + " test_content = f.read()\n", |
| 102 | + "\n", |
| 103 | + "# Extract model output as text\n", |
| 104 | + "new_tests = str(response).strip()\n", |
| 105 | + "\n", |
| 106 | + "# Basic sanity: ensure the model didn’t include extra import/class definitions\n", |
| 107 | + "# If it only includes methods, we’ll inject them before the final closing brace\n", |
| 108 | + "if \"class \" not in new_tests and \"}\" in test_content:\n", |
| 109 | + " # Insert the test cases before the last closing brace\n", |
| 110 | + " test_content = test_content.rstrip()\n", |
| 111 | + " test_content = test_content[:-1] + \"\\n\\n \" + new_tests.replace(\"\\n\", \"\\n \") + \"\\n}\"\n", |
| 112 | + "else:\n", |
| 113 | + " # If the response includes a full class definition, just append it at the end\n", |
| 114 | + " test_content += \"\\n\\n\" + new_tests\n", |
| 115 | + "\n", |
| 116 | + "# Write the updated test file back\n", |
| 117 | + "with open(test_file_path, \"w\", encoding=\"utf-8\") as f:\n", |
| 118 | + " f.write(test_content)\n", |
| 119 | + "\n", |
| 120 | + "print(f\"✅ Appended new test cases to: {test_file_path}\")" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "code", |
| 125 | + "execution_count": null, |
| 126 | + "id": "72d21e6d", |
| 127 | + "metadata": {}, |
| 128 | + "outputs": [], |
| 129 | + "source": [ |
| 130 | + "!pip install ollama pillow" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": null, |
| 136 | + "id": "10b27818", |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [], |
| 139 | + "source": [ |
| 140 | + "# Describe an image with Gemma3:4b via Ollama\n", |
| 141 | + "\n", |
| 142 | + "\n", |
| 143 | + "import ollama\n", |
| 144 | + "from PIL import Image\n", |
| 145 | + "\n", |
| 146 | + "# --- 1) Path to your image ---\n", |
| 147 | + "image_path = \"./input/rag_simple.png\" # <-- replace with your image\n", |
| 148 | + "\n", |
| 149 | + "# Optional: preview image in the notebook\n", |
| 150 | + "display(Image.open(image_path))\n", |
| 151 | + "\n", |
| 152 | + "# --- 2) Send prompt + image to Ollama ---\n", |
| 153 | + "response = ollama.chat(\n", |
| 154 | + " model=\"gemma3:4b\",\n", |
| 155 | + " messages=[\n", |
| 156 | + " {\n", |
| 157 | + " \"role\": \"user\",\n", |
| 158 | + " \"content\": \"Given the image, build me an Agent AI workflow in Python using Ollama for testing Java software, including code coverage and mutation testing.\",\n", |
| 159 | + " \"images\": [image_path], # send the image to the model\n", |
| 160 | + " }\n", |
| 161 | + " ],\n", |
| 162 | + ")\n", |
| 163 | + "\n", |
| 164 | + "# --- 3) Print the description ---\n", |
| 165 | + "print(\"=== Image Description ===\")\n", |
| 166 | + "print(response[\"message\"][\"content\"])" |
| 167 | + ] |
| 168 | + } |
| 169 | + ], |
| 170 | + "metadata": { |
| 171 | + "kernelspec": { |
| 172 | + "display_name": "base", |
| 173 | + "language": "python", |
| 174 | + "name": "python3" |
| 175 | + }, |
| 176 | + "language_info": { |
| 177 | + "codemirror_mode": { |
| 178 | + "name": "ipython", |
| 179 | + "version": 3 |
| 180 | + }, |
| 181 | + "file_extension": ".py", |
| 182 | + "mimetype": "text/x-python", |
| 183 | + "name": "python", |
| 184 | + "nbconvert_exporter": "python", |
| 185 | + "pygments_lexer": "ipython3", |
| 186 | + "version": "3.13.7" |
| 187 | + } |
| 188 | + }, |
| 189 | + "nbformat": 4, |
| 190 | + "nbformat_minor": 5 |
| 191 | +} |
0 commit comments