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

Feat: chat regenerate and proceed #26

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ class ChatGPT {
* @param message - The plaintext message to send.
* @param opts.conversationId - Optional ID of the previous message in a conversation
*/
async sendMessage(conversation: Conversation): Promise<Required<Conversation>> {
async sendMessage(conversation: Conversation, action: string): Promise<Required<Conversation>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async sendMessage(conversation: Conversation, action: string): Promise<Required<Conversation>> {
async sendMessage(conversation: Conversation, action: 'next' | 'variant' | 'continue'): Promise<Required<Conversation>> {

Would be better for the typing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And you can consider to put action into Conversation type as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary. Because I think conversation represents the context of the chat, and I don't think action is part of the chat context, so I put it in the parameters of sendMessage.

const { conversationId, messageId = uuidv4(), message } = conversation

const accessToken = await this.refreshAccessToken()

const body: types.ConversationJSONBody = {
action: 'next',
action,
conversation_id: conversationId,
messages: [
{
Expand Down
22 changes: 17 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ChatGPT from './api'
import ChatGPT, { Conversation } from './api'
import { Context, Logger, Schema, Session, SessionError } from 'koishi'

const logger = new Logger('chatgpt')
Expand Down Expand Up @@ -37,7 +37,7 @@ export const Config: Schema<Config> = Schema.intersect([
}),
])

const conversations = new Map<string, { messageId: string; conversationId: string }>()
const conversations = new Map<string, Conversation>()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really good to use type Conversation here, as we don't save messages in the map.
If you stick with a type, you can split them into two types, but I argue with that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your specific idea? My idea is to use the Conversation type here and save it in a Map. I can't think of any other idea at the moment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just to revert this type would be good to me.
Otherwise you should write something like:

export interface Conversation {
  messageId: string
  conversationId: string
}

export interface ConversationMessage extends Conversation {
  message: string
}

That would be much much redundant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So how do I save the chat context, which is necessary in the implementation of variant?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or

Suggested change
const conversations = new Map<string, Conversation>()
const conversations = new Map<string, Omit<Conversation, 'message'>>()

while Omit is a built-in type of typescript.


export function apply(ctx: Context, config: Config) {
ctx.i18n.define('zh', require('./locales/zh-CN'))
Expand Down Expand Up @@ -92,9 +92,21 @@ export function apply(ctx: Context, config: Config) {

try {
// send a message and wait for the response
const { conversationId, messageId } = conversations.get(key) ?? {}
const response = await api.sendMessage({ message: input, conversationId, messageId })
conversations.set(key, { conversationId: response.conversationId, messageId: response.messageId })
const conversation: Conversation = conversations.get(key) ?? { message: '', conversationId: undefined, messageId: undefined }
Seidko marked this conversation as resolved.
Show resolved Hide resolved
let action = 'next'
switch (input) {
case '重新说':
action = 'variant'
break
case '继续说':
action = 'continue'
break
default:
conversation.message = input
}

Seidko marked this conversation as resolved.
Show resolved Hide resolved
const response = await api.sendMessage(conversation, action)
conversations.set(key, { message: input, conversationId: response.conversationId, messageId: response.messageId })
return response.message
} catch (error) {
logger.warn(error)
Expand Down