Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

是否有function call的案例? #109

Open
mumengmeng opened this issue Sep 24, 2023 · 1 comment
Open

是否有function call的案例? #109

mumengmeng opened this issue Sep 24, 2023 · 1 comment

Comments

@mumengmeng
Copy link

mumengmeng commented Sep 24, 2023

是否有chatGPT的function call的案例

HamaWhiteGG added a commit that referenced this issue Nov 30, 2023
Add OpenAI Function Call Example #109
@HamaWhiteGG
Copy link
Owner

Yes, see doc OpenAI Function Call Example.md

or see ChatFunctionTest directly.

class ChatFunctionTest extends OpenAiClientTest {

    private static final Logger LOG = LoggerFactory.getLogger(ChatFunctionTest.class);

    private final String functionName = "get_current_weather";

    @BeforeEach
    void setUp() {
        // register function
        FunctionExecutor.register(functionName, this::getCurrentWeather);
    }

    WeatherResponse getCurrentWeather(Weather weather) {
        // mock function
        return WeatherResponse.builder()
                .location(weather.location())
                .unit(weather.unit())
                .temperature(new Random().nextInt(50))
                .description("sunny")
                .build();
    }

    @Test
    void testChatFunction() {
        ChatFunction chatFunction = ChatFunction.builder()
                .name(functionName)
                .description("Get the current weather in a given location")
                .parameters(ChatParameterUtils.generate(Weather.class))
                .build();

        Message message = Message.of("What is the weather like in Boston?");

        ChatCompletion chatCompletion = ChatCompletion.builder()
                .model("gpt-4")
                .temperature(0)
                .messages(List.of(message))
                .tools(List.of(new Tool(chatFunction)))
                .toolChoice("auto")
                .build();

        ChatCompletionResp response = client.createChatCompletion(chatCompletion);
        ChatChoice chatChoice = response.getChoices().get(0);
        LOG.info("result: {}", chatChoice);
        assertThat(chatChoice).isNotNull();
        assertEquals("tool_calls", chatChoice.getFinishReason());

        Function function = chatChoice.getMessage().getToolCalls().get(0).getFunction();
        // name=get_current_weather, arguments={ "location": "Boston" }
        assertEquals(functionName, function.getName());

        String expectedArguments = """
                {
                  "location": "Boston, MA"
                }""";
        assertEquals(expectedArguments, function.getArguments());

        // execute function
        WeatherResponse weatherResponse =
                FunctionExecutor.execute(function.getName(), Weather.class, function.getArguments());
        LOG.info("result: {}", weatherResponse);
        assertThat(weatherResponse).isNotNull();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants