openai-agent-llm
- 为本地私有部署大模型提供openai接口兼容
- 开发者只需要定义推理接口内容即可
- 安装
pip install openai_agent_llm
import json
import os
import uuid
import uvicorn
from starlette.middleware.cors import CORSMiddleware
from openai_agent_llm import LLMAPI
from openai_agent_llm import ModelServer
from websockets.sync.client import connect
from loguru import logger
server = os.environ.get("server", "wss://mikeee-chatglm2-6b-4bit.hf.space/queue/join")
class ChatGLMInt4SpiderServer(ModelServer):
http_request = None
def deal_prompt(self, params):
prompt = params.get("prompt")
if type(prompt) == str:
prompt = prompt.replace("Human:", "问:").replace("AI:", "答:")
if len(prompt) > 2 and prompt[-2:0] == "答:":
prompt = prompt[:-2]
elif type(prompt) == list:
prompt = ""
for i in params.get("prompt", []):
role = i.get("role", "")
if role == "system":
pass
else:
prompt = prompt + i.get("content", "") + "\n"
else:
prompt = "hi"
# if len(prompt) * CutWordMultiple > 1024:
# prompt = prompt[int(len(prompt) * 2) / 3:]
return prompt
def send_init(self, websocket, session_hash):
value = websocket.recv()
logger.info(value)
websocket.send(
json.dumps({"fn_index": 1, "session_hash": session_hash}, ensure_ascii=False))
for i in range(2):
value = websocket.recv()
logger.info(value)
def infer(self, params):
prompt = self.deal_prompt(params)
url = server
request_id = str(uuid.uuid4())
with connect(url) as websocket:
self.send_init(websocket=websocket, session_hash=request_id)
websocket.send(
json.dumps({"data": [False, prompt, [], 8192, 0.85, 0.95, None, None],
"event_data": None, "fn_index": 1, "session_hash": request_id}, ensure_ascii=False))
text = ""
while True:
value = websocket.recv()
try:
data = json.loads(value)
except:
continue
msg = data.get("msg")
if data.get("msg") == "process_completed":
finish = True
else:
finish = False
if msg == "process_starts":
continue
elif msg == "process_generating":
data_ = data.get("output", {}).get("data", [])[0][0][1][3:-4].replace("<br>", "\n")
token = data_[len(text):]
text = data_
stop = None
if finish is True:
stop = "stop"
yield {
"text": token,
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0,
},
"finish_reason": stop,
}
if finish is True:
print()
break
app = LLMAPI()
app.init(model_server_class=ChatGLMInt4SpiderServer)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
if __name__ == '__main__':
uvicorn.run(app=app, port=9000)
Metadata
Release files for openai-agent-llm 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| openai-agent-llm-0.0.2.tar.gz | 8.8 kB | Without provenance | Details for openai-agent-llm-0.0.2.tar.gz |
Release files / openai-agent-llm-0.0.2.tar.gz
| Download URL | openai-agent-llm-0.0.2.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
16ce76a2045813be78eb83cff61c0807b7a38bc2877cafd330db2fbda2152616
|
|
BLAKE2b-256 checksum How to use checksums |
50461f943c32d3c80debabaed8bcd26b47b5793e13447b8362cd636646ab769f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.13
|
Security for openai-agent-llm 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.