Skip to content

Commit c706e6c

Browse files
author
Caroline Ladek
committedMar 12, 2025
add basic setup for Ollama to support exercises for generating random items
1 parent f733f0c commit c706e6c

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed
 

‎backend/pom.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,18 @@
137137
<dependency>
138138
<groupId>io.quarkus</groupId>
139139
<artifactId>quarkus-rest-client-reactive-jackson</artifactId>
140-
<scope>test</scope>
141140
</dependency>
142141
<dependency>
143142
<groupId>org.wiremock</groupId>
144143
<artifactId>wiremock</artifactId>
145144
<version>${wiremock.version}</version>
146145
<scope>test</scope>
147146
</dependency>
147+
<dependency>
148+
<groupId>io.quarkiverse.openapi.generator</groupId>
149+
<artifactId>quarkus-openapi-generator</artifactId>
150+
<version>${openapi.generator.version}</version>
151+
</dependency>
148152
</dependencies>
149153
<build>
150154
<plugins>
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
openapi: 3.1.0
2+
info:
3+
title: Ollama LLM API
4+
description: API specification for interacting with a locally hosted Ollama model
5+
version: 1.0.0
6+
servers:
7+
- url: http://localhost:11434
8+
paths:
9+
/api/generate:
10+
post:
11+
summary: Generate a response for a given prompt
12+
description: |
13+
Generates a response using the specified model. This is a streaming endpoint, meaning multiple responses may be returned.
14+
operationId: queryLlm
15+
requestBody:
16+
required: true
17+
content:
18+
application/json:
19+
schema:
20+
type: object
21+
required:
22+
- model
23+
- prompt
24+
properties:
25+
model:
26+
type: string
27+
description: The model name to use for generating responses.
28+
prompt:
29+
type: string
30+
description: The input text for the model.
31+
suffix:
32+
type: string
33+
description: The text to append after the model's response.
34+
nullable: true
35+
images:
36+
type: array
37+
description: List of base64-encoded images (for multimodal models).
38+
items:
39+
type: string
40+
format:
41+
type: object
42+
additionalProperties: true
43+
description: The format of the response. Can be `json` or a JSON schema.
44+
options:
45+
type: object
46+
description: Additional model parameters from the Modelfile (e.g., temperature).
47+
system:
48+
type: string
49+
description: Overrides the system message defined in the Modelfile.
50+
template:
51+
type: string
52+
description: Overrides the prompt template defined in the Modelfile.
53+
stream:
54+
type: boolean
55+
description: If false, returns a single response instead of a stream.
56+
default: true
57+
raw:
58+
type: boolean
59+
description: If true, disables prompt formatting.
60+
default: false
61+
keep_alive:
62+
type: string
63+
description: Controls how long the model stays loaded in memory (default 5m).
64+
example: "5m"
65+
context:
66+
type: string
67+
description: (Deprecated) Context from a previous request to maintain short-term memory.
68+
nullable: true
69+
responses:
70+
"200":
71+
description: Successful response with generated text.
72+
content:
73+
application/json:
74+
schema:
75+
type: object
76+
properties:
77+
response:
78+
type: string
79+
description: The generated text response.
80+
model:
81+
type: string
82+
description: The model used for generation.
83+
usage:
84+
type: object
85+
description: Statistics on token usage.
86+
properties:
87+
prompt_tokens:
88+
type: integer
89+
completion_tokens:
90+
type: integer
91+
total_tokens:
92+
type: integer
93+
"400":
94+
description: Bad request due to invalid parameters.
95+
"500":
96+
description: Internal server error.

‎docker-compose.yaml

+17
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,27 @@ services:
3535
networks:
3636
- quarkus
3737

38+
ollama:
39+
image: ollama/ollama:0.5.13
40+
container_name: ollama
41+
restart: unless-stopped
42+
pull_policy: always
43+
tty: true
44+
volumes:
45+
- ollama:/root/.ollama
46+
- ./entrypoint.sh:/entrypoint.sh
47+
ports:
48+
- 11434:11434
49+
networks:
50+
- quarkus
51+
entrypoint: [ "/usr/bin/bash", "/entrypoint.sh" ]
52+
env_file: "./backend/.env"
53+
3854
networks:
3955
quarkus:
4056
driver: bridge
4157

4258
volumes:
59+
ollama: {}
4360
db-data:
4461
driver: local

‎entrypoint.sh

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
# Start Ollama in the background
4+
/bin/ollama serve &
5+
pid=$!
6+
7+
# Wait for Ollama to start
8+
sleep 5
9+
10+
# Check if the ollama model exists
11+
if ! ollama list | grep -q "^${OLLAMA_MODEL}"; then
12+
echo "${OLLAMA_MODEL} model not found. Pulling now..."
13+
ollama pull "${OLLAMA_MODEL}"
14+
echo "Model download complete!"
15+
else
16+
echo "${OLLAMA_MODEL} model is already available."
17+
fi
18+
19+
# Run the model with the specified keep-alive time
20+
echo "Starting ${OLLAMA_MODEL} model with keep-alive time of ${OLLAMA_KEEP_ALIVE_TIME}..."
21+
ollama run "${OLLAMA_MODEL}" --keepalive "${OLLAMA_KEEP_ALIVE_TIME}"
22+
23+
# Wait for Ollama process to finish
24+
wait $pid

‎pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
2525
<quarkus.platform.version>3.6.3</quarkus.platform.version>
2626
<querydsl.version>5.0.0</querydsl.version>
27+
<openapi.generator.version>2.8.1</openapi.generator.version>
2728
<!-- Run integration tests by default -->
2829
<skipITs>false</skipITs>
2930
<surefire-plugin.version>3.0.0</surefire-plugin.version>

0 commit comments

Comments
 (0)
Please sign in to comment.