Skip to content

Commit

Permalink
New Prompt, but still some issues with send poem. Need to tune it to …
Browse files Browse the repository at this point in the history
…avoid unnecessary multiple calls to same tool
  • Loading branch information
humcqc committed Jun 12, 2024
1 parent 213c0af commit e33b92d
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private static String generateInvoker(MethodInfo methodInfo, ClassOutput classOu

boolean toolReturnsVoid = methodInfo.returnType().kind() == Type.Kind.VOID;
if (toolReturnsVoid) {
invokeMc.returnValue(invokeMc.load("Success"));
invokeMc.returnValue(invokeMc.load("Success")); // TODO: To change
} else {
invokeMc.returnValue(result);
}
Expand Down
5 changes: 5 additions & 0 deletions model-providers/ollama/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.quarkiverse.langchain4j.ollama.tools;

import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import io.quarkiverse.langchain4j.RegisterAiService;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.enterprise.context.control.ActivateRequestContext;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

@Disabled("Integration tests that need an ollama server running")
@DisplayName("LLM Tools test - " + Llama3ToolsTest.MODEL_NAME)
@QuarkusTest
public class Llama3ToolsTest {

public static final String MODEL_NAME = "llama3";

@RegisterAiService(tools = Tools.Calculator.class, modelName = MODEL_NAME)
public interface MathAssistantLlama3 {
String chat(String userMessage);
}

@Inject
MathAssistantLlama3 mathAssistantLlama3;

@Test
@ActivateRequestContext
void square_of_sum_of_number_of_letters() {
String msg = "What is the square root with maximal precision of the sum " +
"of the numbers of letters in the words hello and llama";
String response = mathAssistantLlama3.chat(msg);
assertThat(response).contains("3.1622776601683795");
}

@RegisterAiService(tools = Tools.ExpenseService.class, modelName = MODEL_NAME)
public interface Assistant {
@SystemMessage("""
You are a property manager assistant, answering to co-owners requests.
Format the date as YYYY-MM-DD and the time as HH:MM
Today is {{current_date}} use this date as date time reference
The co-owners is living in the following condominium: {condominium}
""")
@UserMessage("""
{{request}}
""")
String answer(String condominium, String request);
}
@Inject
Assistant assistant;

@Test
@ActivateRequestContext
void get_expenses() {
String response = assistant.answer("Rives de Marne",
"What are the expenses for this year ?");
assertThat(response).contains("Expense hp12");
}

@Test
@ActivateRequestContext
void should_not_calls_tool() {
String response = assistant.answer("Rives de Marne", "What time is it ?");
assertThat(response).doesNotContain("Expense hp12");
}

@RegisterAiService(tools = Tools.EmailService.class, modelName = MODEL_NAME)
public interface PoemService {
@SystemMessage("You are a professional poet")
@UserMessage("""
Write a poem about {topic}. The poem should be {lines} lines long. Then send this poem by email.
""")
String writeAPoem(String topic, int lines);
}

@Inject
PoemService poemService;

@Test
@ActivateRequestContext
void send_a_poem() {
String response = poemService.writeAPoem("Condominium Rives de marne", 4);
assertThat(response).contains("Success");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.quarkiverse.langchain4j.ollama.tools;

import dev.langchain4j.agent.tool.Tool;
import io.quarkus.logging.Log;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Singleton;

public class Tools {

@Singleton
@SuppressWarnings("unused")
public static class Calculator {
@Tool("Calculates the length of a string")
int stringLength(String s) {
return s.length();
}

String stringLengthStr(String s) {
return String.format("The length of the word %s is %d", s, s.length());
}

@Tool("Calculates the sum of two numbers")
int add(int a, int b) {
return a + b;
}

String addStr(int a, int b) {
return String.format("The sum of %s and %s is %d", a, b, a + b);
}

@Tool("Calculates the square root of a number")
double sqrt(int x) {
return Math.sqrt(x);
}

String sqrtStr(int x) {
return String.format("The square root of %s is %f", x, Math.sqrt(x));
}
}

@Singleton
@SuppressWarnings("unused")
static class ExpenseService {
@Tool("useful for when you need to lookup condominium expenses for given dates.")
public String getExpenses(String condominium, String fromDate, String toDate) {
String result = String.format("""
The Expenses for %s from %s to %s are:
- Expense hp12: 2800e
- Expense 2: 15000e
""", condominium, fromDate, toDate);
Log.infof(result);
return result;
}
}

@ApplicationScoped
static class EmailService {
@Tool("send the given content by email")
@SuppressWarnings("unused")
public void sendAnEmail(String content) {
Log.info("Tool sendAnEmail has been executed successfully!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
quarkus.langchain4j.ollama.log-requests = true
quarkus.langchain4j.ollama.log-responses = true
quarkus.langchain4j.ollama.chat-model.num-predict = 8192
quarkus.langchain4j.ollama.chat-model.num-ctx = 4096


# Not working llm: calebfahlgren/natural-functions , phi3, aya, mistral, gemma,
# Working llm: llama3, qwen2
quarkus.langchain4j.ollama.llama3-2048_ctx.chat-model.model-id = llama3-2048_ctx
quarkus.langchain4j.ollama.llama3-2048_ctx.timeout = 60s
quarkus.langchain4j.ollama.llama3-2048_ctx.chat-model.temperature = 0.0
quarkus.langchain4j.ollama.llama3-2048_ctx.chat-model.num-ctx = 4096
quarkus.langchain4j.ollama.llama3-2048_ctx.chat-model.num-predict = 8192
quarkus.langchain4j.ollama.llama3-2048_ctx.experimental-tools = true

quarkus.langchain4j.ollama.llama3.chat-model.model-id = llama3-2048_ctx
quarkus.langchain4j.ollama.llama3.timeout = 60s
quarkus.langchain4j.ollama.llama3.chat-model.temperature = 0.0
quarkus.langchain4j.ollama.llama3.chat-model.num-ctx = 4096
quarkus.langchain4j.ollama.llama3.chat-model.num-predict = 8192
quarkus.langchain4j.ollama.llama3.experimental-tools = true

quarkus.langchain4j.ollama.mistral.chat-model.model-id = mistral
quarkus.langchain4j.ollama.mistral.timeout = 60s
quarkus.langchain4j.ollama.mistral.chat-model.temperature = 0.0
quarkus.langchain4j.ollama.mistral.experimental-tools = true

quarkus.langchain4j.ollama.qwen2.chat-model.model-id = qwen2
quarkus.langchain4j.ollama.qwen2.timeout = 60s
quarkus.langchain4j.ollama.qwen2.chat-model.temperature = 0.0
quarkus.langchain4j.ollama.qwen2.experimental-tools = true
Loading

0 comments on commit e33b92d

Please sign in to comment.