Skip to content

Commit

Permalink
update service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Nov 27, 2023
1 parent d6a02dc commit 6a0bf4b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
)

var defaultMaxRetries = 3
var midjourneyMaxRetries = 10

type RequestProps struct {
MaxRetries *int
Expand Down
7 changes: 4 additions & 3 deletions adapter/midjourney/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func (c *ChatInstance) CreateImagineRequest(prompt string) (*ImagineResponse, er
return utils.MapToStruct[ImagineResponse](res), nil
}

func getStatusCode(code int) error {
func getStatusCode(response *ImagineResponse) error {
code := response.Code
switch code {
case SuccessCode, QueueCode:
return nil
Expand All @@ -42,7 +43,7 @@ func getStatusCode(code int) error {
case NudeCode:
return fmt.Errorf("prompt violates the content policy of midjourney, the request is rejected")
default:
return fmt.Errorf("unknown error from midjourney")
return fmt.Errorf(fmt.Sprintf("unknown error from midjourney (code: %d, description: %s)", code, response.Description))
}
}

Expand All @@ -57,7 +58,7 @@ func (c *ChatInstance) CreateStreamImagineTask(prompt string, hook func(progress
return "", err
}

if err := getStatusCode(res.Code); err != nil {
if err := getStatusCode(res); err != nil {
return "", err
}

Expand Down
7 changes: 5 additions & 2 deletions adapter/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ func isQPSOverLimit(model string, err error) bool {
}
}

func getRetries(retries *int) int {
func getRetries(model string, retries *int) int {
if retries == nil {
if globals.IsMidjourneyModel(model) {
return midjourneyMaxRetries
}
return defaultMaxRetries
}

Expand All @@ -31,7 +34,7 @@ func getRetries(retries *int) int {
func NewChatRequest(props *ChatProps, hook globals.Hook) error {
err := createChatRequest(props, hook)

retries := getRetries(props.MaxRetries)
retries := getRetries(props.Model, props.MaxRetries)
props.Current++

if IsAvailableError(err) {
Expand Down
7 changes: 4 additions & 3 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=3.0, user-scalable=yes" />
<title>Chat Nio</title>
<meta name="keywords" content="ChatGPT, AI聊天, ChatNio, chatnio">
<meta name="description" content="👋 Chat Nio, lightweight Web ChatGPT chat site 👋 Chat Nio, 一个轻量级的联网版 AI 聊天网站">
<meta name="keywords" content="ChatGPT, ChatNio, chatnio">
<meta name="description" content="👋 Chat Nio, Your Next Powerful AI Chat Platform. Creating Possibilities in the World of AI Chat.">
<meta name="author" content="Deeptrain Team">
<meta name="theme-color" content="#000000">
<meta itemprop="image" content="/favicon.ico">
<meta name="baidu-site-verification" content="codeva-TJkbi40ZBi" />
<link href="https://open.lightxi.com/fonts/Andika" rel="stylesheet">
<link href="https://open.lightxi.com/fonts/Jetbrains-Mono" rel="stylesheet">
<link href="https://open.lightxi.com/jsdelivr/npm/[email protected]/dist/katex.min.css" rel="stylesheet">
<script src="/workbox.js" defer></script>
</head>
<body>
<div id="root"></div>
<noscript>You need to enable JavaScript to run chatnio.</noscript>
<noscript>You need to enable JavaScript to run this app.</noscript>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
24 changes: 24 additions & 0 deletions app/public/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

const SERVICE_NAME = "chatnio";

self.addEventListener('activate', function (event) {
console.debug("[service] service worker activated");
});

self.addEventListener('install', function (event) {
event.waitUntil(
caches.open(SERVICE_NAME)
.then(function (cache) {
return cache.addAll([]);
})
);
});

self.addEventListener('fetch', function (event) {
event.respondWith(
caches.match(event.request)
.then(function (response) {
return response || fetch(event.request);
})
);
});
10 changes: 10 additions & 0 deletions app/public/workbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register('/service.js').then(function (registration) {
console.debug(`[service] service worker registered with scope: ${registration.scope}`);
}, function (err) {
console.debug(`[service] service worker registration failed: ${err}`);
});
});
}
6 changes: 3 additions & 3 deletions utils/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ func CountOutputToken(model string, t int) float32 {
case globals.StableDiffusion:
return 0.25
case globals.Midjourney:
return 0.5
return 0.1
case globals.MidjourneyFast:
return 2
return 0.5
case globals.MidjourneyTurbo:
return 5
return 1
case globals.Dalle3:
return 5.6
default:
Expand Down

0 comments on commit 6a0bf4b

Please sign in to comment.