Skip to content

Commit

Permalink
add more stream events support
Browse files Browse the repository at this point in the history
  • Loading branch information
hayeah committed May 21, 2024
1 parent c502ada commit f8c9b69
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
53 changes: 53 additions & 0 deletions stream_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ type StreamDone struct {
streamEvent
}

type StreamThreadMessageCompleted struct {
Message
streamEvent
}

type StreamThreadMessageDelta struct {
ID string `json:"id"`
Object string `json:"object"`
Expand Down Expand Up @@ -108,6 +113,21 @@ type StreamThreadRunCreated struct {
streamEvent
}

type StreamThreadRunRequiresAction struct {
Run
streamEvent
}

type StreamThreadRunCompleted struct {
Run
streamEvent
}

type StreamRunStepCompleted struct {
RunStep
streamEvent
}

type StreamEvent interface {
Event() string
JSON() json.RawMessage
Expand Down Expand Up @@ -157,12 +177,45 @@ func (s *StreamerV2) Next() bool {
streamEvent: streamEvent,
}
}

case "thread.run.requires_action":
var run Run
if err := json.Unmarshal([]byte(event.Data), &run); err == nil {
s.next = &StreamThreadRunRequiresAction{
Run: run,
streamEvent: streamEvent,
}
}
case "thread.run.completed":
var run Run
if err := json.Unmarshal([]byte(event.Data), &run); err == nil {
s.next = &StreamThreadRunCompleted{
Run: run,
streamEvent: streamEvent,
}
}
case "thread.message.delta":
var delta StreamThreadMessageDelta
if err := json.Unmarshal([]byte(event.Data), &delta); err == nil {
delta.streamEvent = streamEvent
s.next = &delta
}
case "thread.run.step.completed":
var runStep RunStep
if err := json.Unmarshal([]byte(event.Data), &runStep); err == nil {
s.next = &StreamRunStepCompleted{
RunStep: runStep,
streamEvent: streamEvent,
}
}
case "thread.message.completed":
var msg Message
if err := json.Unmarshal([]byte(event.Data), &msg); err == nil {
s.next = &StreamThreadMessageCompleted{
Message: msg,
streamEvent: streamEvent,
}
}
case "done":
streamEvent.data = nil
s.next = &StreamDone{
Expand Down
22 changes: 22 additions & 0 deletions stream_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ func TestStreamerV2(t *testing.T) {
Event: "thread.message.delta",
Data: `{"id":"msg_KFiZxHhXYQo6cGFnGjRDHSee","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":"hello"}}]}}`,
},
{
Event: "thread.run.requires_action",
Data: `{"id":"run_oNjmoH9jHSQBSPkuVqfHSaLs","object":"thread.run","created_at":1716281751,"assistant_id":"asst_FDlm0qwiBOu65jhL95yNuRv3","thread_id":"thread_4yCKEOWSRQRofNuzl7Ny3uNs","status":"requires_action","started_at":1716281751,"expires_at":1716282351,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":{"type":"submit_tool_outputs","submit_tool_outputs":{"tool_calls":[{"id":"call_q7J5q7taE0K0x83HRuJxJJjR","type":"function","function":{"name":"lookupDefinition","arguments":"{\"entry\":\"square root of pi\",\"language\":\"en\"}"}}]}},"last_error":null,"model":"gpt-3.5-turbo","instructions":null,"tools":[{"type":"function","function":{"name":"lookupDefinition","description":"Lookup the definition of an entry. e.g. word, short phrase, person, place, or term","parameters":{"properties":{"entry":{"description":"The entry to lookup","type":"string"},"language":{"description":"ISO 639-1 language code, e.g., 'en' for English, 'zh' for Chinese","type":"string"}},"type":"object"}}}],"tool_resources":{"code_interpreter":{"file_ids":[]}},"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto"}`,
},
{
Event: "thread.run.completed",
Data: `{"id":"run_o14scUSKGFFRrwhsfGkh2pMJ","object":"thread.run","created_at":1716281844,"assistant_id":"asst_FDlm0qwiBOu65jhL95yNuRv3","thread_id":"thread_732uu0FpoLAGrOlxAz8syqD0","status":"completed","started_at":1716281844,"expires_at":null,"cancelled_at":null,"failed_at":null,"completed_at":1716281845,"required_action":null,"last_error":null,"model":"gpt-3.5-turbo","instructions":null,"tools":[{"type":"function","function":{"name":"lookupDefinition","description":"Lookup the definition of an entry. e.g. word, short phrase, person, place, or term","parameters":{"properties":{"entry":{"description":"The entry to lookup","type":"string"},"language":{"description":"ISO 639-1 language code, e.g., 'en' for English, 'zh' for Chinese","type":"string"}},"type":"object"}}}],"tool_resources":{"code_interpreter":{"file_ids":[]}},"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":{"prompt_tokens":300,"completion_tokens":24,"total_tokens":324},"response_format":"auto","tool_choice":"auto"}`,
},
{
Event: "thread.run.step.completed",
Data: `{"id":"step_9UKPyHGdL6VczTfigS5bdGQb","object":"thread.run.step","created_at":1716281845,"run_id":"run_o14scUSKGFFRrwhsfGkh2pMJ","assistant_id":"asst_FDlm0qwiBOu65jhL95yNuRv3","thread_id":"thread_732uu0FpoLAGrOlxAz8syqD0","type":"message_creation","status":"completed","cancelled_at":null,"completed_at":1716281845,"expires_at":1716282444,"failed_at":null,"last_error":null,"step_details":{"type":"message_creation","message_creation":{"message_id":"msg_Hb14QXWwPWEiMJ12L8Spa3T9"}},"usage":{"prompt_tokens":300,"completion_tokens":24,"total_tokens":324}}`,
},
{
Event: "thread.message.completed",
Data: `{"id":"msg_Hb14QXWwPWEiMJ12L8Spa3T9","object":"thread.message","created_at":1716281845,"assistant_id":"asst_FDlm0qwiBOu65jhL95yNuRv3","thread_id":"thread_732uu0FpoLAGrOlxAz8syqD0","run_id":"run_o14scUSKGFFRrwhsfGkh2pMJ","status":"completed","incomplete_details":null,"incomplete_at":null,"completed_at":1716281845,"role":"assistant","content":[{"type":"text","text":{"value":"Sure! Here you go:\n\nWhy couldn't the leopard play hide and seek?\n\nBecause he was always spotted!","annotations":[]}}],"attachments":[],"metadata":{}}`,
},
{
Event: "done",
Data: "[DONE]",
Expand Down Expand Up @@ -148,6 +164,12 @@ func TestStreamerV2(t *testing.T) {
}

jsonEqual(t, []byte(tc.Data), delta)

Check failure on line 166 in stream_v2_test.go

View workflow job for this annotation

GitHub Actions / Sanity check

Error return value is not checked (errcheck)
case *openai.StreamThreadRunRequiresAction:
jsonEqual(t, []byte(tc.Data), event.Run)

Check failure on line 168 in stream_v2_test.go

View workflow job for this annotation

GitHub Actions / Sanity check

Error return value is not checked (errcheck)
case *openai.StreamThreadRunCompleted:
jsonEqual(t, []byte(tc.Data), event.Run)

Check failure on line 170 in stream_v2_test.go

View workflow job for this annotation

GitHub Actions / Sanity check

Error return value is not checked (errcheck)
case *openai.StreamRunStepCompleted:
jsonEqual(t, []byte(tc.Data), event.RunStep)

Check failure on line 172 in stream_v2_test.go

View workflow job for this annotation

GitHub Actions / Sanity check

Error return value is not checked (errcheck)
case *openai.StreamDone:
if event.JSON() != nil {
t.Fatalf("Expected JSON data to be nil, but got %s", string(event.JSON()))
Expand Down

0 comments on commit f8c9b69

Please sign in to comment.