Skip to content

Commit

Permalink
Merge pull request #216 from ibuildthecloud/main
Browse files Browse the repository at this point in the history
fix: reading tool from stdin using 'gptscript -'
  • Loading branch information
ibuildthecloud authored Apr 4, 2024
2 parents d79063b + 6712734 commit d9f1c0d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
5 changes: 1 addition & 4 deletions pkg/cli/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,7 @@ func (r *GPTScript) readProgram(ctx context.Context, args []string) (prg types.P
if err != nil {
return prg, err
}
prg, err = loader.ProgramFromSource(ctx, string(data), r.SubTool)
if err != nil {
return prg, err
}
return loader.ProgramFromSource(ctx, string(data), r.SubTool)
}

return loader.Program(ctx, args[0], r.SubTool)
Expand Down
35 changes: 31 additions & 4 deletions pkg/openai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,8 @@ func (c *Client) fromCache(ctx context.Context, messageRequest types.CompletionR

func toToolCall(call types.CompletionToolCall) openai.ToolCall {
return openai.ToolCall{
Index: call.Index,
ID: call.ID,
Type: openai.ToolTypeFunction,
ID: call.ID,
Type: openai.ToolTypeFunction,
Function: openai.FunctionCall{
Name: call.Function.Name,
Arguments: call.Function.Arguments,
Expand Down Expand Up @@ -471,7 +470,7 @@ func (c *Client) store(ctx context.Context, key string, responses []openai.ChatC

func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest, transactionID string, partial chan<- types.CompletionStatus) (responses []openai.ChatCompletionStreamResponse, _ error) {
cacheKey := c.cacheKey(request)
request.Stream = true
request.Stream = os.Getenv("GPTSCRIPT_INTERNAL_OPENAI_STREAMING") != "false"

partial <- types.CompletionStatus{
CompletionID: transactionID,
Expand All @@ -482,6 +481,34 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
}

slog.Debug("calling openai", "message", request.Messages)

if !request.Stream {
resp, err := c.c.CreateChatCompletion(ctx, request)
if err != nil {
return nil, err
}
return []openai.ChatCompletionStreamResponse{
{
ID: resp.ID,
Object: resp.Object,
Created: resp.Created,
Model: resp.Model,
Choices: []openai.ChatCompletionStreamChoice{
{
Index: resp.Choices[0].Index,
Delta: openai.ChatCompletionStreamChoiceDelta{
Content: resp.Choices[0].Message.Content,
Role: resp.Choices[0].Message.Role,
FunctionCall: resp.Choices[0].Message.FunctionCall,
ToolCalls: resp.Choices[0].Message.ToolCalls,
},
FinishReason: resp.Choices[0].FinishReason,
},
},
},
}, nil
}

stream, err := c.c.CreateChatCompletionStream(ctx, request)
if err != nil {
return nil, err
Expand Down

0 comments on commit d9f1c0d

Please sign in to comment.