Skip to content

Commit

Permalink
Merge pull request #8 from Yanyutin753/good
Browse files Browse the repository at this point in the history
fix logger info update files
  • Loading branch information
Vinlic authored Apr 2, 2024
2 parents 30bfa2a + 1af80ca commit 26e1735
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 26 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/sync.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Upstream Sync

permissions:
contents: write
issues: write
actions: write

on:
schedule:
- cron: '0 * * * *' # every hour
workflow_dispatch:

jobs:
sync_latest_from_upstream:
name: Sync latest commits from upstream repo
runs-on: ubuntu-latest
if: ${{ github.event.repository.fork }}

steps:
- uses: actions/checkout@v4

- name: Clean issue notice
uses: actions-cool/issues-helper@v3
with:
actions: 'close-issues'
labels: '🚨 Sync Fail'

- name: Sync upstream changes
id: sync
uses: aormsby/[email protected]
with:
upstream_sync_repo: LLM-Red-Team/glm-free-api
upstream_sync_branch: master
target_sync_branch: master
target_repo_token: ${{ secrets.GITHUB_TOKEN }} # automatically generated, no need to set
test_mode: false

- name: Sync check
if: failure()
uses: actions-cool/issues-helper@v3
with:
actions: 'create-issue'
title: '🚨 同步失败 | Sync Fail'
labels: '🚨 Sync Fail'
body: |
Due to a change in the workflow file of the LLM-Red-Team/glm-free-api upstream repository, GitHub has automatically suspended the scheduled automatic update. You need to manually sync your fork. Please refer to the detailed [Tutorial][tutorial-en-US] for instructions.
由于 LLM-Red-Team/glm-free-api 上游仓库的 workflow 文件变更,导致 GitHub 自动暂停了本次自动更新,你需要手动 Sync Fork 一次,
58 changes: 32 additions & 26 deletions src/api/controllers/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ async function generateImages(
console.error(err)
);

if(imageUrls.length == 0)
if (imageUrls.length == 0)
throw new APIException(EX.API_IMAGE_GENERATION_FAILED);

return imageUrls;
Expand All @@ -452,29 +452,27 @@ async function generateImages(
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
*/
function extractRefFileUrls(messages: any[]) {
return messages.reduce((urls, message) => {
if (_.isArray(message.content)) {
message.content.forEach((v) => {
if (!_.isObject(v) || !["file", "image_url"].includes(v["type"]))
return;
// glm-free-api支持格式
if (
v["type"] == "file" &&
_.isObject(v["file_url"]) &&
_.isString(v["file_url"]["url"])
)
urls.push(v["file_url"]["url"]);
// 兼容gpt-4-vision-preview API格式
else if (
v["type"] == "image_url" &&
_.isObject(v["image_url"]) &&
_.isString(v["image_url"]["url"])
)
urls.push(v["image_url"]["url"]);
});
}
const urls = [];
// 如果没有消息,则返回[]
if (!messages.length) {
return urls;
}, []);
}
// 只获取最新的消息
const lastMessage = messages[messages.length - 1];
if (_.isArray(lastMessage.content)) {
lastMessage.content.forEach(v => {
if (!_.isObject(v) || !['file', 'image_url'].includes(v['type']))
return;
// glm-free-api支持格式
if (v['type'] == 'file' && _.isObject(v['file_url']) && _.isString(v['file_url']['url']))
urls.push(v['file_url']['url']);
// 兼容gpt-4-vision-preview API格式
else if (v['type'] == 'image_url' && _.isObject(v['image_url']) && _.isString(v['image_url']['url']))
urls.push(v['image_url']['url']);
});
}
logger.info("本次请求上传:" + urls.length + "个文件");
return urls;
}

/**
Expand All @@ -489,8 +487,16 @@ function extractRefFileUrls(messages: any[]) {
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
*/
function messagesPrepare(messages: any[], refs: any[]) {
// 只保留最新消息以及不包含"type": "image_url"或"type": "file"的消息
let validMessages = messages.filter((message, index) => {
if (index === messages.length - 1) return true;
if (!Array.isArray(message.content)) return true;
// 不含"type": "image_url"或"type": "file"的消息保留
return !message.content.some(v => (typeof v === 'object' && ['file', 'image_url'].includes(v['type'])));
});

const content =
messages.reduce((content, message) => {
validMessages.reduce((content, message) => {
if (_.isArray(message.content)) {
return (
message.content.reduce((_content, v) => {
Expand Down Expand Up @@ -1002,8 +1008,8 @@ async function receiveImages(
throw new Error(`Stream response invalid: ${event.data}`);
if (!convId && result.conversation_id)
convId = result.conversation_id;
if(result.status == "intervene")
throw new APIException(EX.API_CONTENT_FILTERED);
if (result.status == "intervene")
throw new APIException(EX.API_CONTENT_FILTERED);
if (result.status != "finish") {
result.parts.forEach(part => {
const { content } = part;
Expand Down

0 comments on commit 26e1735

Please sign in to comment.