Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
9e2de1bd
Unverified
Commit
9e2de1bd
authored
Oct 04, 2023
by
Bruce MacDonald
Committed by
GitHub
Oct 04, 2023
Browse files
increase streaming buffer size (#692)
parent
dc87e9c9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
3 deletions
+11
-3
api/client.go
api/client.go
+6
-3
llm/llama.go
llm/llama.go
+5
-0
No files found.
api/client.go
View file @
9e2de1bd
...
@@ -18,9 +18,7 @@ import (
...
@@ -18,9 +18,7 @@ import (
const
DefaultHost
=
"127.0.0.1:11434"
const
DefaultHost
=
"127.0.0.1:11434"
var
(
var
envHost
=
os
.
Getenv
(
"OLLAMA_HOST"
)
envHost
=
os
.
Getenv
(
"OLLAMA_HOST"
)
)
type
Client
struct
{
type
Client
struct
{
Base
url
.
URL
Base
url
.
URL
...
@@ -123,6 +121,8 @@ func (c *Client) do(ctx context.Context, method, path string, reqData, respData
...
@@ -123,6 +121,8 @@ func (c *Client) do(ctx context.Context, method, path string, reqData, respData
return
nil
return
nil
}
}
const
maxBufferSize
=
512
*
1024
// 512KB
func
(
c
*
Client
)
stream
(
ctx
context
.
Context
,
method
,
path
string
,
data
any
,
fn
func
([]
byte
)
error
)
error
{
func
(
c
*
Client
)
stream
(
ctx
context
.
Context
,
method
,
path
string
,
data
any
,
fn
func
([]
byte
)
error
)
error
{
var
buf
*
bytes
.
Buffer
var
buf
*
bytes
.
Buffer
if
data
!=
nil
{
if
data
!=
nil
{
...
@@ -151,6 +151,9 @@ func (c *Client) stream(ctx context.Context, method, path string, data any, fn f
...
@@ -151,6 +151,9 @@ func (c *Client) stream(ctx context.Context, method, path string, data any, fn f
defer
response
.
Body
.
Close
()
defer
response
.
Body
.
Close
()
scanner
:=
bufio
.
NewScanner
(
response
.
Body
)
scanner
:=
bufio
.
NewScanner
(
response
.
Body
)
// increase the buffer size to avoid running out of space
scanBuf
:=
make
([]
byte
,
0
,
maxBufferSize
)
scanner
.
Buffer
(
scanBuf
,
maxBufferSize
)
for
scanner
.
Scan
()
{
for
scanner
.
Scan
()
{
var
errorResponse
struct
{
var
errorResponse
struct
{
Error
string
`json:"error,omitempty"`
Error
string
`json:"error,omitempty"`
...
...
llm/llama.go
View file @
9e2de1bd
...
@@ -438,6 +438,8 @@ type PredictRequest struct {
...
@@ -438,6 +438,8 @@ type PredictRequest struct {
Stop
[]
string
`json:"stop,omitempty"`
Stop
[]
string
`json:"stop,omitempty"`
}
}
const
maxBufferSize
=
512
*
1024
// 512KB
func
(
llm
*
llama
)
Predict
(
ctx
context
.
Context
,
prevContext
[]
int
,
prompt
string
,
fn
func
(
api
.
GenerateResponse
))
error
{
func
(
llm
*
llama
)
Predict
(
ctx
context
.
Context
,
prevContext
[]
int
,
prompt
string
,
fn
func
(
api
.
GenerateResponse
))
error
{
prevConvo
,
err
:=
llm
.
Decode
(
ctx
,
prevContext
)
prevConvo
,
err
:=
llm
.
Decode
(
ctx
,
prevContext
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -498,6 +500,9 @@ func (llm *llama) Predict(ctx context.Context, prevContext []int, prompt string,
...
@@ -498,6 +500,9 @@ func (llm *llama) Predict(ctx context.Context, prevContext []int, prompt string,
}
}
scanner
:=
bufio
.
NewScanner
(
resp
.
Body
)
scanner
:=
bufio
.
NewScanner
(
resp
.
Body
)
// increase the buffer size to avoid running out of space
buf
:=
make
([]
byte
,
0
,
maxBufferSize
)
scanner
.
Buffer
(
buf
,
maxBufferSize
)
for
scanner
.
Scan
()
{
for
scanner
.
Scan
()
{
select
{
select
{
case
<-
ctx
.
Done
()
:
case
<-
ctx
.
Done
()
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment