forked from HamaWhiteGG/langchain-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request HamaWhiteGG#134 from HamaWhiteGG/dev
Supports functions HamaWhiteGG#133
- Loading branch information
Showing
22 changed files
with
869 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
openai-client/src/main/java/com/hw/openai/entity/chat/ChatFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hw.openai.entity.chat; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author HamaWhite | ||
*/ | ||
@Data | ||
@Builder | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class ChatFunction { | ||
|
||
/** | ||
* The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, | ||
* with a maximum length of 64. | ||
*/ | ||
@NotBlank | ||
private String name; | ||
|
||
/** | ||
* A description of what the function does, used by the model to choose when and how to call the function. | ||
*/ | ||
private String description; | ||
|
||
/** | ||
* The parameters the functions accepts, described as a JSON Schema object. | ||
* To describe a function that accepts no parameters, provide the value {"type": "object", "properties": {}} | ||
*/ | ||
@NotNull | ||
private ChatParameter parameters; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public static class ChatParameter { | ||
|
||
private String type; | ||
|
||
private ObjectNode properties; | ||
|
||
private List<String> required; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
openai-client/src/main/java/com/hw/openai/entity/chat/Function.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hw.openai.entity.chat; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* @author HamaWhite | ||
*/ | ||
@Data | ||
public class Function { | ||
|
||
/** | ||
* The name of the function to call. | ||
*/ | ||
private String name; | ||
|
||
/** | ||
* The arguments to call the function with, as generated by the model in JSON format. | ||
* Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your | ||
* function schema. Validate the arguments in your code before calling your function. | ||
*/ | ||
private String arguments; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
openai-client/src/main/java/com/hw/openai/entity/chat/Tool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hw.openai.entity.chat; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* @author HamaWhite | ||
*/ | ||
|
||
@Data | ||
public class Tool { | ||
|
||
/** | ||
* The type of the tool. Currently, only function is supported. | ||
*/ | ||
private String type = "function"; | ||
|
||
@JsonProperty("function") | ||
private ChatFunction chatFunction; | ||
|
||
public Tool(ChatFunction chatFunction) { | ||
this.chatFunction = chatFunction; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
openai-client/src/main/java/com/hw/openai/entity/chat/ToolCall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hw.openai.entity.chat; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* @author HamaWhite | ||
*/ | ||
@Data | ||
public class ToolCall { | ||
|
||
private Integer index; | ||
|
||
/** | ||
* The ID of the tool call. | ||
*/ | ||
private String id; | ||
|
||
/** | ||
* The type of the tool. Currently, only function is supported. | ||
*/ | ||
private String type; | ||
|
||
private Function function; | ||
} |
47 changes: 47 additions & 0 deletions
47
openai-client/src/main/java/com/hw/openai/function/FunctionExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hw.openai.function; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import static com.hw.openai.utils.JsonUtils.convertFromJsonStr; | ||
|
||
/** | ||
* @author HamaWhite | ||
*/ | ||
public class FunctionExecutor { | ||
|
||
private static final Map<String, Function<?, ?>> FUNCTION_MAP = new HashMap<>(16); | ||
|
||
public static <T> void register(String functionName, Function<T, ?> function) { | ||
FUNCTION_MAP.put(functionName, function); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T, R> R execute(String functionName, Class<T> clazz, String arguments) throws ClassCastException { | ||
Function<T, R> function = (Function<T, R>) FUNCTION_MAP.get(functionName); | ||
if (function == null) { | ||
throw new IllegalArgumentException("No function registered with name: " + functionName); | ||
} | ||
T input = convertFromJsonStr(arguments, clazz); | ||
return function.apply(input); | ||
} | ||
} |
Oops, something went wrong.