openai-scala-client icon indicating copy to clipboard operation
openai-scala-client copied to clipboard

One-shot tool prompting

Open 0sten opened this issue 1 year ago • 4 comments

Recently I moved to newer version of openai-scala-client and after some time of ridding of deprecated declarations, discovered that my code does not work any more. Later I found that the problem is with AssistantToolMessage class which makes this code snipped not working (respond is never received). But when I use deprecated AssistantFunMessage, everything works fine. Could someone make a guess why this is going on, please?

  def completeFunctionTask() = {
    val funcSchema = FunctionSpec(
      "result",
      parameters = Map(
        "type" -> "object",
        "properties" -> Map(
          "words" -> Map(
            "type" -> "array",
            "description" -> "words with emotional connotations",
            "items" -> Map(
              "type" -> "object",
              "description" -> "word",
              "properties" -> Map(
                "word" -> Map("type" -> "string", "description" -> "word text"),
              ),
              "required" -> Seq("word")
            )
          )
        )
      )
    )
    val answer = "{\"words\":[{\"word\":\"grateful\"},{\"word\":\"joy\"},{\"word\":\"happiness\"}]}"
    val messages: Seq[BaseMessage] = Seq(
      SystemMessage("You get a text in the form of a word list. Pick words with emotional connotations."),
      UserMessage("[\"We\",\"are\",\"grateful\",\"even\",\"before\",\"we\",\"have\",\"something.\",\"Flowers\",\"always\",\"bring\",\"joy\",\"and\",\"happiness.\"]"),
      AssistantToolMessage(Option.empty, Option.empty, Seq(("call_1", FunctionCallSpec(funcSchema.name, answer)))), // doesn't work with this line
//      AssistantFunMessage(Option.empty, Option.empty, Option(FunctionCallSpec(funcSchema.name, answer))), // works fine with this line
      UserMessage("[\"I'm\",\"lucky\",\"to\",\"be\",\"here\",\"with\",\"you.\"]")
    )

    val createChatCompletionSettings = CreateChatCompletionSettings(model = "gpt-3.5-turbo")
    service.createChatToolCompletion(messages, Seq(funcSchema), Option.apply(funcSchema.name), createChatCompletionSettings).map(resp => {
      println(resp.choices(0).message) // {"words":[{"word":"lucky"}]}
    })

  }

0sten avatar May 02 '24 09:05 0sten

Hey @0sten , I would rename call_1 to result_1 since the name of your function is result. If it doesn't work will take a look.

peterbanda avatar May 05 '24 19:05 peterbanda

@peterbanda thank you for the answer. Unfortunately renaming call_1 to result_1 didn't help.

0sten avatar May 06 '24 08:05 0sten

ok, will check briefly

peterbanda avatar May 08 '24 07:05 peterbanda

Hi @0sten,

thank you for reporting your issues.

By any chance, did you have any exceptions in your console? For your example, I'm getting this one:

io.cequence.openaiscala.OpenAIScalaClientException: Code 400 : {
  "error": {
    "message": "An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: call_1",
    "type": "invalid_request_error",
    "param": "messages",
    "code": null
  }
}

you can resolve it by providing a tool message:

  ...
  AssistantToolMessage(
     Option.empty,
     Option.empty,
     Seq(("call_1", FunctionCallSpec(funcSchema.name, answer)))
   ), // doesn't work with this line
   ToolMessage(
     tool_call_id = "call_1",
     name = funcSchema.name,
     content = Some(answer)
   ),
   UserMessage("[\"I'm\",\"lucky\",\"to\",\"be\",\"here\",\"with\",\"you.\"]")
   ...

Does this resolve your issue?

For further details about tool calling, have a look at our example CreateChatToolCompletionWithFeedback.

bburdiliak avatar May 14 '24 11:05 bburdiliak

Considered resolved. Closing

peterbanda avatar May 20 '24 07:05 peterbanda