AlexStocks commented on code in PR #659:
URL: https://github.com/apache/dubbo-go-pixiu/pull/659#discussion_r2073308627


##########
pkg/filter/tokenizer/tokenizer.go:
##########
@@ -0,0 +1,194 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tokenizer
+
+import (
+       "bufio"
+       "encoding/json"
+       "io"
+       "strings"
+       "sync"
+)
+
+import (
+       "github.com/apache/dubbo-go-pixiu/pkg/client"
+       "github.com/apache/dubbo-go-pixiu/pkg/common/constant"
+       "github.com/apache/dubbo-go-pixiu/pkg/common/extension/filter"
+       "github.com/apache/dubbo-go-pixiu/pkg/context/http"
+       "github.com/apache/dubbo-go-pixiu/pkg/logger"
+)
+
+const (
+       Kind      = constant.LLMTokenizerFilter
+       LoggerFmt = "[Tokenizer] [DOWNSTREAM] "
+)
+
+func init() {
+       filter.RegisterHttpFilter(&Plugin{})
+}
+
+type (
+       // Plugin is http filter plugin.
+       Plugin struct {
+       }
+       // FilterFactory is http filter instance
+       FilterFactory struct {
+               cfg *Config
+       }
+       // Filter is http filter instance
+       Filter struct {
+               cfg *Config
+       }
+       // Config describe the config of FilterFactory
+       Config struct {
+       }
+)
+
+func (p *Plugin) Kind() string {
+       return Kind
+}
+
+func (p *Plugin) CreateFilterFactory() (filter.HttpFilterFactory, error) {
+       return &FilterFactory{cfg: &Config{}}, nil
+}
+
+func (factory *FilterFactory) Config() interface{} {
+       return factory.cfg
+}
+
+func (factory *FilterFactory) Apply() error {
+       return nil
+}
+
+func (factory *FilterFactory) PrepareFilterChain(ctx *http.HttpContext, chain 
filter.FilterChain) error {
+       f := &Filter{
+               cfg: factory.cfg,
+       }
+       chain.AppendEncodeFilters(f)
+       return nil
+}
+
+func (f *Filter) Encode(hc *http.HttpContext) filter.FilterStatus {
+       switch res := hc.TargetResp.(type) {
+       case *client.StreamResponse:
+               pr, pw := io.Pipe()
+               res.Stream = newTeeReadCloser(res.Stream, pw)
+               go f.processStreamResponse(pr)
+       case *client.UnaryResponse:
+               f.processUsageData(res.Data)
+       default:
+               logger.Infof(LoggerFmt+"Response type not suitable for token 
calc: %T", res)
+       }
+
+       return filter.Continue
+}
+
+func (f *Filter) processStreamResponse(stream io.Reader) {
+       scanner := bufio.NewScanner(stream)
+       for scanner.Scan() {
+               line := scanner.Text()
+               if strings.HasPrefix(line, "data:") {
+                       line = strings.TrimPrefix(line, "data:")
+                       f.processUsageData([]byte(line))
+               }
+       }
+       if err := scanner.Err(); err != nil && err != io.EOF {
+               logger.Errorf(LoggerFmt+"Error reading stream: %v", err)
+       }
+}
+
+func (f *Filter) processUsageData(data []byte) {
+       var dataCont map[string]interface{}
+       err := json.Unmarshal(data, &dataCont)
+       if err != nil {
+               return
+       }
+
+       usage, ok := dataCont["usage"].(map[string]interface{})
+       if !ok || usage == nil {
+               return
+       }
+
+       // todo: currently we only log the usage, we should export it to metrics
+       f.logUsage(usage)
+}
+
+func (f *Filter) logUsage(usage map[string]interface{}) {
+       for key, value := range usage {
+               if key == "prompt_tokens_details" {
+                       promptTokensDetails, ok := 
value.(map[string]interface{})
+                       if !ok {
+                               logger.Warnf(LoggerFmt+"prompt_tokens_details 
is not a map, value: %+v", value)
+                               continue
+                       }
+                       for detailKey, detailValue := range promptTokensDetails 
{
+                               logger.Infof(LoggerFmt+"Usage | %s: %v", 
detailKey, detailValue)
+                       }
+               } else {
+                       logger.Infof(LoggerFmt+"Usage | %s: %v", key, value)
+               }
+       }
+}
+
+type teeReadCloser struct {
+       reader   io.Reader
+       closer   io.Closer
+       writer   io.Writer
+       once     sync.Once
+       closeErr error
+}
+
+func newTeeReadCloser(r io.ReadCloser, w io.Writer) *teeReadCloser {
+       return &teeReadCloser{
+               reader: r,
+               closer: r,
+               writer: w,
+       }
+}
+
+func (t *teeReadCloser) Read(p []byte) (n int, err error) {

Review Comment:
        n, err = t.reader.Read(p)
        if n <= 0 {
             return
           }
           nw, ew := t.writer.Write(p[:n])
           if ew != nil {
                logger.Errorf(LoggerFmt+"Error writing to tee writer: %v", ew)
           }
           if nw != n {
                logger.Errorf(LoggerFmt+"Short write to tee writer: %d/%d", nw, 
n)
           }



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to