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

Token-level streaming support #1215

Closed
cpacker opened this issue Apr 3, 2024 · 0 comments · Fixed by #1262
Closed

Token-level streaming support #1215

cpacker opened this issue Apr 3, 2024 · 0 comments · Fixed by #1262
Labels
API Related to MemGPT API roadmap Planned features

Comments

@cpacker
Copy link
Owner

cpacker commented Apr 3, 2024

Feature: add token-level streaming support for the API

Rough high-level implementation:

  • For OpenAI / function-calling APIs that support streaming
    • Transparently use the function-calling streaming and pass the outputs back
  • For local LLMs / non-function-calling APIs where we are using custom prompt formatters
    • Stream back the raw completion tokens to the model wrapper / output parser
    • The output parser acts as middleware and buffers the stream to mimic an OpenAI function-calling API token stream request

Example SSE response from a function calling OpenAI request:

curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
  "model": "gpt-4",
  "messages": [
    {
      "role": "user",
      "content": "What is the weather like in Boston?"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
              "type": "string",
              "enum": ["celsius", "fahrenheit"]
            }
          },
          "required": ["location"]
        }
      }
    }
  ],
  "tool_choice": "auto",
  "stream": true
}'
data: {"id":"chatcmpl-9A0pokSpYASGLPfIzV8jx6tuvBUtz","object":"chat.completion.chunk","created":1712173516,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"id":"call_telkvFkERZ3MW3eLzHsVrRhl","type":"function","function":{"name":"get_current_weather","arguments":""}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-9A0pokSpYASGLPfIzV8jx6tuvBUtz","object":"chat.completion.chunk","created":1712173516,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\n"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-9A0pokSpYASGLPfIzV8jx6tuvBUtz","object":"chat.completion.chunk","created":1712173516,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" "}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-9A0pokSpYASGLPfIzV8jx6tuvBUtz","object":"chat.completion.chunk","created":1712173516,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" \""}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-9A0pokSpYASGLPfIzV8jx6tuvBUtz","object":"chat.completion.chunk","created":1712173516,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"location"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-9A0pokSpYASGLPfIzV8jx6tuvBUtz","object":"chat.completion.chunk","created":1712173516,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-9A0pokSpYASGLPfIzV8jx6tuvBUtz","object":"chat.completion.chunk","created":1712173516,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" \""}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-9A0pokSpYASGLPfIzV8jx6tuvBUtz","object":"chat.completion.chunk","created":1712173516,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"Boston"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-9A0pokSpYASGLPfIzV8jx6tuvBUtz","object":"chat.completion.chunk","created":1712173516,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"\n"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-9A0pokSpYASGLPfIzV8jx6tuvBUtz","object":"chat.completion.chunk","created":1712173516,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"}"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-9A0pokSpYASGLPfIzV8jx6tuvBUtz","object":"chat.completion.chunk","created":1712173516,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"tool_calls"}]}

data: [DONE]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Related to MemGPT API roadmap Planned features
Projects
Status: Done
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant