Skip to content

Commit

Permalink
Merge pull request #34 from sashirestela/33-error-in-chat-completion-…
Browse files Browse the repository at this point in the history
…with-images

Error in Chat Completion with images
  • Loading branch information
sashirestela authored Jan 5, 2024
2 parents 0e2169c + a25ed08 commit 0d9741c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.sashirestela</groupId>
<artifactId>simple-openai</artifactId>
<version>1.2.4</version>
<version>1.2.5</version>
<packaging>jar</packaging>

<name>simple-openai</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import io.github.sashirestela.openai.domain.chat.ChatRequest;
import io.github.sashirestela.openai.domain.chat.ChatResponse;
import io.github.sashirestela.openai.domain.chat.content.ContentPartImage;
import io.github.sashirestela.openai.domain.chat.content.ContentPartText;
import io.github.sashirestela.openai.domain.chat.content.ImageUrl;
import io.github.sashirestela.openai.domain.chat.message.ChatMsg;
import io.github.sashirestela.openai.domain.chat.message.ChatMsgSystem;
import io.github.sashirestela.openai.domain.chat.message.ChatMsgTool;
Expand Down Expand Up @@ -92,6 +95,25 @@ public void demoCallChatWithFunctions() {
System.out.println(chatResponse.firstContent());
}

public void demoCallChatWithVision() {
var chatRequest = ChatRequest.builder()
.model("gpt-4-vision-preview")
.messages(List.of(
new ChatMsgUser(List.of(
new ContentPartText(
"What do you see in the image? Give in details in no more than 100 words."),
new ContentPartImage(new ImageUrl(
"https://upload.wikimedia.org/wikipedia/commons/e/eb/Machu_Picchu%2C_Peru.jpg"))))))
.temperature(0.0)
.maxTokens(500)
.build();
var chatResponse = openAI.chatCompletions().createStream(chatRequest).join();
chatResponse.filter(chatResp -> chatResp.firstContent() != null)
.map(chatResp -> chatResp.firstContent())
.forEach(System.out::print);
System.out.println();
}

public static class Weather implements Functional {
@JsonPropertyDescription("City and state, for example: León, Guanajuato")
public String location;
Expand Down Expand Up @@ -134,6 +156,7 @@ public static void main(String[] args) {
demo.addTitleAction("Call Chat (Streaming Approach)", demo::demoCallChatStreaming);
demo.addTitleAction("Call Chat (Blocking Approach)", demo::demoCallChatBlocking);
demo.addTitleAction("Call Chat with Functions", demo::demoCallChatWithFunctions);
demo.addTitleAction("Call Chat with Vision", demo::demoCallChatWithVision);

demo.run();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.github.sashirestela.openai.domain.chat.content;

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Getter;
import lombok.NonNull;

@Getter
public class ContentPartImage extends ContentPart {

@JsonProperty("image_url")
private ImageUrl imageUrl;

public ContentPartImage(@NonNull ImageUrl imageUrl) {
Expand Down

0 comments on commit 0d9741c

Please sign in to comment.