Skip to content

Commit

Permalink
Fixes for multiple responses, chat request and checks on responses
Browse files Browse the repository at this point in the history
  • Loading branch information
JexSrs committed May 23, 2024
1 parent e1b2538 commit b17794a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 39 deletions.
2 changes: 1 addition & 1 deletion ChatRequestBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type ChatRequestBuilder struct {
Format *string `json:"format"`
Images []string `json:"images"`
Raw *bool `json:"raw"`
Messages []Message `json:"message"`
Messages []Message `json:"messages"`

Options *Options `json:"options"`
}
Expand Down
10 changes: 10 additions & 0 deletions ModelFileBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ type Parameter struct {
Value string
}

// WithName sets the new model's name for this request.
//
// Parameters:
// - v: The model name.
func (c *CreateModelFunc) WithName(v string) func(*ModelFileBuilder) {
return func(r *ModelFileBuilder) {
r.Name = &v
}
}

// WithStream passes a function to allow reading stream types.
//
// Parameters:
Expand Down
18 changes: 15 additions & 3 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ollama
import (
"bytes"
json2 "encoding/json"
"fmt"
"io"
)

Expand Down Expand Up @@ -152,8 +153,13 @@ func (o *Ollama) newChatFunc() ChatFunc {
final.Done = r.Done
}

final.Message.Content = pointer(*final.Message.Content + *r.Message.Content)
final.Message.Images = append(final.Message.Images, r.Message.Images...)
if r.Message.Content != nil {
final.Message.Content = pointer(*final.Message.Content + *r.Message.Content)
}

if r.Message.Images != nil && len(r.Message.Images) > 0 {
final.Message.Images = append(final.Message.Images, r.Message.Images...)
}

if i == len(resp)-1 {
final.TotalDuration = r.TotalDuration
Expand Down Expand Up @@ -423,7 +429,13 @@ func (o *Ollama) newPullModelFunc() PullModelFunc {

final := &StatusResponse{}
for _, r := range resp {
final.Status += r.Status + "\n"
if len(r.Status) != 0 {
final.Status += r.Status + "\n"
}

if len(r.Error) != 0 {
final.Error += r.Error + "\n"
}
}

return final, nil
Expand Down
54 changes: 19 additions & 35 deletions ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,48 +100,32 @@ func (o *Ollama) Do(path string, data interface{}, maxBufferSize int, streamFunc
}
defer resp.Body.Close()

if streamFunc != nil {
var res [][]byte
var buffer bytes.Buffer

for {
buf := make([]byte, maxBufferSize)
n, err := resp.Body.Read(buf)
if err != nil && err != io.EOF {
return nil, err
}

if n == 0 {
break
}
var res [][]byte
var buffer bytes.Buffer

for {
buf := make([]byte, maxBufferSize)
n, err := resp.Body.Read(buf)
if err != nil && err != io.EOF {
return nil, err
}

chunk := buf[:n]
if n == 0 {
break
}

bigChunk := splitJSONObjects(buf[:n])
for _, chunk := range bigChunk {
res = append(res, chunk)
buffer.Write(chunk)
streamFunc(chunk)
}

//// Process buffered data to handle complete JSON objects
//decoder := json.NewDecoder(resp.Body)
//for buffer.Len() > 0 {
// var jsonObj map[string]interface{}
// err := decoder.Decode(&jsonObj)
// if err != nil {
// if err == io.EOF {
// break
// }
// return nil, err
// }
// // Here you can handle each JSON object as needed
// fmt.Printf("JSON object: %v\n", jsonObj)
//}

return res, nil
if streamFunc != nil {
streamFunc(chunk)
}
}
}

b, err := io.ReadAll(resp.Body)
return [][]byte{b}, nil
return res, nil
}

// Request performs an HTTP request to the Ollama API.
Expand Down
1 change: 1 addition & 0 deletions responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ type ShowModelInfoResponse struct {
// StatusResponse represents the API response for endpoint that return status updates.
type StatusResponse struct {
Status string `json:"status"`
Error string `json:"error"`
}
33 changes: 33 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,36 @@ func buildUrl(baseUrl, path string) string {
url += path
return url
}

func splitJSONObjects(data []byte) [][]byte {
var results [][]byte
var stack []byte
var start, end int
var inString bool

for i := 0; i < len(data); i++ {
switch data[i] {
case '{':
if !inString {
if len(stack) == 0 {
start = i
}
stack = append(stack, '{')
}
case '}':
if !inString {
stack = stack[:len(stack)-1]
if len(stack) == 0 {
end = i + 1
results = append(results, data[start:end])
}
}
case '"':
if i == 0 || data[i-1] != '\\' {
inString = !inString
}
}
}

return results
}

0 comments on commit b17794a

Please sign in to comment.