Skip to content

Commit 49edc93

Browse files
committed
Local models demo
1 parent 4c909af commit 49edc93

1 file changed

Lines changed: 144 additions & 0 deletions

File tree

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,146 @@
11
= Working with local models
22

3+
:project-ollama-name: quarkus-ollama-app
4+
5+
Throughout this tutorial, we've been working with remote models. Let's switch now to a local model.
6+
7+
There are various options out there, and we'll work with Ollama.
8+
9+
10+
== Installing Ollama
11+
12+
First, you must download and install the specific Ollama version on your operating system. https://ollama.com/download[The instructions can be found here, window="_blank"].
13+
14+
Once installed, go ahead and download the local model by running this command:
15+
16+
[.console-input]
17+
[source,bash]
18+
----
19+
ollama pull llama3:latest
20+
----
21+
22+
Now, let's run our modal locally:
23+
24+
[.console-input]
25+
[source,bash]
26+
----
27+
ollama serve
28+
----
29+
30+
== Creating a new project with the Ollama extension
31+
32+
The Ollama extension isn't compatible with other extensions we have used in this tutorial, so we'll create a new project.
33+
34+
[tabs%sync]
35+
====
36+
37+
Maven::
38+
+
39+
--
40+
[.console-input]
41+
[source,bash,subs="+macros,+attributes"]
42+
----
43+
mvn "io.quarkus.platform:quarkus-maven-plugin:create" -DprojectGroupId="com.redhat.developers" -DprojectArtifactId="{project-ollama-name}" -DprojectVersion="1.0-SNAPSHOT" -Dextensions=rest,langchain4j-ollama
44+
cd {project-ollama-name}
45+
----
46+
--
47+
Quarkus CLI::
48+
+
49+
--
50+
51+
[.console-input]
52+
[source,bash,subs="+macros,+attributes"]
53+
----
54+
quarkus create app -x rest langchain4j-ollama com.redhat.developers:{project-ollama-name}:1.0-SNAPSHOT
55+
cd {project-ollama-name}
56+
----
57+
--
58+
====
59+
60+
== Connect to Ollama
61+
62+
Just add these properties to the `application.properties` file available in `src/main/resources`:
63+
64+
[.console-input]
65+
[source,properties]
66+
----
67+
quarkus.langchain4j.ollama.chat-model.model-id=llama3:latest
68+
quarkus.langchain4j.ollama.timeout=120s
69+
----
70+
71+
== Create the AI service
72+
73+
Let's create an interface for our AI service.
74+
75+
Create a new `Assistant` Java interface in `src/main/java` in the `com.redhat.developers` package with the following contents:
76+
77+
[.console-input]
78+
[source,java]
79+
----
80+
package com.redhat.developers;
81+
82+
import io.quarkiverse.langchain4j.RegisterAiService;
83+
84+
@RegisterAiService
85+
public interface Assistant {
86+
String chat(String message);
87+
}
88+
----
89+
90+
== Create the prompt-base resource
91+
92+
Now we're going to implement a resource that send prompts using the AI service.
93+
94+
Create a new `ExistencialQuestionResource` Java class in `src/main/java` in the `com.redhat.developers` package with the following contents:
95+
96+
[.console-input]
97+
[source,java]
98+
----
99+
package com.redhat.developers;
100+
101+
import jakarta.inject.Inject;
102+
import jakarta.ws.rs.GET;
103+
import jakarta.ws.rs.Path;
104+
import jakarta.ws.rs.Produces;
105+
import jakarta.ws.rs.core.MediaType;
106+
107+
@Path("/earth")
108+
public class ExistencialQuestionResource {
109+
110+
@Inject
111+
Assistant assistant;
112+
113+
@GET
114+
@Path("/flat")
115+
@Produces(MediaType.TEXT_PLAIN)
116+
public String isEarthFlat() {
117+
return assistant.chat("Can you explain me why earth is flat?");
118+
}
119+
}
120+
----
121+
122+
== Invoke the endpoint
123+
124+
You can check your prompt implementation by pointing your browser to http://localhost:8080/earth/flat[window=_blank]
125+
126+
You can also run the following command:
127+
128+
[.console-input]
129+
[source,bash]
130+
----
131+
curl localhost:8080/earth/flat
132+
----
133+
134+
An example of output (it can vary on each prompt execution):
135+
136+
[.console-output]
137+
[source,text]
138+
----
139+
I think there may be a misunderstanding here!
140+
141+
Actually, the scientific consensus is that the Earth is an oblate spheroid, meaning it's slightly flattened at the poles and bulging at the equator. The evidence from various fields of science, including astronomy, geology, and physics, all point to the fact that our planet is indeed round.
142+
143+
Here are some reasons why we know the Earth is not flat:
144+
145+
1. **Ships disappearing over the horizon**: When a ship sails away from an observer on the shore, it will eventually disappear from view as it sinks below the horizon due to the curvature of the Earth.
146+
----

0 commit comments

Comments
 (0)