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

fix: ai service race #768

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions pkg/bridge/ai/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"net"
"net/http"
"sync"
"time"

gonanoid "github.com/matoous/go-nanoid/v2"
Expand Down Expand Up @@ -127,14 +126,6 @@ func HandleInvoke(w http.ResponseWriter, r *http.Request) {
return
}

ci := &CacheItem{
wg: &sync.WaitGroup{},
ResponseWriter: w,
}
if _, ok := service.cache[reqID]; !ok {
service.cache[reqID] = ci
}

var req ai.InvokeRequest
req.ReqID = reqID

Expand Down
38 changes: 17 additions & 21 deletions pkg/bridge/ai/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ai

import (
"fmt"
"net/http"
"sync"
"time"

Expand All @@ -24,27 +23,19 @@ var (
services *expirable.LRU[string, *Service]
)

// CacheItem cache the http.ResponseWriter, which is used for writing response from reducer.
// TODO: http.ResponseWriter is from the SimpleRestfulServer interface, should be decoupled
// from here.
type CacheItem struct {
ResponseWriter http.ResponseWriter
wg *sync.WaitGroup
mu sync.Mutex
}

// Service is used to invoke LLM Provider to get the functions to be executed,
// then, use source to send arguments which returned by llm provider to target
// function. Finally, use reducer to aggregate all the results, and write the
// result by the http.ResponseWriter.
type Service struct {
credential string
zipperAddr string
md metadata.M
source yomo.Source
reducer yomo.StreamFunction
cache map[string]*CacheItem
credential string
zipperAddr string
md metadata.M
source yomo.Source
reducer yomo.StreamFunction
// cache map[string]*CacheItem
sfnCallCache map[string]*sfnAsyncCall
muCallCache sync.Mutex
LLMProvider
}

Expand Down Expand Up @@ -72,9 +63,9 @@ func DefaultExchangeMetadataFunc(credential string) (metadata.M, error) {

func newService(credential string, zipperAddr string, aiProvider LLMProvider, exFn ExchangeMetadataFunc) (*Service, error) {
s := &Service{
credential: credential,
zipperAddr: zipperAddr,
cache: make(map[string]*CacheItem),
credential: credential,
zipperAddr: zipperAddr,
// cache: make(map[string]*CacheItem),
LLMProvider: aiProvider,
sfnCallCache: make(map[string]*sfnAsyncCall),
}
Expand Down Expand Up @@ -116,7 +107,6 @@ func (s *Service) Release() {
if s.reducer != nil {
s.reducer.Close()
}
clear(s.cache)
}

func (s *Service) createSource() (yomo.Source, error) {
Expand Down Expand Up @@ -156,7 +146,9 @@ func (s *Service) createReducer() (yomo.StreamFunction, error) {
reqID := invoke.ReqID

// write parallel function calling results to cache, after all the results are written, the reducer will be done
s.muCallCache.Lock()
c, ok := s.sfnCallCache[reqID]
s.muCallCache.Unlock()
if !ok {
ylog.Error("[sfn-reducer] req_id not found", "req_id", reqID)
return
Expand Down Expand Up @@ -242,7 +234,9 @@ func (s *Service) runFunctionCalls(fns map[uint32][]*ai.ToolCall, reqID string)
wg: &sync.WaitGroup{},
val: make(map[string]ai.ToolMessage),
}
s.muCallCache.Lock()
s.sfnCallCache[reqID] = asyncCall
s.muCallCache.Unlock()

for tag, tcs := range fns {
ylog.Debug("+++invoke toolCalls", "tag", tag, "len(toolCalls)", len(tcs), "reqID", reqID)
Expand All @@ -262,11 +256,13 @@ func (s *Service) runFunctionCalls(fns map[uint32][]*ai.ToolCall, reqID string)

arr := make([]ai.ToolMessage, 0)

asyncCall.mu.RLock()
for _, call := range asyncCall.val {
ylog.Debug("---invoke done", "id", call.ToolCallId, "content", call.Content)
call.Role = "tool"
arr = append(arr, call)
}
asyncCall.mu.RUnlock()

return arr, nil
}
Expand Down Expand Up @@ -307,6 +303,6 @@ func init() {

type sfnAsyncCall struct {
wg *sync.WaitGroup
mu sync.Mutex
mu sync.RWMutex
val map[string]ai.ToolMessage
}