Skip to content

Commit

Permalink
Clean up throttle rates, throttle the agent
Browse files Browse the repository at this point in the history
  • Loading branch information
cephalization committed Oct 19, 2023
1 parent d661c52 commit 01cbad4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions apps/messages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@neondatabase/serverless": "^0.6.0",
"database": "*",
"dotenv": "^16.3.1",
"just-throttle": "^4.2.0",
"mclient": "*",
"openai": "^4.12.3",
"partyrpc": "*",
Expand Down
21 changes: 19 additions & 2 deletions apps/messages/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Pool } from '@neondatabase/serverless';
import { getServerlessDb } from 'database/drizzle';
import invariant from 'tiny-invariant';
import type { ChatCompletionMessageParam } from 'openai/resources';
import throttle from 'just-throttle';

const connectionRequestBodySchema = valibot.object({
/** The connect action */
Expand Down Expand Up @@ -190,18 +191,34 @@ export default class Agent implements Party.Server {
stream: true
});

const sendEdit = throttle(
(content: string) => {
this.client?.send({
type: 'editMessage',
messageId: newMessagePlaceholder.id,
content,
updatedAt: Dates.getCurrentDateInUTC()
});
},
128,
{ trailing: true, leading: true }
);

let text = '';
for await (const part of stream) {
const delta = part.choices?.[0]?.delta?.content;
if (delta) {
if (delta !== null && delta !== undefined) {
text = `${text}${delta}`;
console.log(delta);
sendEdit(text);
} else {
// dump the final text when done
this.client?.send({
type: 'editMessage',
messageId: newMessagePlaceholder.id,
content: text,
updatedAt: Dates.getCurrentDateInUTC()
});
} else {
this.responding = false;
}
}
Expand Down

0 comments on commit 01cbad4

Please sign in to comment.