Commit 5c3224d448eae85b36cc015b188a74a0cc547087
1 parent
e845f252e0
Exists in
master
api和entity
Showing
10 changed files
with
173 additions
and
0 deletions
Show diff stats
app/src/main/java/com/hjx/parent/api/ChatApi.java
app/src/main/java/com/hjx/parent/api/ChatChoices.java
... | ... | @@ -0,0 +1,13 @@ |
1 | +package com.hjx.parent.api; | |
2 | + | |
3 | +import com.google.gson.annotations.SerializedName; | |
4 | + | |
5 | +public class ChatChoices { | |
6 | + | |
7 | + public ChatMessage message; | |
8 | + public int index; | |
9 | + @SerializedName("finish_reason") | |
10 | + public String finishReason; | |
11 | + public String logprobs; | |
12 | + | |
13 | +} | ... | ... |
app/src/main/java/com/hjx/parent/api/ChatError.java
app/src/main/java/com/hjx/parent/api/ChatMessage.java
... | ... | @@ -0,0 +1,44 @@ |
1 | +package com.hjx.parent.api; | |
2 | + | |
3 | +import java.util.ArrayList; | |
4 | +import java.util.List; | |
5 | +import java.util.Objects; | |
6 | + | |
7 | +public class ChatMessage { | |
8 | + public ChatMessage() { | |
9 | + } | |
10 | + public ChatMessage(String role, Object content) { | |
11 | + this.role = role; | |
12 | + this.content = content; | |
13 | + } | |
14 | + | |
15 | + public String role; | |
16 | + public Object content; | |
17 | + | |
18 | + public static ChatMessage fromImage(String role, String text, String... urls) { | |
19 | + List<ContentItem> items = new ArrayList<>(); | |
20 | + if (text != null && !text.isEmpty()) { | |
21 | + items.add(new ContentItem(ContentItem.TYPE_TEXT, text)); | |
22 | + } | |
23 | + for (String it: urls) { | |
24 | + items.add(new ContentItem(ContentItem.TYPE_IMAGE, it)); | |
25 | + } | |
26 | + return new ChatMessage(role, items); | |
27 | + } | |
28 | + | |
29 | + public String getTextContent() { | |
30 | + if (content == null) return null; | |
31 | + if (content instanceof String) return (String) content; | |
32 | + ContentItem item = firstText(); | |
33 | + return item == null ? null : item.text; | |
34 | + } | |
35 | + | |
36 | + private ContentItem firstText() { | |
37 | + if (!(content instanceof List<?>)) return null; | |
38 | + List<?> anyList = (List<?>) content; | |
39 | + return anyList.stream() | |
40 | + .map(ContentItem::reSerialize) | |
41 | + .filter(Objects::nonNull) | |
42 | + .findFirst().orElse(null); | |
43 | + } | |
44 | +} | ... | ... |
app/src/main/java/com/hjx/parent/api/ChatRequest.java
app/src/main/java/com/hjx/parent/api/ChatResponse.java
... | ... | @@ -0,0 +1,19 @@ |
1 | +package com.hjx.parent.api; | |
2 | + | |
3 | +import com.google.gson.annotations.SerializedName; | |
4 | + | |
5 | +import java.util.List; | |
6 | + | |
7 | +public class ChatResponse { | |
8 | + public List<ChatChoices> choices; | |
9 | + public String created; | |
10 | + public String id; | |
11 | + public String model; | |
12 | + @SerializedName("service_tier") | |
13 | + public String serviceTier; | |
14 | + @SerializedName("object") | |
15 | + public String obj; | |
16 | + public ChatUsage usage; | |
17 | + | |
18 | + public ChatError error; | |
19 | +} | ... | ... |
app/src/main/java/com/hjx/parent/api/ChatUsage.java
... | ... | @@ -0,0 +1,16 @@ |
1 | +package com.hjx.parent.api; | |
2 | + | |
3 | +import com.google.gson.annotations.SerializedName; | |
4 | + | |
5 | +public class ChatUsage { | |
6 | + @SerializedName("completion_tokens") | |
7 | + public int completionTokens; | |
8 | + @SerializedName("prompt_tokens") | |
9 | + public int promptTokens; | |
10 | + @SerializedName("total_tokens") | |
11 | + public int totalTokens; | |
12 | + @SerializedName("prompt_tokens_details") | |
13 | + public PromptTokensDetails promptTokensDetails; | |
14 | + @SerializedName("completion_tokens_details") | |
15 | + public CompletionTokensDetails completionTokensDetails; | |
16 | +} | ... | ... |
app/src/main/java/com/hjx/parent/api/CompletionTokensDetails.java
app/src/main/java/com/hjx/parent/api/ContentItem.java
... | ... | @@ -0,0 +1,44 @@ |
1 | +package com.hjx.parent.api; | |
2 | + | |
3 | +import com.google.gson.Gson; | |
4 | +import com.google.gson.annotations.SerializedName; | |
5 | + | |
6 | +public class ContentItem { | |
7 | + public static final String TYPE_TEXT = "text"; | |
8 | + public static final String TYPE_IMAGE = "image_url"; | |
9 | + | |
10 | + | |
11 | + public ContentItem() { | |
12 | + } | |
13 | + public ContentItem(String type, String text) { | |
14 | + this.type = type; | |
15 | + this.text = text; | |
16 | + } | |
17 | + public ContentItem(String type, ContentImage imageUrl) { | |
18 | + this.type = type; | |
19 | + this.imageUrl = imageUrl; | |
20 | + } | |
21 | + | |
22 | + public String type; | |
23 | + public String text; | |
24 | + @SerializedName("image_url") | |
25 | + public ContentImage imageUrl; | |
26 | + | |
27 | + public static class ContentImage { | |
28 | + public String url; | |
29 | + } | |
30 | + | |
31 | + | |
32 | + private static final Gson gson = new Gson(); | |
33 | + static ContentItem reSerialize(Object any) { | |
34 | + if (any == null) return null; | |
35 | + try { | |
36 | + String json = gson.toJson(any); | |
37 | + ContentItem it = gson.fromJson(json, ContentItem.class); | |
38 | + return it; | |
39 | + } catch (Throwable t) { | |
40 | + t.printStackTrace(); | |
41 | + } | |
42 | + return null; | |
43 | + } | |
44 | +} | ... | ... |
app/src/main/java/com/hjx/parent/api/PromptTokensDetails.java