From b3bc527841fc3e7d11eca31cf962493eee2e15cc Mon Sep 17 00:00:00 2001 From: CDFMLR Date: Thu, 9 Mar 2023 21:43:21 +0800 Subject: [PATCH] add V3(official) ChatGPT API. Use unfied Chatbot gRPC api proto changed to: https://github.com/cdfmlr/muvtuber-proto/commit/183c5dd47545a32c1802a15d011dcf72a98627e6 solve #1 --- .gitignore | 5 +- README.md | 14 +-- chatgpt/chatbot.py | 113 +++++++++++++++--- chatgpt/grpcapi.py | 40 ++++--- chatgpt/httpapi.py | 6 +- chatgpt/protos/chatbot_pb2.py | 39 ++++++ ...hatbot_pb2_grpc.py => chatbot_pb2_grpc.py} | 66 +++++----- chatgpt/protos/chatgpt_chatbot_pb2.py | 39 ------ 8 files changed, 204 insertions(+), 118 deletions(-) create mode 100644 chatgpt/protos/chatbot_pb2.py rename chatgpt/protos/{chatgpt_chatbot_pb2_grpc.py => chatbot_pb2_grpc.py} (57%) delete mode 100644 chatgpt/protos/chatgpt_chatbot_pb2.py diff --git a/.gitignore b/.gitignore index 5a0f2f1..bbb51d6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.idea + ### https://github.com/github/gitignore/blob/main/Global/macOS.gitignore # General .DS_Store @@ -5,7 +7,8 @@ .LSOverride # Icon must end with two \r -Icon +Icon + # Thumbnails ._* diff --git a/README.md b/README.md index 410494a..43571fc 100644 --- a/README.md +++ b/README.md @@ -47,20 +47,20 @@ options: ### 请求 ```sh -$ grpcurl -d '{"access_token": "eyJ***99A", "initial_prompt": "..."}' -plaintext localhost:50052 muvtuber.chatbot.chatgpt_chatbot.v1.ChatGPTService.NewSession - +$ grpcurl -d '{"config": "{\"version\": 3, \"api_key\": \"sk-xxxx\"}", "initial_prompt": "你好"}' -plaintext localhost:50052 muvtuber.chatbot.v2.ChatbotService.NewSession { - "sessionId": "b7268187-ab7a-4e2d-9d4a-0161975369bd" + "sessionId": "2617613c-9f20-4d6c-b47e-1622392a134e", + "initialResponse": "你好!我很高兴能和你交流。有什么我可以帮助你的吗?" } -$ grpcurl -d '{"session_id": "b7268187-ab7a-4e2d-9d4a-0161975369bd", "prompt": "hello!!"}' -plaintext localhost:50052 muvtuber.chatbot.chatgpt_chatbot.v1.ChatGPTService.Chat +$ grpcurl -d '{"session_id": "2617613c-9f20-4d6c-b47e-1622392a134e", "prompt": "hello!!"}' -plaintext localhost:50052 muvtuber.chatbot.v2.ChatbotService.Chat { "response": "Hello! How can I assist you today?" } -$ grpcurl -d '{"session_id": "b7268187-ab7a-4e2d-9d4a-0161975369bd"}' -plaintext localhost:50052 muvtuber.chatbot.chatgpt_chatbot.v1.ChatGPTService.DeleteSession +$ grpcurl -d '{"session_id": "2617613c-9f20-4d6c-b47e-1622392a134e"}' -plaintext localhost:50052 muvtuber.chatbot.v2.ChatbotService.DeleteSession { - "sessionId": "b7268187-ab7a-4e2d-9d4a-0161975369bd" + "sessionId": "2617613c-9f20-4d6c-b47e-1622392a134e" } ``` @@ -68,7 +68,7 @@ $ grpcurl -d '{"session_id": "b7268187-ab7a-4e2d-9d4a-0161975369bd"}' -plaintext ```md - NewSession - - INVALID_ARGUMENT: access_token is required + - INVALID_ARGUMENT: version & access_token|api_key is required - RESOURCE_EXHAUSTED: TooManySessions (该系统内 MultiChatGPT 的最大会话数限制) - UNAVAILABLE: ChatGPTError (向 ChatGPT 请求 initial_prompt 时出错) - Chat diff --git a/chatgpt/chatbot.py b/chatgpt/chatbot.py index d2897f9..bf4187b 100644 --- a/chatgpt/chatbot.py +++ b/chatgpt/chatbot.py @@ -1,10 +1,13 @@ -from dataclasses import dataclass import logging +from abc import ABCMeta, abstractmethod +from dataclasses import dataclass import os +from enum import Enum from typing import Dict import uuid from warnings import warn -from revChatGPT.V1 import Chatbot +from revChatGPT.V1 import Chatbot as ChatbotV1 +from revChatGPT.V3 import Chatbot as ChatbotV3 import threading from datetime import datetime @@ -19,24 +22,39 @@ # Cooldown: a decorator to limit the frequency of function calls -class ChatGPT: +class ChatGPT(metaclass=ABCMeta): + @abstractmethod + def ask(self, session_id, prompt): + """Ask ChatGPT with prompt, return response text + + Raises: + ChatGPTError: ChatGPT error + """ + pass + + +# V1 Standard ChatGPT +# Update 2023/03/09 9:50AM - No longer functional +class ChatGPTv1(ChatGPT): def __init__(self, config={'access_token': 'your access token', 'initial_prompt': 'your initial prompt'}): """ChatGPT with config: {access_token, initial_prompt}""" - self.chatbot = Chatbot(config=config) + self.chatbot = ChatbotV1(config=config) self.lock = threading.Lock() # for self.chatbot q = config.get('initial_prompt', None) if q: - a = self.ask(q) + a = self.ask('', q) print(f'{datetime.now()} ChatGPT initial ask: {q} -> {a}') self.initial_response = a else: self.initial_response = None @cooldown(os.getenv("CHATGPT_COOLDOWN", 75)) - def ask(self, prompt) -> str: # raises Exception + def ask(self, session_id, prompt) -> str: # raises Exception """Ask ChatGPT with prompt, return response text + - session_id: unused + Raises: ChatGPTError: ChatGPT error """ @@ -63,25 +81,80 @@ def renew(self, access_token: str): warn("ChatGPT.renew is deprecated", DeprecationWarning) with self.lock: - self.chatbot = Chatbot(config={"access_token": access_token}) + self.chatbot = ChatbotV1(config={"access_token": access_token}) + + +# V3 Official Chat API +# Paid +class ChatGPTv3(ChatGPT): + def __init__(self, config={'api_key': 'your api key', 'initial_prompt': 'your initial prompt'}): + self.chatbot = ChatbotV3(api_key=config.get('api_key', '')) + self.lock = threading.Lock() # for self.chatbot + + q = config.get('initial_prompt', None) + if q: + a = self.ask('', q) + logging.info(f'ChatGPT initial ask: {q} -> {a}') + self.initial_response = a + else: + self.initial_response = None + + + # Rate Limits: 20 RPM / 40,000 TPM + # https://platform.openai.com/docs/guides/rate-limits/overview + # 主要是价格w + # gpt-3.5-turbo: $0.002 / 1K tokens + @cooldown(os.getenv("CHATGPT_V3_COOLDOWN", 30)) + def ask(self, session_id, prompt) -> str: # raises Exception + """Ask ChatGPT with prompt, return response text + + - session_id: unused + + Raises: + ChatGPTError: ChatGPT error + """ + response: str | None = None + + try: + with self.lock: + response = self.chatbot.ask(prompt) + except Exception as e: + logging.warning(f"ChatGPT ask error: {e}") + raise ChatGPTError(str(e)) + + if not response: + raise ChatGPTError("ChatGPT response is None") + + return response + + +class APIVersion(Enum): + V1 = 1 + V3 = 3 + + def get_ChatGPT_class(self): + if self == self.V1: + return ChatGPTv1 + elif self == self.V3: + return ChatGPTv3 # ChatGPTConfig: {access_token, initial_prompt} @dataclass class ChatGPTConfig: + version: APIVersion access_token: str initial_prompt: str MAX_SESSIONS = 10 + # MultiChatGPT: {session_id: ChatGPT}: # - new(config) -> session_id # - ask(session_id, prompt) -> response # - delete(session_id) - - -class MultiChatGPT: +class MultiChatGPT(ChatGPT): """MultiChatGPT: {session_id: ChatGPT}""" def __init__(self): @@ -90,7 +163,7 @@ def __init__(self): def new(self, config: ChatGPTConfig) -> str: # raises TooManySessions, ChatGPTError """Create new ChatGPT session, return session_id - session_id is a uuid4 string + session_id is an uuid4 string Raises: TooManySessions: Too many sessions @@ -100,10 +173,18 @@ def new(self, config: ChatGPTConfig) -> str: # raises TooManySessions, ChatGPTE raise TooManySessions(MAX_SESSIONS) session_id = str(uuid.uuid4()) - self.chatgpt[session_id] = ChatGPT(config={ - "access_token": config.access_token, - "initial_prompt": config.initial_prompt - }) + + if config.version == APIVersion.V3: + self.chatgpt[session_id] = ChatGPTv3(config={ + "api_key": config.access_token, + "initial_prompt": config.initial_prompt + }) + else: + self.chatgpt[session_id] = ChatGPTv1(config={ + "access_token": config.access_token, + "initial_prompt": config.initial_prompt + }) + return session_id def ask(self, session_id: str, prompt: str) -> str: # raises ChatGPTError @@ -116,7 +197,7 @@ def ask(self, session_id: str, prompt: str) -> str: # raises ChatGPTError if session_id not in self.chatgpt: raise SessionNotFound(session_id) - resp = self.chatgpt[session_id].ask(prompt) + resp = self.chatgpt[session_id].ask(session_id, prompt) return resp diff --git a/chatgpt/grpcapi.py b/chatgpt/grpcapi.py index cd6f794..e8d883c 100644 --- a/chatgpt/grpcapi.py +++ b/chatgpt/grpcapi.py @@ -1,15 +1,15 @@ -from datetime import datetime +import json import logging import os -from chatbot import MultiChatGPT, ChatGPTConfig, ChatGPTError, TooManySessions, SessionNotFound +from chatbot import MultiChatGPT, ChatGPTConfig, ChatGPTError, TooManySessions, SessionNotFound, APIVersion from cooldown import CooldownException -from protos import chatgpt_chatbot_pb2, chatgpt_chatbot_pb2_grpc +from protos import chatbot_pb2, chatbot_pb2_grpc import grpc from concurrent import futures -class ChatGPTgRPCServer(chatgpt_chatbot_pb2_grpc.ChatGPTServiceServicer): +class ChatGPTgRPCServer(chatbot_pb2_grpc.ChatbotServiceServicer): def __init__(self): self.multiChatGPT = MultiChatGPT() @@ -18,16 +18,18 @@ def NewSession(self, request, context): Input: access_token (string) and initial_prompt (string). Output: session_id (string). """ - if not request.access_token: - # raise ValueError('access_token is required') + try: + c = request.config + c = json.loads(c) + except Exception as e: context.set_code(grpc.StatusCode.INVALID_ARGUMENT) - context.set_details('access_token is required') - logging.warn( - 'ChatGPTgRPCServer.NewSession: access_token is required') - return chatgpt_chatbot_pb2.NewSessionResponse() + context.set_details(str(e)) + logging.warning('ChatGPTgRPCServer.NewSession: bad config') + return chatbot_pb2.NewSessionResponse() config = ChatGPTConfig( - access_token=request.access_token, + version=APIVersion(c.get('version', None)), + access_token=c.get('access_token', False) or c.get('api_key', False) or '', initial_prompt=request.initial_prompt) session_id = None @@ -48,7 +50,7 @@ def NewSession(self, request, context): f'ChatGPTgRPCServer.NewSession: (OK) session_id={session_id}') # TODO: 这个 initial_response 太恶心了,还是逐层传比较好吧 - return chatgpt_chatbot_pb2.NewSessionResponse(session_id=session_id, initial_response=self.multiChatGPT.chatgpt[session_id].initial_response) + return chatbot_pb2.NewSessionResponse(session_id=session_id, initial_response=self.multiChatGPT.chatgpt[session_id].initial_response) def Chat(self, request, context): """Chat sends a prompt to ChatGPT and receives a response. @@ -60,13 +62,13 @@ def Chat(self, request, context): context.set_code(grpc.StatusCode.INVALID_ARGUMENT) context.set_details('session_id is required') logging.warn('ChatGPTgRPCServer.Chat: session_id is required') - return chatgpt_chatbot_pb2.ChatResponse() + return chatbot_pb2.ChatResponse() if not request.prompt: # raise ValueError('prompt is required') context.set_code(grpc.StatusCode.INVALID_ARGUMENT) context.set_details('prompt is required') logging.warn('ChatGPTgRPCServer.Chat: prompt is required') - return chatgpt_chatbot_pb2.ChatResponse() + return chatbot_pb2.ChatResponse() response = None try: @@ -89,7 +91,7 @@ def Chat(self, request, context): logging.info( f'ChatGPTgRPCServer.Chat: (OK) {response}') - return chatgpt_chatbot_pb2.ChatResponse(response=response) + return chatbot_pb2.ChatResponse(response=response) def DeleteSession(self, request, context): """DeleteSession deletes a session with ChatGPT. @@ -102,7 +104,7 @@ def DeleteSession(self, request, context): context.set_details('session_id is required') logging.warn( 'ChatGPTgRPCServer.DeleteSession: session_id is required') - return chatgpt_chatbot_pb2.DeleteSessionResponse() + return chatbot_pb2.DeleteSessionResponse() try: self.multiChatGPT.delete(request.session_id) @@ -117,17 +119,17 @@ def DeleteSession(self, request, context): logging.info( f'ChatGPTgRPCServer.DeleteSession: (OK) {request.session_id}') - return chatgpt_chatbot_pb2.DeleteSessionResponse(session_id=request.session_id) + return chatbot_pb2.DeleteSessionResponse(session_id=request.session_id) def serveGRPC(address: str = 'localhost:50052'): """Starts a gRPC server at the specified address 'host:port'.""" server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) - chatgpt_chatbot_pb2_grpc.add_ChatGPTServiceServicer_to_server( + chatbot_pb2_grpc.add_ChatbotServiceServicer_to_server( ChatGPTgRPCServer(), server) SERVICE_NAMES = [ - chatgpt_chatbot_pb2.DESCRIPTOR.services_by_name['ChatGPTService'].full_name] + chatbot_pb2.DESCRIPTOR.services_by_name['ChatbotService'].full_name] # the reflection service if os.getenv('GRPC_REFLECTION', False): diff --git a/chatgpt/httpapi.py b/chatgpt/httpapi.py index 4f3c0c4..2e8f0bf 100644 --- a/chatgpt/httpapi.py +++ b/chatgpt/httpapi.py @@ -1,11 +1,11 @@ from datetime import datetime -from chatbot import ChatGPT +from chatbot import ChatGPTv1 import aiohttp from aiohttp.web import Request, Response class ChatGPTHTTPServer: - def __init__(self, chatgpt: ChatGPT, host: str = "localhost", port: int = 9006): + def __init__(self, chatgpt: ChatGPTv1, host: str = "localhost", port: int = 9006): self.chatgpt = chatgpt self.host = host self.port = port @@ -68,7 +68,7 @@ def serveHTTP(address: str = "localhost:9006"): except: raise ValueError("address should be in format of host:port") - chatgpt = ChatGPT() + chatgpt = ChatGPTv1() server = ChatGPTHTTPServer(chatgpt, host, port) server.run() diff --git a/chatgpt/protos/chatbot_pb2.py b/chatgpt/protos/chatbot_pb2.py new file mode 100644 index 0000000..4da5e20 --- /dev/null +++ b/chatgpt/protos/chatbot_pb2.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: muvtuber/chatbot/v2/chatbot.proto +"""Generated protocol buffer code.""" +from google.protobuf.internal import builder as _builder +from google.protobuf import descriptor as _descriptor +from google.protobuf import descriptor_pool as _descriptor_pool +from google.protobuf import symbol_database as _symbol_database +# @@protoc_insertion_point(imports) + +_sym_db = _symbol_database.Default() + + + + +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n!muvtuber/chatbot/v2/chatbot.proto\x12\x13muvtuber.chatbot.v2\"R\n\x11NewSessionRequest\x12\x16\n\x06\x63onfig\x18\x01 \x01(\tR\x06\x63onfig\x12%\n\x0einitial_prompt\x18\x02 \x01(\tR\rinitialPrompt\"^\n\x12NewSessionResponse\x12\x1d\n\nsession_id\x18\x01 \x01(\tR\tsessionId\x12)\n\x10initial_response\x18\x02 \x01(\tR\x0finitialResponse\"5\n\x14\x44\x65leteSessionRequest\x12\x1d\n\nsession_id\x18\x01 \x01(\tR\tsessionId\"6\n\x15\x44\x65leteSessionResponse\x12\x1d\n\nsession_id\x18\x01 \x01(\tR\tsessionId\"D\n\x0b\x43hatRequest\x12\x1d\n\nsession_id\x18\x01 \x01(\tR\tsessionId\x12\x16\n\x06prompt\x18\x02 \x01(\tR\x06prompt\"*\n\x0c\x43hatResponse\x12\x1a\n\x08response\x18\x02 \x01(\tR\x08response2\xa4\x02\n\x0e\x43hatbotService\x12]\n\nNewSession\x12&.muvtuber.chatbot.v2.NewSessionRequest\x1a\'.muvtuber.chatbot.v2.NewSessionResponse\x12K\n\x04\x43hat\x12 .muvtuber.chatbot.v2.ChatRequest\x1a!.muvtuber.chatbot.v2.ChatResponse\x12\x66\n\rDeleteSession\x12).muvtuber.chatbot.v2.DeleteSessionRequest\x1a*.muvtuber.chatbot.v2.DeleteSessionResponseB\xc7\x01\n\x17\x63om.muvtuber.chatbot.v2B\x0c\x43hatbotProtoP\x01Z0muvtuberdriver/gen/muvtuber/chatbot/v2;chatbotv2\xa2\x02\x03MCX\xaa\x02\x13Muvtuber.Chatbot.V2\xca\x02\x13Muvtuber\\Chatbot\\V2\xe2\x02\x1fMuvtuber\\Chatbot\\V2\\GPBMetadata\xea\x02\x15Muvtuber::Chatbot::V2b\x06proto3') + +_globals = globals() +_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) +_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'muvtuber.chatbot.v2.chatbot_pb2', _globals) +if _descriptor._USE_C_DESCRIPTORS == False: + + DESCRIPTOR._options = None + DESCRIPTOR._serialized_options = b'\n\027com.muvtuber.chatbot.v2B\014ChatbotProtoP\001Z0muvtuberdriver/gen/muvtuber/chatbot/v2;chatbotv2\242\002\003MCX\252\002\023Muvtuber.Chatbot.V2\312\002\023Muvtuber\\Chatbot\\V2\342\002\037Muvtuber\\Chatbot\\V2\\GPBMetadata\352\002\025Muvtuber::Chatbot::V2' + _globals['_NEWSESSIONREQUEST']._serialized_start=58 + _globals['_NEWSESSIONREQUEST']._serialized_end=140 + _globals['_NEWSESSIONRESPONSE']._serialized_start=142 + _globals['_NEWSESSIONRESPONSE']._serialized_end=236 + _globals['_DELETESESSIONREQUEST']._serialized_start=238 + _globals['_DELETESESSIONREQUEST']._serialized_end=291 + _globals['_DELETESESSIONRESPONSE']._serialized_start=293 + _globals['_DELETESESSIONRESPONSE']._serialized_end=347 + _globals['_CHATREQUEST']._serialized_start=349 + _globals['_CHATREQUEST']._serialized_end=417 + _globals['_CHATRESPONSE']._serialized_start=419 + _globals['_CHATRESPONSE']._serialized_end=461 + _globals['_CHATBOTSERVICE']._serialized_start=464 + _globals['_CHATBOTSERVICE']._serialized_end=756 +# @@protoc_insertion_point(module_scope) diff --git a/chatgpt/protos/chatgpt_chatbot_pb2_grpc.py b/chatgpt/protos/chatbot_pb2_grpc.py similarity index 57% rename from chatgpt/protos/chatgpt_chatbot_pb2_grpc.py rename to chatgpt/protos/chatbot_pb2_grpc.py index c1c6487..8e1a527 100644 --- a/chatgpt/protos/chatgpt_chatbot_pb2_grpc.py +++ b/chatgpt/protos/chatbot_pb2_grpc.py @@ -2,10 +2,10 @@ """Client and server classes corresponding to protobuf-defined services.""" import grpc -from . import chatgpt_chatbot_pb2 as muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2 +from . import chatbot_pb2 as muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2 -class ChatGPTServiceStub(object): +class ChatbotServiceStub(object): """Missing associated documentation comment in .proto file.""" def __init__(self, channel): @@ -15,27 +15,27 @@ def __init__(self, channel): channel: A grpc.Channel. """ self.NewSession = channel.unary_unary( - '/muvtuber.chatbot.chatgpt_chatbot.v1.ChatGPTService/NewSession', - request_serializer=muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.NewSessionRequest.SerializeToString, - response_deserializer=muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.NewSessionResponse.FromString, + '/muvtuber.chatbot.v2.ChatbotService/NewSession', + request_serializer=muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.NewSessionRequest.SerializeToString, + response_deserializer=muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.NewSessionResponse.FromString, ) self.Chat = channel.unary_unary( - '/muvtuber.chatbot.chatgpt_chatbot.v1.ChatGPTService/Chat', - request_serializer=muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.ChatRequest.SerializeToString, - response_deserializer=muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.ChatResponse.FromString, + '/muvtuber.chatbot.v2.ChatbotService/Chat', + request_serializer=muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.ChatRequest.SerializeToString, + response_deserializer=muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.ChatResponse.FromString, ) self.DeleteSession = channel.unary_unary( - '/muvtuber.chatbot.chatgpt_chatbot.v1.ChatGPTService/DeleteSession', - request_serializer=muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.DeleteSessionRequest.SerializeToString, - response_deserializer=muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.DeleteSessionResponse.FromString, + '/muvtuber.chatbot.v2.ChatbotService/DeleteSession', + request_serializer=muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.DeleteSessionRequest.SerializeToString, + response_deserializer=muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.DeleteSessionResponse.FromString, ) -class ChatGPTServiceServicer(object): +class ChatbotServiceServicer(object): """Missing associated documentation comment in .proto file.""" def NewSession(self, request, context): - """NewSession creates a new session with ChatGPT. + """NewSession creates a new session with Chatbot. Input: access_token (string) and initial_prompt (string). Output: session_id (string). """ @@ -44,7 +44,7 @@ def NewSession(self, request, context): raise NotImplementedError('Method not implemented!') def Chat(self, request, context): - """Chat sends a prompt to ChatGPT and receives a response. + """Chat sends a prompt to Chatbot and receives a response. Input: session_id (string) and prompt (string). Output: response (string). """ @@ -53,7 +53,7 @@ def Chat(self, request, context): raise NotImplementedError('Method not implemented!') def DeleteSession(self, request, context): - """DeleteSession deletes a session with ChatGPT. + """DeleteSession deletes a session with Chatbot. Input: session_id (string). Output: session_id (string). """ @@ -62,31 +62,31 @@ def DeleteSession(self, request, context): raise NotImplementedError('Method not implemented!') -def add_ChatGPTServiceServicer_to_server(servicer, server): +def add_ChatbotServiceServicer_to_server(servicer, server): rpc_method_handlers = { 'NewSession': grpc.unary_unary_rpc_method_handler( servicer.NewSession, - request_deserializer=muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.NewSessionRequest.FromString, - response_serializer=muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.NewSessionResponse.SerializeToString, + request_deserializer=muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.NewSessionRequest.FromString, + response_serializer=muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.NewSessionResponse.SerializeToString, ), 'Chat': grpc.unary_unary_rpc_method_handler( servicer.Chat, - request_deserializer=muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.ChatRequest.FromString, - response_serializer=muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.ChatResponse.SerializeToString, + request_deserializer=muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.ChatRequest.FromString, + response_serializer=muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.ChatResponse.SerializeToString, ), 'DeleteSession': grpc.unary_unary_rpc_method_handler( servicer.DeleteSession, - request_deserializer=muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.DeleteSessionRequest.FromString, - response_serializer=muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.DeleteSessionResponse.SerializeToString, + request_deserializer=muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.DeleteSessionRequest.FromString, + response_serializer=muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.DeleteSessionResponse.SerializeToString, ), } generic_handler = grpc.method_handlers_generic_handler( - 'muvtuber.chatbot.chatgpt_chatbot.v1.ChatGPTService', rpc_method_handlers) + 'muvtuber.chatbot.v2.ChatbotService', rpc_method_handlers) server.add_generic_rpc_handlers((generic_handler,)) # This class is part of an EXPERIMENTAL API. -class ChatGPTService(object): +class ChatbotService(object): """Missing associated documentation comment in .proto file.""" @staticmethod @@ -100,9 +100,9 @@ def NewSession(request, wait_for_ready=None, timeout=None, metadata=None): - return grpc.experimental.unary_unary(request, target, '/muvtuber.chatbot.chatgpt_chatbot.v1.ChatGPTService/NewSession', - muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.NewSessionRequest.SerializeToString, - muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.NewSessionResponse.FromString, + return grpc.experimental.unary_unary(request, target, '/muvtuber.chatbot.v2.ChatbotService/NewSession', + muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.NewSessionRequest.SerializeToString, + muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.NewSessionResponse.FromString, options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) @@ -117,9 +117,9 @@ def Chat(request, wait_for_ready=None, timeout=None, metadata=None): - return grpc.experimental.unary_unary(request, target, '/muvtuber.chatbot.chatgpt_chatbot.v1.ChatGPTService/Chat', - muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.ChatRequest.SerializeToString, - muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.ChatResponse.FromString, + return grpc.experimental.unary_unary(request, target, '/muvtuber.chatbot.v2.ChatbotService/Chat', + muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.ChatRequest.SerializeToString, + muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.ChatResponse.FromString, options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) @@ -134,8 +134,8 @@ def DeleteSession(request, wait_for_ready=None, timeout=None, metadata=None): - return grpc.experimental.unary_unary(request, target, '/muvtuber.chatbot.chatgpt_chatbot.v1.ChatGPTService/DeleteSession', - muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.DeleteSessionRequest.SerializeToString, - muvtuber_dot_chatbot_dot_chatgpt__chatbot_dot_v1_dot_chatgpt__chatbot__pb2.DeleteSessionResponse.FromString, + return grpc.experimental.unary_unary(request, target, '/muvtuber.chatbot.v2.ChatbotService/DeleteSession', + muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.DeleteSessionRequest.SerializeToString, + muvtuber_dot_chatbot_dot_v2_dot_chatbot__pb2.DeleteSessionResponse.FromString, options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) diff --git a/chatgpt/protos/chatgpt_chatbot_pb2.py b/chatgpt/protos/chatgpt_chatbot_pb2.py deleted file mode 100644 index 6f1e4ae..0000000 --- a/chatgpt/protos/chatgpt_chatbot_pb2.py +++ /dev/null @@ -1,39 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by the protocol buffer compiler. DO NOT EDIT! -# source: muvtuber/chatbot/chatgpt_chatbot/v1/chatgpt_chatbot.proto -"""Generated protocol buffer code.""" -from google.protobuf.internal import builder as _builder -from google.protobuf import descriptor as _descriptor -from google.protobuf import descriptor_pool as _descriptor_pool -from google.protobuf import symbol_database as _symbol_database -# @@protoc_insertion_point(imports) - -_sym_db = _symbol_database.Default() - - - - -DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n9muvtuber/chatbot/chatgpt_chatbot/v1/chatgpt_chatbot.proto\x12#muvtuber.chatbot.chatgpt_chatbot.v1\"]\n\x11NewSessionRequest\x12!\n\x0c\x61\x63\x63\x65ss_token\x18\x01 \x01(\tR\x0b\x61\x63\x63\x65ssToken\x12%\n\x0einitial_prompt\x18\x02 \x01(\tR\rinitialPrompt\"^\n\x12NewSessionResponse\x12\x1d\n\nsession_id\x18\x01 \x01(\tR\tsessionId\x12)\n\x10initial_response\x18\x02 \x01(\tR\x0finitialResponse\"5\n\x14\x44\x65leteSessionRequest\x12\x1d\n\nsession_id\x18\x01 \x01(\tR\tsessionId\"6\n\x15\x44\x65leteSessionResponse\x12\x1d\n\nsession_id\x18\x01 \x01(\tR\tsessionId\"D\n\x0b\x43hatRequest\x12\x1d\n\nsession_id\x18\x01 \x01(\tR\tsessionId\x12\x16\n\x06prompt\x18\x02 \x01(\tR\x06prompt\"*\n\x0c\x43hatResponse\x12\x1a\n\x08response\x18\x02 \x01(\tR\x08response2\x85\x03\n\x0e\x43hatGPTService\x12}\n\nNewSession\x12\x36.muvtuber.chatbot.chatgpt_chatbot.v1.NewSessionRequest\x1a\x37.muvtuber.chatbot.chatgpt_chatbot.v1.NewSessionResponse\x12k\n\x04\x43hat\x12\x30.muvtuber.chatbot.chatgpt_chatbot.v1.ChatRequest\x1a\x31.muvtuber.chatbot.chatgpt_chatbot.v1.ChatResponse\x12\x86\x01\n\rDeleteSession\x12\x39.muvtuber.chatbot.chatgpt_chatbot.v1.DeleteSessionRequest\x1a:.muvtuber.chatbot.chatgpt_chatbot.v1.DeleteSessionResponseB\xb3\x02\n\'com.muvtuber.chatbot.chatgpt_chatbot.v1B\x13\x43hatgptChatbotProtoP\x01ZHmuvtuberdriver/gen/muvtuber/chatbot/chatgpt_chatbot/v1;chatgpt_chatbotv1\xa2\x02\x03MCC\xaa\x02\"Muvtuber.Chatbot.ChatgptChatbot.V1\xca\x02\"Muvtuber\\Chatbot\\ChatgptChatbot\\V1\xe2\x02.Muvtuber\\Chatbot\\ChatgptChatbot\\V1\\GPBMetadata\xea\x02%Muvtuber::Chatbot::ChatgptChatbot::V1b\x06proto3') - -_globals = globals() -_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) -_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'muvtuber.chatbot.chatgpt_chatbot.v1.chatgpt_chatbot_pb2', _globals) -if _descriptor._USE_C_DESCRIPTORS == False: - - DESCRIPTOR._options = None - DESCRIPTOR._serialized_options = b'\n\'com.muvtuber.chatbot.chatgpt_chatbot.v1B\023ChatgptChatbotProtoP\001ZHmuvtuberdriver/gen/muvtuber/chatbot/chatgpt_chatbot/v1;chatgpt_chatbotv1\242\002\003MCC\252\002\"Muvtuber.Chatbot.ChatgptChatbot.V1\312\002\"Muvtuber\\Chatbot\\ChatgptChatbot\\V1\342\002.Muvtuber\\Chatbot\\ChatgptChatbot\\V1\\GPBMetadata\352\002%Muvtuber::Chatbot::ChatgptChatbot::V1' - _globals['_NEWSESSIONREQUEST']._serialized_start=98 - _globals['_NEWSESSIONREQUEST']._serialized_end=191 - _globals['_NEWSESSIONRESPONSE']._serialized_start=193 - _globals['_NEWSESSIONRESPONSE']._serialized_end=287 - _globals['_DELETESESSIONREQUEST']._serialized_start=289 - _globals['_DELETESESSIONREQUEST']._serialized_end=342 - _globals['_DELETESESSIONRESPONSE']._serialized_start=344 - _globals['_DELETESESSIONRESPONSE']._serialized_end=398 - _globals['_CHATREQUEST']._serialized_start=400 - _globals['_CHATREQUEST']._serialized_end=468 - _globals['_CHATRESPONSE']._serialized_start=470 - _globals['_CHATRESPONSE']._serialized_end=512 - _globals['_CHATGPTSERVICE']._serialized_start=515 - _globals['_CHATGPTSERVICE']._serialized_end=904 -# @@protoc_insertion_point(module_scope)