mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-10-27 14:40:42 +01:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b15066a204 | ||
|
|
e2b29b6a07 | ||
|
|
7470ebe846 | ||
|
|
422efa68aa | ||
|
|
f4d8671922 | ||
|
|
70b136c9fc |
14
Makefile
14
Makefile
@@ -10,4 +10,16 @@ it:
|
|||||||
list-releases:
|
list-releases:
|
||||||
curl 'https://central.sonatype.com/api/internal/browse/component/versions?sortField=normalizedVersion&sortDirection=asc&page=0&size=12&filter=namespace%3Aio.github.amithkoujalgi%2Cname%3Aollama4j' \
|
curl 'https://central.sonatype.com/api/internal/browse/component/versions?sortField=normalizedVersion&sortDirection=asc&page=0&size=12&filter=namespace%3Aio.github.amithkoujalgi%2Cname%3Aollama4j' \
|
||||||
--compressed \
|
--compressed \
|
||||||
--silent | jq '.components[].version'
|
--silent | jq '.components[].version'
|
||||||
|
|
||||||
|
build-docs:
|
||||||
|
npm i --prefix docs && npm run build --prefix docs
|
||||||
|
|
||||||
|
start-docs:
|
||||||
|
npm i --prefix docs && npm run start --prefix docs
|
||||||
|
|
||||||
|
start-cpu:
|
||||||
|
docker run -it -v ~/ollama:/root/.ollama -p 11434:11434 ollama/ollama
|
||||||
|
|
||||||
|
start-gpu:
|
||||||
|
docker run -it --gpus=all -v ~/ollama:/root/.ollama -p 11434:11434 ollama/ollama
|
||||||
@@ -8,7 +8,7 @@ This API lets you ask questions along with the image files to the LLMs.
|
|||||||
These APIs correlate to
|
These APIs correlate to
|
||||||
the [completion](https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-completion) APIs.
|
the [completion](https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-completion) APIs.
|
||||||
|
|
||||||
:::caution
|
:::note
|
||||||
|
|
||||||
Executing this on Ollama server running in CPU-mode will take longer to generate response. Hence, GPU-mode is
|
Executing this on Ollama server running in CPU-mode will take longer to generate response. Hence, GPU-mode is
|
||||||
recommended.
|
recommended.
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ This API lets you ask questions along with the image files to the LLMs.
|
|||||||
These APIs correlate to
|
These APIs correlate to
|
||||||
the [completion](https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-completion) APIs.
|
the [completion](https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-completion) APIs.
|
||||||
|
|
||||||
:::caution
|
:::note
|
||||||
|
|
||||||
Executing this on Ollama server running in CPU-mode will take longer to generate response. Hence, GPU-mode is
|
Executing this on Ollama server running in CPU-mode will take longer to generate response. Hence, GPU-mode is
|
||||||
recommended.
|
recommended.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
sidebar_position: 5
|
sidebar_position: 6
|
||||||
---
|
---
|
||||||
|
|
||||||
# Generate Embeddings
|
# Generate Embeddings
|
||||||
@@ -30,17 +30,17 @@ public class Main {
|
|||||||
|
|
||||||
You will get a response similar to:
|
You will get a response similar to:
|
||||||
|
|
||||||
```json
|
```javascript
|
||||||
[
|
[
|
||||||
0.5670403838157654,
|
0.5670403838157654,
|
||||||
0.009260174818336964,
|
0.009260174818336964,
|
||||||
0.23178744316101074,
|
0.23178744316101074,
|
||||||
-0.2916173040866852,
|
-0.2916173040866852,
|
||||||
-0.8924556970596313,
|
-0.8924556970596313,
|
||||||
0.8785552978515625,
|
0.8785552978515625,
|
||||||
-0.34576427936553955,
|
-0.34576427936553955,
|
||||||
0.5742510557174683,
|
0.5742510557174683,
|
||||||
-0.04222835972905159,
|
-0.04222835972905159,
|
||||||
-0.137906014919281
|
-0.137906014919281
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
73
docs/docs/apis-ask/prompt-builder.md
Normal file
73
docs/docs/apis-ask/prompt-builder.md
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
---
|
||||||
|
sidebar_position: 5
|
||||||
|
---
|
||||||
|
|
||||||
|
# Prompt Builder
|
||||||
|
|
||||||
|
This is designed for prompt engineering. It allows you to easily build the prompt text for zero-shot, one-shot, few-shot
|
||||||
|
inferences.
|
||||||
|
|
||||||
|
```java
|
||||||
|
|
||||||
|
import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;
|
||||||
|
import io.github.amithkoujalgi.ollama4j.core.models.OllamaResult;
|
||||||
|
import io.github.amithkoujalgi.ollama4j.core.types.OllamaModelType;
|
||||||
|
import io.github.amithkoujalgi.ollama4j.core.utils.PromptBuilder;
|
||||||
|
|
||||||
|
public class AskPhi {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
|
String host = "http://localhost:11434/";
|
||||||
|
OllamaAPI ollamaAPI = new OllamaAPI(host);
|
||||||
|
ollamaAPI.setRequestTimeoutSeconds(10);
|
||||||
|
|
||||||
|
String model = OllamaModelType.PHI;
|
||||||
|
|
||||||
|
PromptBuilder promptBuilder =
|
||||||
|
new PromptBuilder()
|
||||||
|
.addLine("You are an expert coder and understand different programming languages.")
|
||||||
|
.addLine("Given a question, answer ONLY with code.")
|
||||||
|
.addLine("Produce clean, formatted and indented code in markdown format.")
|
||||||
|
.addLine(
|
||||||
|
"DO NOT include ANY extra text apart from code. Follow this instruction very strictly!")
|
||||||
|
.addLine("If there's any additional information you want to add, use comments within code.")
|
||||||
|
.addLine("Answer only in the programming language that has been asked for.")
|
||||||
|
.addSeparator()
|
||||||
|
.addLine("Example: Sum 2 numbers in Python")
|
||||||
|
.addLine("Answer:")
|
||||||
|
.addLine("```python")
|
||||||
|
.addLine("def sum(num1: int, num2: int) -> int:")
|
||||||
|
.addLine(" return num1 + num2")
|
||||||
|
.addLine("```")
|
||||||
|
.addSeparator()
|
||||||
|
.add("How do I read a file in Go and print its contents to stdout?");
|
||||||
|
|
||||||
|
OllamaResult response = ollamaAPI.ask(model, promptBuilder.build());
|
||||||
|
System.out.println(response.getResponse());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
You will get a response similar to:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func readFile(fileName string) {
|
||||||
|
file, err := ioutil.ReadFile(fileName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "Error reading file:", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
f, _ := ioutil.ReadFile("file.txt")
|
||||||
|
if f != nil {
|
||||||
|
fmt.Println(f.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -2,10 +2,38 @@
|
|||||||
sidebar_position: 1
|
sidebar_position: 1
|
||||||
---
|
---
|
||||||
|
|
||||||
# Intro
|
# Introduction
|
||||||
|
|
||||||
Let's get started with **Ollama4j**.
|
Let's get started with **Ollama4j**.
|
||||||
|
|
||||||
|
## 🦙 What is Ollama?
|
||||||
|
|
||||||
|
[Ollama](https://ollama.ai/) is an advanced AI tool that allows users to easily set up and run large language models
|
||||||
|
locally (in CPU and GPU
|
||||||
|
modes). With Ollama, users can leverage powerful language models such as Llama 2 and even customize and create their own
|
||||||
|
models.
|
||||||
|
|
||||||
|
## 👨💻 Why Ollama4j?
|
||||||
|
|
||||||
|
Ollama4j was built for the simple purpose of integrating Ollama with Java applications.
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
flowchart LR
|
||||||
|
o4j[Ollama4j]
|
||||||
|
o[Ollama Server]
|
||||||
|
o4j -->|Communicates with| o;
|
||||||
|
m[Models]
|
||||||
|
p[Your Java Project]
|
||||||
|
subgraph Your Java Environment
|
||||||
|
direction TB
|
||||||
|
p -->|Uses| o4j
|
||||||
|
end
|
||||||
|
subgraph Ollama Setup
|
||||||
|
direction TB
|
||||||
|
o -->|Manages| m
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
### What you'll need
|
### What you'll need
|
||||||
|
|||||||
@@ -131,8 +131,13 @@ const config = {
|
|||||||
prism: {
|
prism: {
|
||||||
theme: prismThemes.github,
|
theme: prismThemes.github,
|
||||||
darkTheme: prismThemes.dracula,
|
darkTheme: prismThemes.dracula,
|
||||||
|
additionalLanguages: ['java'],
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
markdown: {
|
||||||
|
mermaid: true,
|
||||||
|
},
|
||||||
|
themes: ['@docusaurus/theme-mermaid']
|
||||||
};
|
};
|
||||||
|
|
||||||
export default config;
|
export default config;
|
||||||
|
|||||||
1136
docs/package-lock.json
generated
1136
docs/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -16,6 +16,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@docusaurus/core": "3.0.1",
|
"@docusaurus/core": "3.0.1",
|
||||||
"@docusaurus/preset-classic": "3.0.1",
|
"@docusaurus/preset-classic": "3.0.1",
|
||||||
|
"@docusaurus/theme-mermaid": "^3.0.1",
|
||||||
"@mdx-js/react": "^3.0.0",
|
"@mdx-js/react": "^3.0.0",
|
||||||
"clsx": "^2.0.0",
|
"clsx": "^2.0.0",
|
||||||
"prism-react-renderer": "^2.3.0",
|
"prism-react-renderer": "^2.3.0",
|
||||||
|
|||||||
4
pom.xml
4
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
<groupId>io.github.amithkoujalgi</groupId>
|
<groupId>io.github.amithkoujalgi</groupId>
|
||||||
<artifactId>ollama4j</artifactId>
|
<artifactId>ollama4j</artifactId>
|
||||||
<version>1.0.38</version>
|
<version>1.0.40</version>
|
||||||
|
|
||||||
<name>Ollama4j</name>
|
<name>Ollama4j</name>
|
||||||
<description>Java library for interacting with Ollama API.</description>
|
<description>Java library for interacting with Ollama API.</description>
|
||||||
@@ -39,7 +39,7 @@
|
|||||||
<connection>scm:git:git@github.com:amithkoujalgi/ollama4j.git</connection>
|
<connection>scm:git:git@github.com:amithkoujalgi/ollama4j.git</connection>
|
||||||
<developerConnection>scm:git:https://github.com/amithkoujalgi/ollama4j.git</developerConnection>
|
<developerConnection>scm:git:https://github.com/amithkoujalgi/ollama4j.git</developerConnection>
|
||||||
<url>https://github.com/amithkoujalgi/ollama4j</url>
|
<url>https://github.com/amithkoujalgi/ollama4j</url>
|
||||||
<tag>v1.0.38</tag>
|
<tag>v1.0.40</tag>
|
||||||
</scm>
|
</scm>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ public class OllamaModelType {
|
|||||||
public static final String VICUNA = "vicuna";
|
public static final String VICUNA = "vicuna";
|
||||||
public static final String WIZARD_VICUNA_UNCENSORED = "wizard-vicuna-uncensored";
|
public static final String WIZARD_VICUNA_UNCENSORED = "wizard-vicuna-uncensored";
|
||||||
public static final String PHIND_CODELLAMA = "phind-codellama";
|
public static final String PHIND_CODELLAMA = "phind-codellama";
|
||||||
|
public static final String PHI = "phi";
|
||||||
public static final String ZEPHYR = "zephyr";
|
public static final String ZEPHYR = "zephyr";
|
||||||
public static final String WIZARDCODER = "wizardcoder";
|
public static final String WIZARDCODER = "wizardcoder";
|
||||||
public static final String MISTRAL_OPENORCA = "mistral-openorca";
|
public static final String MISTRAL_OPENORCA = "mistral-openorca";
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package io.github.amithkoujalgi.ollama4j.core.utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@code PromptBuilder} class is used to construct prompt texts for language models (LLMs). It
|
||||||
|
* provides methods for adding text, adding lines, adding separators, and building the final prompt.
|
||||||
|
*
|
||||||
|
* <p>Example usage:
|
||||||
|
*
|
||||||
|
* <pre>{@code
|
||||||
|
* PromptBuilder promptBuilder = new PromptBuilder();
|
||||||
|
* promptBuilder.add("This is a sample prompt for language models.")
|
||||||
|
* .addLine("You can add lines to provide context.")
|
||||||
|
* .addSeparator()
|
||||||
|
* .add("Feel free to customize as needed.");
|
||||||
|
* String finalPrompt = promptBuilder.build();
|
||||||
|
* System.out.println(finalPrompt);
|
||||||
|
* }</pre>
|
||||||
|
*/
|
||||||
|
public class PromptBuilder {
|
||||||
|
|
||||||
|
private final StringBuilder prompt;
|
||||||
|
|
||||||
|
/** Constructs a new {@code PromptBuilder} with an empty prompt. */
|
||||||
|
public PromptBuilder() {
|
||||||
|
this.prompt = new StringBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends the specified text to the prompt.
|
||||||
|
*
|
||||||
|
* @param text the text to be added to the prompt
|
||||||
|
* @return a reference to this {@code PromptBuilder} instance for method chaining
|
||||||
|
*/
|
||||||
|
public PromptBuilder add(String text) {
|
||||||
|
prompt.append(text);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends the specified text followed by a newline character to the prompt.
|
||||||
|
*
|
||||||
|
* @param text the text to be added as a line to the prompt
|
||||||
|
* @return a reference to this {@code PromptBuilder} instance for method chaining
|
||||||
|
*/
|
||||||
|
public PromptBuilder addLine(String text) {
|
||||||
|
prompt.append(text).append("\n");
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends a separator line to the prompt. The separator is a newline followed by a line of
|
||||||
|
* dashes.
|
||||||
|
*
|
||||||
|
* @return a reference to this {@code PromptBuilder} instance for method chaining
|
||||||
|
*/
|
||||||
|
public PromptBuilder addSeparator() {
|
||||||
|
prompt.append("\n--------------------------------------------------\n");
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds and returns the final prompt as a string.
|
||||||
|
*
|
||||||
|
* @return the final prompt as a string
|
||||||
|
*/
|
||||||
|
public String build() {
|
||||||
|
return prompt.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user