Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowcz007 committed Aug 17, 2024
1 parent 496ff41 commit bfcadde
Show file tree
Hide file tree
Showing 15 changed files with 546 additions and 2,663 deletions.
60 changes: 60 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
# except:
# print("##nodes.ChatGPT ImportError")

from .nodes.ChatGPT import openai_client

from .nodes.RembgNode import get_rembg_models,U2NET_HOME,run_briarmbg,run_rembg

Expand Down Expand Up @@ -584,6 +585,65 @@ async def mixlab_hander(request):
print(e)
return web.json_response(data)

# llm的api key,使用硅基流动
@routes.post('/mixlab/llm_api_key')
async def mixlab_llm_api_key_handler(request):
data = await request.json()
api_key = data.get('key')

app_folder = os.path.join(current_path, "app")
key_file_path = os.path.join(app_folder, "llm_api_key.txt")

if api_key:
if not os.path.exists(app_folder):
os.makedirs(app_folder)
try:
with open(key_file_path, 'w') as f:
f.write(api_key)
return web.json_response({'message': 'API key saved successfully'})
except Exception as e:
return web.json_response({'error': str(e)}, status=500)
else:
if os.path.exists(key_file_path):
try:
with open(key_file_path, 'r') as f:
saved_api_key = f.read().strip()
return web.json_response({'key': saved_api_key})
except Exception as e:
return web.json_response({'error': str(e)}, status=500)
else:
return web.json_response({'error': 'No API key provided and no key found in local storage'}, status=400)


@routes.post('/chat/completions')
async def chat_completions(request):
data = await request.json()
messages = data.get('messages')
key=data.get('key')
if not messages:
return web.json_response({"error": "No messages provided"}, status=400)

async def generate():
try:
client=openai_client(key,"https://api.siliconflow.cn/v1")

response = client.chat.completions.create(
model="01-ai/Yi-1.5-9B-Chat-16K",
messages=messages,
stream=True
)

for chunk in response:
if hasattr(chunk.choices[0].delta, 'content'):
content = chunk.choices[0].delta.content
if content is not None:
yield content.encode('utf-8') + b"\r\n"

except Exception as e:
yield f"Error: {str(e)}".encode('utf-8') + b"\r\n"

return web.Response(body=generate(), content_type='text/event-stream')


@routes.get('/mixlab/app')
async def mixlab_app_handler(request):
Expand Down
1 change: 0 additions & 1 deletion example/AIPC大赛模板_4.json

This file was deleted.

1 change: 0 additions & 1 deletion example/Image-to-Image_2.json

This file was deleted.

Loading

0 comments on commit bfcadde

Please sign in to comment.