web服务与local_model服务的原理
if services.__contains__('web'):
management.call_command('runserver', "0.0.0.0:8080")
elif services.__contains__('celery'):
management.call_command('celery', 'celery')
elif services.__contains__('local_model'):
os.environ.setdefault('SERVER_NAME', 'local_model')
from smartdoc.const import CONFIG
bind = f'{CONFIG.get("LOCAL_MODEL_HOST")}:{CONFIG.get("LOCAL_MODEL_PORT")}'
management.call_command('runserver', bind)
web服务与local_model服务 不同之处在于添加的路由,可以理解为启动了一个相同的服务,路由阉割了,web服务接受请求转去请求另local_model服务
if os.environ.get('SERVER_NAME', 'web') == 'local_model':
urlpatterns += [
path('model/<str:model_id>/embed_documents', views.ModelApply.EmbedDocuments.as_view(),
name='model/embed_documents'),
path('model/<str:model_id>/embed_query', views.ModelApply.EmbedQuery.as_view(),
name='model/embed_query'),
path('model/<str:model_id>/compress_documents', views.ModelApply.CompressDocuments.as_view(),
name='model/embed_query'),
]
请求转发的代码
def embed_query(self, text: str) -> List[float]:
bind = f'{CONFIG.get("LOCAL_MODEL_HOST")}:{CONFIG.get("LOCAL_MODEL_PORT")}'
res = requests.post(f'{CONFIG.get("LOCAL_MODEL_PROTOCOL")}://{bind}/api/model/{self.model_id}/embed_query',{'text': text})
result = res.json()
if result.get('code', 500) == 200:
return result.get('data')
raise Exception(result.get('message'))