Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ from dotenv import load_dotenv
|
|
| 11 |
from grpc_tools import protoc
|
| 12 |
from grpc._channel import _MultiThreadedRendezvous
|
| 13 |
import re
|
|
|
|
| 14 |
|
| 15 |
# 加载环境变量
|
| 16 |
load_dotenv()
|
|
@@ -60,8 +61,34 @@ class GRPCHandler:
|
|
| 60 |
|
| 61 |
def _load_proto(self):
|
| 62 |
module_name = os.path.splitext(os.path.basename(self.proto_file))[0] + '_pb2_grpc'
|
| 63 |
-
proto_module =
|
| 64 |
self.stub_class = getattr(proto_module, f"{module_name.split('_')[0]}Stub")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
async def grpc_to_pieces(self, model, content, rules, temperature, top_p):
|
| 67 |
metadata = [
|
|
|
|
| 11 |
from grpc_tools import protoc
|
| 12 |
from grpc._channel import _MultiThreadedRendezvous
|
| 13 |
import re
|
| 14 |
+
import importlib
|
| 15 |
|
| 16 |
# 加载环境变量
|
| 17 |
load_dotenv()
|
|
|
|
| 61 |
|
| 62 |
def _load_proto(self):
|
| 63 |
module_name = os.path.splitext(os.path.basename(self.proto_file))[0] + '_pb2_grpc'
|
| 64 |
+
proto_module = importlib.import_module(module_name)
|
| 65 |
self.stub_class = getattr(proto_module, f"{module_name.split('_')[0]}Stub")
|
| 66 |
+
|
| 67 |
+
pb2_module_name = os.path.splitext(os.path.basename(self.proto_file))[0] + '_pb2'
|
| 68 |
+
self.pb2_module = importlib.import_module(pb2_module_name)
|
| 69 |
+
|
| 70 |
+
def _build_request(self, model, content, rules, temperature, top_p):
|
| 71 |
+
if model.startswith('gpt'):
|
| 72 |
+
return self.pb2_module.Request(
|
| 73 |
+
models=model,
|
| 74 |
+
messages=[
|
| 75 |
+
{"role": 0, "message": rules},
|
| 76 |
+
{"role": 1, "message": content}
|
| 77 |
+
],
|
| 78 |
+
temperature=temperature or 0.1,
|
| 79 |
+
top_p=top_p or 1.0
|
| 80 |
+
)
|
| 81 |
+
else:
|
| 82 |
+
return self.pb2_module.Request(
|
| 83 |
+
models=model,
|
| 84 |
+
args={
|
| 85 |
+
"messages": {
|
| 86 |
+
"unknown": 1,
|
| 87 |
+
"message": content
|
| 88 |
+
},
|
| 89 |
+
"rules": rules
|
| 90 |
+
}
|
| 91 |
+
)
|
| 92 |
|
| 93 |
async def grpc_to_pieces(self, model, content, rules, temperature, top_p):
|
| 94 |
metadata = [
|