OvidijusParsiunas / deep-chat

Fully customizable AI chatbot component for your website
https://deepchat.dev
MIT License
1.27k stars 175 forks source link

Response interceptor not triggering when using OpenAI tools #108

Closed devpulse01 closed 4 months ago

devpulse01 commented 5 months ago

Hey Ovidijus, I've been trying to implement the response interceptor ( as described on https://deepchat.dev/docs/interceptors#responseInterceptor ).

Everything works well without tools, but when I add a tool, the interceptor doesn't trigger for the OpenAI response anymore.

For example, in my case, the interceptor is triggered for the tool call:

{
   "id":"chatcmpl-454564641878945465",
   "object":"chat.completion",
   "created":1706708920,
   "model":"gpt-4-0125-preview",
   "choices":[
      {
         "index":0,
         "message":{
            "role":"assistant",
            "content":null,
            "tool_calls":[
               {
                  "id":"call_123456789",
                  "type":"function",
                  "function":{
                     "name":"get_current_weather",
                     "arguments":"{\"location\":\"New York\",\"unit\":\"metric\"}"
                  }
               }
            ]
         },
         "logprobs":null,
         "finish_reason":"tool_calls"
      }
   ],
   "usage":{
      "prompt_tokens":81,
      "completion_tokens":19,
      "total_tokens":100
   },
   "system_fingerprint":""
}

Then, I return a result formatted like this:

[
   {
      "response":"{\"location\":\"New York\",\"temperature\":\"12.5\"}"
   }
]

However, I'm not able to retrieve the follow-up reply because the interceptor is not triggered.

{
  "id": "chatcmpl-456456456421689489",
  "object": "chat.completion",
  "created": 1706708925,
  "model": "gpt-4-0125-preview",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The current temperature in New York is 12.5°C."
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 64,
    "completion_tokens": 12,
    "total_tokens": 76
  },
  "system_fingerprint": ""
}

Am I missing something, or is this behavior expected? Thank you for your assistance. Best

OvidijusParsiunas commented 5 months ago

Hi @devpulse01.

I have updated the interceptor code for OpenAI Assistants and published it to version 9.0.119 of deep-chat-dev and deep-chat-react-dev packages. Can you let me know if that works for you.

Thanks!

devpulse01 commented 5 months ago

Hi, Thank you for the quick update :) However, my current issue is related to the OpenAI Chat API.

OvidijusParsiunas commented 5 months ago

Oh, my bad, I'll see what I can change there.

OvidijusParsiunas commented 5 months ago

I've made the change in version 9.0.121. Let me know if this works for you.

devpulse01 commented 5 months ago

The onResponse event is now triggered.

However, it contains only the text message:

{
    "text": "The current temperature in New York, NY is 5°C."
}

Would it be possible to receive the entire response object, similar to other types of responses?

{
    "id": "chatcmpl.",
    "object": "chat.completion",
    "created": 1706806717,
    "model": "gpt-4-0125-preview",
    "choices": [
      {
        "index": 0,
        "message": {
          "role": "assistant",
          "content": "The current temperature in New York, NY is 5°C."
        },
        "logprobs": null,
        "finish_reason": "stop"
      }
    ],
    "usage": {
      "prompt_tokens": 62,
      "completion_tokens": 13,
      "total_tokens": 75
    },
    "system_fingerprint": "fp_123456789"
}

Thank you.

OvidijusParsiunas commented 5 months ago

That makes sense, I made this change in 9.0.122.

devpulse01 commented 5 months ago

Everything is working perfectly. Thank you so much!

OvidijusParsiunas commented 5 months ago

Glad to hear it works! I'll include this code in the next release. Thanks!

OvidijusParsiunas commented 4 months ago

This has now been deployed in Deep Chat version 1.4.11. See the release notes for more.