Skip to content

Commit

Permalink
Tested and fixed code for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mzattera committed Apr 22, 2024
1 parent 31298ec commit b14d699
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 84 deletions.
2 changes: 1 addition & 1 deletion eclipse/predictive-powers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.mzattera</groupId>
<artifactId>predictive-powers</artifactId>
<version>0.5.0</version>
<version>0.6.0.preview</version>
<name>PredictivePowers</name>
<description>A Java library to (easily) create GenAI-powered autonomous
agents</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import io.github.mzattera.predictivepowers.AiEndpoint;
import io.github.mzattera.predictivepowers.anthropic.client.AnthropicEndpoint;
import io.github.mzattera.predictivepowers.huggingface.client.HuggingFaceEndpoint;
import io.github.mzattera.predictivepowers.huggingface.services.HuggingFaceChatService;
import io.github.mzattera.predictivepowers.services.ChatService;

public class ChatExample {
Expand All @@ -38,12 +36,13 @@ public static void main(String[] args) throws Exception {
// ChatService agent = endpoint.getChatService("<YourDeployModelName>");

// Uncomment the below to use Anthropic API
// AiEndpoint endpoint = new AnthropicEndpoint();
// ChatService agent = endpoint.getChatService();

AiEndpoint endpoint = new HuggingFaceEndpoint();
AiEndpoint endpoint = new AnthropicEndpoint();
ChatService agent = endpoint.getChatService();

// Uncomment the below to use Hugging Face API
// AiEndpoint endpoint = new HuggingFaceEndpoint();
// ChatService agent = endpoint.getChatService();

) {

// Give instructions to agent
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import io.github.mzattera.predictivepowers.AiEndpoint;
import io.github.mzattera.predictivepowers.huggingface.client.HuggingFaceEndpoint;
import io.github.mzattera.predictivepowers.openai.client.DirectOpenAiEndpoint;
import io.github.mzattera.predictivepowers.services.ImageGenerationService;
import io.github.mzattera.util.ImageUtil;

Expand All @@ -35,24 +34,21 @@ public class ImageGenerationExample {

public static void main(String[] args) throws Exception {

// DALL-E image generation
try (AiEndpoint endpoint = new DirectOpenAiEndpoint();
ImageGenerationService svc = endpoint.getImageGenerationService();) {
try (
// Uncomment this to use DALL-E
// AiEndpoint endpoint = new DirectOpenAiEndpoint();
// ImageGenerationService svc = endpoint.getImageGenerationService();

// Uncomment this to use Openjourney
AiEndpoint endpoint = new HuggingFaceEndpoint();
ImageGenerationService svc = endpoint.getImageGenerationService();
) {
// Generates image
BufferedImage img = svc.createImage(PROMPT, 1, 1024, 1024).get(0);

// Saves it in a temporary file
save(img);
}

// Openjourney (notice is same code as above)
try (AiEndpoint endpoint = new HuggingFaceEndpoint();
ImageGenerationService svc = endpoint.getImageGenerationService();) {
BufferedImage img = svc.createImage(PROMPT, 1, 1024, 1024).get(0);
save(img);
}

}

private static void save(BufferedImage img) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,10 @@ public class OracleExample {

public static void main(String[] args) throws Exception {

// Uncomment the below to use OpenAI services for the oracle
AiEndpoint endpoint = new DirectOpenAiEndpoint();

// Uncomment the below to use Hugging Face services for the oracle
// Endpoint endpoint = new HuggingFaceEndpoint();

// Question answering service
try (endpoint;
try (AiEndpoint endpoint = new DirectOpenAiEndpoint();
QuestionAnsweringService answerSvc = endpoint.getQuestionAnsweringService();
KnowledgeBase knowledgeBase = new KnowledgeBase();) {
KnowledgeBase knowledgeBase = new KnowledgeBase();
) {

try (Scanner console = new Scanner(System.in)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@ public class VisionApiExample {

public static void main(String[] args) throws Exception {

// Create agent using GPT vision model
// try (OpenAiEndpoint endpoint = new DirectOpenAiEndpoint()) {
// Agent bot = endpoint.getChatService("gpt-4-vision-preview");

// Create agent using Anthropic
try (AiEndpoint endpoint = new AnthropicEndpoint()) {
try (
// Uncomment the below code to use OpenAI
// OpenAiEndpoint endpoint = new DirectOpenAiEndpoint();
// Agent bot = endpoint.getChatService("gpt-4-vision-preview");

// Uncomment the below code to use ANTHROP\C API
AiEndpoint endpoint = new AnthropicEndpoint();
ChatService bot = endpoint.getChatService();
) {

// Build the message to send
ChatMessage msg = new ChatMessage("Is there any grass in this image?");

// Include the image to inspect from an URL in the message
// Include Provide an URL to the the image to inspect
msg.getParts().add(FilePart.fromUrl(
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"));

// The below shows as you can do the same with a local file image
// The below code shows as you can do the same with a local file image
// msg.getParts().add(
// new FilePart(new File("YourFileName.jpg"), ContentType.IMAGE)
// );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;
import okhttp3.Request;
import okhttp3.Response;
import okio.Buffer;
import okio.BufferedSource;
import retrofit2.HttpException;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*/
public class HuggingFaceChatService extends AbstractChatService {

public static final String DEFAULT_MODEL = "microsoft/DialoGPT-medium";
public static final String DEFAULT_MODEL = "mistralai/Mistral-7B-Instruct-v0.1";

public HuggingFaceChatService(HuggingFaceEndpoint ep) {
this(ep, ConversationalRequest.builder().options(Options.builder().waitForModel(true).build()).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static Base64FilePart forAnthropic(FilePart file) throws IOException {
int h = img.getHeight();

double scale1 = 1568d / Math.max(w, h); // Longest edge must be < 1568
double scale2 = Math.sqrt(1_150_000/(w * h)); // Image < 1.15 Mpixel, or ~1600 tokens
double scale2 = Math.sqrt(1_150_000d /(w * h)); // Image < 1.15 Mpixel, or ~1600 tokens
double scale = Math.min(scale1, scale2);

if (scale < 1.0d) {
Expand Down

0 comments on commit b14d699

Please sign in to comment.