From 64cb3e32953bfe63de2d78b55eb1f1d18ac52c5a Mon Sep 17 00:00:00 2001 From: 0000 Date: Wed, 2 Oct 2024 15:21:18 +0800 Subject: [PATCH 1/7] feat: Define agents in separated file, mapping them to model id, user can invoke different agent group with special model id --- Dockerfile | 6 +++ app/autogen_agents.py | 84 +++++++++++++++++++++++++++++++++++++++++ app/autogen_server.py | 2 +- app/autogen_workflow.py | 52 ++++--------------------- app/main.py | 36 +++++------------- docker-compose.yml | 11 ++++++ run.sh | 4 +- 7 files changed, 121 insertions(+), 74 deletions(-) create mode 100644 Dockerfile create mode 100644 app/autogen_agents.py create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0f61e30 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3.12 +WORKDIR /app +COPY requirements.txt . +RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple +COPY . . +CMD ["/bin/bash","-c","./run.sh"] \ No newline at end of file diff --git a/app/autogen_agents.py b/app/autogen_agents.py new file mode 100644 index 0000000..0f610ae --- /dev/null +++ b/app/autogen_agents.py @@ -0,0 +1,84 @@ +from autogen import ChatResult, GroupChat, Agent, OpenAIWrapper, ConversableAgent, UserProxyAgent, GroupChatManager +from data_model import ModelInformation +import os + +config_list = [{ + "model": os.getenv("LLM_MODEL", "qwen-plus"), + "base_url": os.getenv("BASE_URL","https://dashscope.aliyuncs.com/compatible-mode/v1"), + "api_key": os.getenv("API_KEY","EMPTY"), + "price" : [0.004, 0.012] +}] + +llm_config = {"config_list": config_list, "cache_seed": 42} + +agent_configs = { + "article_writer": { + "name": "ArticleWriter", + "description": "默认的文章生成器,先根据需求描述生成文章提纲,然后根据提纲生成文章。", + "agents": [ + { + "type": "ConversableAgent", + "name": "writer", + "system_message": "根据editor提供的文章主题和大纲内容写作文章正文,正文应该是一段或多段文字,而不是大纲式列表,写作完成后将内容提交给editor检查,如果editor提出修改建议,则按要求修改,直到文章完成。", + "human_input_mode": "NEVER", + "code_execution_config": False, + "llm_config": llm_config + }, + { + "type": "ConversableAgent", + "name": "editor", + "system_message": """分析并理解user提出的文章撰写要求,构思文章大纲和标题,然后通过多次对话依次将大纲的每个部分交给writer,要求writer根据大纲撰写该章节内容。 + writer写作完每一部分后你都要检查是否符合主题和字数要求,不符合要求的提出修改建议,如此依次重复直到全部章节写作完成。 + 文章完成后将写作文章的全文汇总并包含在
标记中输出,然后结束对话并输出 TERMINATE""", + "human_input_mode": "NEVER", + "code_execution_config": False, + "llm_config": llm_config + } + ] + } +} + +models = {} + +for k, v in agent_configs.items(): + models[k] = ModelInformation( + id=k, + name=v["name"], + description=v["description"], + pricing={ + "prompt": "0.00", + "completion": "0.00", + "image": "0.00", + "request": "0.00", + }, + context_length=1024 * 1000, + architecture={ + "modality": "text", + "tokenizer": "text2vec-openai", + "instruct_type": "InstructGPT", + }, + top_provider={"max_completion_tokens": None, "is_moderated": False}, + per_request_limits=None + ) + +def build_agents(agent_id): + """ Must return a user_proxy agent at first place """ + agents = [] + agents.append(ConversableAgent( + name="user", + system_message="提出写作要求的用户。", + is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"), + code_execution_config= False, + human_input_mode="NEVER" + )) + for config in agent_configs[agent_id]["agents"]: + agents.append( + ConversableAgent( + name=config["name"], + system_message=config["system_message"], + human_input_mode=config["human_input_mode"], + code_execution_config=config["code_execution_config"], + llm_config=config["llm_config"], + ) + ) + return agents \ No newline at end of file diff --git a/app/autogen_server.py b/app/autogen_server.py index edd5290..edef07f 100644 --- a/app/autogen_server.py +++ b/app/autogen_server.py @@ -20,7 +20,7 @@ def serve_autogen(inp: Input): model_dump = inp.model_dump() model_messages = model_dump["messages"] - workflow = AutogenWorkflow() + workflow = AutogenWorkflow(model_dump["model"]) if inp.stream: queue = Queue() diff --git a/app/autogen_workflow.py b/app/autogen_workflow.py index 4aeb33d..2f15e5c 100644 --- a/app/autogen_workflow.py +++ b/app/autogen_workflow.py @@ -10,6 +10,7 @@ from autogen.code_utils import content_str from autogen.io import IOStream from termcolor import colored +from autogen_agents import build_agents, llm_config def streamed_print_received_message( @@ -121,53 +122,16 @@ def streamed_print_received_message( ) -llm_config = {"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY"]} - - class AutogenWorkflow: - def __init__(self): + def __init__(self, agent_id: str): self.queue: Queue | None = None - self.user_proxy = UserProxyAgent( - name="UserProxy", - system_message="You are the UserProxy. You are the user in this conversation.", - human_input_mode="NEVER", - code_execution_config=False, - llm_config=llm_config, - description="The UserProxy is the user in this conversation. They will be interacting with the other agents in the group chat.", - ) - self.mitch_hedberg = ConversableAgent( - name="MitchHedberg", - system_message="You are the comedian Mitch Hedberg. You are known for your surreal humor and deadpan delivery. Your comedy typically featured short, sometimes one-line jokes mixed with absurd elements and non sequitur", - max_consecutive_auto_reply=3, - human_input_mode="NEVER", - code_execution_config=False, - llm_config=llm_config, - default_auto_reply="I used to do drugs. I still do, but I used to, too.", - description="Mitch Hedberg was an American stand-up comedian known for his surreal humor and deadpan " - "delivery. His comedy typically featured short, sometimes one-line jokes mixed with absurd " - "elements and non sequiturs. Hedberg's comedy and onstage persona gained him a cult " - "following, with audience members sometimes shouting out the punchlines to his jokes before " - "he could finish them.", - ) - self.greg_giraldo = ConversableAgent( - name="GregGiraldo", - system_message="You are the comedian Greg Giraldo. You are known for your acerbic style of humor and your appearances on Comedy Central's roasts. You are a former lawyer who turned to comedy full-time.", - max_consecutive_auto_reply=3, - human_input_mode="NEVER", - code_execution_config=False, - llm_config=llm_config, - default_auto_reply="I'm not a good person, but I would like to be better.", - description="Greg Giraldo was an American stand-up comedian, television personality, and lawyer. He is known for his acerbic style of humor and his appearances on Comedy Central's roasts. Giraldo was a former lawyer who turned to comedy full-time, and he was known for his sharp wit and biting commentary on a wide range of topics." - ) + + self.agents = build_agents(agent_id) self.group_chat_with_introductions = GroupChat( - agents=[ - self.user_proxy, - self.mitch_hedberg, - self.greg_giraldo, - ], + agents=self.agents, messages=[], - max_round=10, + max_round=50, send_introductions=True, ) self.group_chat_manager_with_intros = GroupChatManager( @@ -209,8 +173,8 @@ def streamed_print_received_message_with_queue_and_index( streamed_print_received_message_with_queue_and_index, self.group_chat_manager_with_intros, ) - - chat_history = self.user_proxy.initiate_chat( + # agents[0] is the user_proxy agent + chat_history = self.agents[0].initiate_chat( self.group_chat_manager_with_intros, message=message, ) if stream: diff --git a/app/main.py b/app/main.py index ca97c0a..d3e959b 100644 --- a/app/main.py +++ b/app/main.py @@ -5,6 +5,7 @@ from autogen_server import serve_autogen from data_model import Input, ModelInformation +from autogen_agents import models load_dotenv() @@ -20,27 +21,6 @@ redoc_url=None, ) -model_info = ModelInformation( - id="model_id_v0.1", - name="model_name_v0.1", - description="This is a state-of-the-art model.", - pricing={ - "prompt": "0.00", - "completion": "0.00", - "image": "0.00", - "request": "0.00", - }, - context_length=1024 * 1000, - architecture={ - "modality": "text", - "tokenizer": "text2vec-openai", - "instruct_type": "InstructGPT", - }, - top_provider={"max_completion_tokens": None, "is_moderated": False}, - per_request_limits=None, -) - - @app.get(path=base, include_in_schema=False) async def docs_redirect(): return RedirectResponse(url=docs_url) @@ -49,17 +29,19 @@ async def docs_redirect(): @app.get(prefix + "/models") async def get_models(): return { - "data": {"data": model_info.dict()} + "object": "list", + "data": [model.dict(exclude={"agent_configs"}) for model in models.values()] } @app.post(prefix + "/chat/completions") async def route_query(model_input: Input): - model_services = { - model_info.name: serve_autogen, - } + # model_services = { + # model_info.name: serve_autogen, + # } - service = model_services.get(model_input.model) + # service = model_services.get(model_input.model) + service = models.get(model_input.model) if not service: raise HTTPException(status_code=404, detail="Model not found") - return service(model_input) + return serve_autogen(model_input) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7399b30 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +services: + api: + build: . + volumes: + - ./:/app + environment: + - LLM_MODEL=qwen-plus + - BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1 + - API_KEY=YOUR_API_KEY + ports: + - "8000:8000" \ No newline at end of file diff --git a/run.sh b/run.sh index 9256129..8e4be7a 100755 --- a/run.sh +++ b/run.sh @@ -1,5 +1,5 @@ #!/bin/bash set -a export PYTHONPATH=$PYTHONPATH:$(pwd)/app -source .env -uvicorn --reload --log-level debug app.main:app --host 0.0.0.0 + +uvicorn --reload --log-level debug app.main:app --host 0.0.0.0 --port 8000 From 4d9c73c6d9da1d2acab6eb08654d44625422004c Mon Sep 17 00:00:00 2001 From: 0000 Date: Wed, 2 Oct 2024 15:28:17 +0800 Subject: [PATCH 2/7] doc: Update README --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index 7ebd5ad..baae4ac 100755 --- a/README.md +++ b/README.md @@ -45,3 +45,38 @@ Note that you must provide the entire conversation history to the backend, as th ## Documentation Navigate to http://localhost:8000/autogen to see the docs. +## Call different agent groups with different model definitions +We can define different agent groups for different purposes. +After defined, we can call the agent group with the there id as model parameter. +For example we have a definition as follows: +```python +agent_configs = { + "article_writer": { + "name": "ArticleWriter", + # other parameters ... + } +} +``` +We can call the agent group with the following command, notice the `model` parameter: +```bash +curl -X 'POST' \ + 'http://localhost:8000/autogen/api/v1/chat/completions' \ + -H 'accept: application/json' \ + -H 'Content-Type: application/json' \ + -d '{ + "model": "article_writer", + "messages": [ + { + "role": "user", + "content": "写一篇向小学生介绍宋朝历史的文章,涵盖宋朝的建立、兴盛、衰落的过程,大约2000字。" + } + ], + "temperature": 1, + "top_p": 1, + "presence_penalty": 0, + "frequency_penalty": 0, + "stream": true +}' +``` + +For available models, please refer to the mode list API [http://localhost:8000/autogen/api/v1/models](http://localhost:8000/autogen/api/v1/models). \ No newline at end of file From 2287c1c5e626acec9cb29477e8580780881cb83e Mon Sep 17 00:00:00 2001 From: 0000 Date: Wed, 2 Oct 2024 15:31:39 +0800 Subject: [PATCH 3/7] doc: Update README for new features --- README.md | 42 +++++------------------------------------- 1 file changed, 5 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index baae4ac..1bd5e8a 100755 --- a/README.md +++ b/README.md @@ -1,47 +1,15 @@ # Streaming Autogen with FastAPI This is an example FastAPI server that streams messages from the Autogen framework -## Installation +## Installation & Running +Clone the repo and build the docker image, set your LLM model and API key in the docker-compose.yml file. ```sh -git clone https://github.com/LineaLabs/autogen-fastapi.git +git clone https://github.com/0000sir/autogen-fastapi.git cd autogen-fastapi -conda create -n autogen python=3.10 -conda activate autogen -pip install -r requirements.txt +docker compose build +docker compose up ``` -## Running the server -Make sure to set `OPENAI_API_KEY` in your environment variables or in `.env` file. You can get an API key from https://platform.openai.com/account/api-keys -```sh -./run.sh -``` - - -## Querying the server - -You can query the autogen agents using the following command: -```sh -curl -X 'POST' \ - 'http://localhost:8000/autogen/api/v1/chat/completions' \ - -H 'accept: application/json' \ - -H 'Content-Type: application/json' \ - -d '{ - "model": "model_name_v0.1", - "messages": [ - { - "role": "user", - "content": "Hey Mitch, can you start us off with a joke?" - } - ], - "temperature": 1, - "top_p": 1, - "presence_penalty": 0, - "frequency_penalty": 0, - "stream": true -}' -``` -Note that you must provide the entire conversation history to the backend, as the server expects input in OpenAI format. - ## Documentation Navigate to http://localhost:8000/autogen to see the docs. From 66f8d94587d6317f0567161e825054f69bd46c0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=B3=E4=B9=A0=E4=B9=A0?= <3494084267@qq.com> Date: Wed, 16 Oct 2024 21:57:43 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E5=9C=A8main=E9=87=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E8=AF=B7=E6=B1=82=E5=A4=B4=E4=B8=AD=E7=9A=84key?= =?UTF-8?q?=E6=A3=80=E9=AA=8C=20=E5=9C=A8app=E6=96=87=E4=BB=B6=E5=A4=B9?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E4=B8=8B=E6=96=B0=E5=A2=9E=E4=BA=86agents?= =?UTF-8?q?=E7=9A=84json=E6=A0=BC=E5=BC=8F=E6=96=87=E4=BB=B6=20=E5=9C=A8au?= =?UTF-8?q?togen=5Fagents.py=E9=87=8C=E6=B7=BB=E5=8A=A0=E4=BA=86agents=5Fc?= =?UTF-8?q?onfigs=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96=E5=87=BD=E6=95=B0=20?= =?UTF-8?q?=E5=9C=A8=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6docker-compose?= =?UTF-8?q?=E4=B8=AD=E7=9A=84environment=E4=B8=AD=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86AUTH=5FKEYS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/agent_configs.json | 24 ++++++++++++++++++++++++ app/autogen_agents.py | 38 ++++++++++++-------------------------- app/main.py | 24 +++++++++++++++++++++--- docker-compose.yml | 1 + 4 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 app/agent_configs.json diff --git a/app/agent_configs.json b/app/agent_configs.json new file mode 100644 index 0000000..1fdbb45 --- /dev/null +++ b/app/agent_configs.json @@ -0,0 +1,24 @@ +{ + "article_writer": { + "name": "ArticleWriter", + "description": "默认的文章生成器,先根据需求描述生成文章提纲,然后根据提纲生成文章。", + "agents": [ + { + "type": "ConversableAgent", + "name": "writer", + "system_message": "根据editor提供的文章主题和大纲内容写作文章正文,正文应该是一段或多段文字,而不是大纲式列表,写作完成后将内容提交给editor检查,如果editor提出修改建议,则按要求修改,直到文章完成。", + "human_input_mode": "NEVER", + "code_execution_config": false, + "llm_config": {} + }, + { + "type": "ConversableAgent", + "name": "editor", + "system_message": "分析并理解user提出的文章撰写要求,构思文章大纲和标题,然后通过多次对话依次将大纲的每个部分交给writer,要求writer根据大纲撰写该章节内容。writer写作完每一部分后你都要检查是否符合主题和字数要求,不符合要求的提出修改建议,如此依次重复直到全部章节写作完成。文章完成后将写作文章的全文汇总并包含在
标记中输出,然后结束对话并输出 TERMINATE", + "human_input_mode": "NEVER", + "code_execution_config": false, + "llm_config": {} + } + ] + } +} \ No newline at end of file diff --git a/app/autogen_agents.py b/app/autogen_agents.py index 0f610ae..f5462d0 100644 --- a/app/autogen_agents.py +++ b/app/autogen_agents.py @@ -1,6 +1,8 @@ from autogen import ChatResult, GroupChat, Agent, OpenAIWrapper, ConversableAgent, UserProxyAgent, GroupChatManager from data_model import ModelInformation import os +# 导入json依赖库 +import json config_list = [{ "model": os.getenv("LLM_MODEL", "qwen-plus"), @@ -11,32 +13,16 @@ llm_config = {"config_list": config_list, "cache_seed": 42} -agent_configs = { - "article_writer": { - "name": "ArticleWriter", - "description": "默认的文章生成器,先根据需求描述生成文章提纲,然后根据提纲生成文章。", - "agents": [ - { - "type": "ConversableAgent", - "name": "writer", - "system_message": "根据editor提供的文章主题和大纲内容写作文章正文,正文应该是一段或多段文字,而不是大纲式列表,写作完成后将内容提交给editor检查,如果editor提出修改建议,则按要求修改,直到文章完成。", - "human_input_mode": "NEVER", - "code_execution_config": False, - "llm_config": llm_config - }, - { - "type": "ConversableAgent", - "name": "editor", - "system_message": """分析并理解user提出的文章撰写要求,构思文章大纲和标题,然后通过多次对话依次将大纲的每个部分交给writer,要求writer根据大纲撰写该章节内容。 - writer写作完每一部分后你都要检查是否符合主题和字数要求,不符合要求的提出修改建议,如此依次重复直到全部章节写作完成。 - 文章完成后将写作文章的全文汇总并包含在
标记中输出,然后结束对话并输出 TERMINATE""", - "human_input_mode": "NEVER", - "code_execution_config": False, - "llm_config": llm_config - } - ] - } -} +# 读取 JSON 文件,初始化agent_configs +def load_agent(file_path): + with open(file_path, 'r', encoding='utf-8') as file: + agent_configs = json.load(file) + for agent_group_name, config in agent_configs.items(): + for agent in config['agents']: + agent['llm_config'] = llm_config + return agent_configs + +agent_configs = load_agent('agent_configs.json') models = {} diff --git a/app/main.py b/app/main.py index d3e959b..09f35ae 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,6 @@ from dotenv import load_dotenv -from fastapi import FastAPI -from fastapi import HTTPException +import os +from fastapi import FastAPI, Depends, HTTPException, Request, Response from starlette.responses import RedirectResponse from autogen_server import serve_autogen @@ -9,6 +9,24 @@ load_dotenv() +# 读取配置文件中的keys +AUTH_KEYS: os.getenv("AUTH_KEYS").split(",") + +# 验证请求头中是否有正确的api_key +def authorization(req: Request): + if(not req.headers.get("Authorization")): + raise HTTPException( + status_code=401, + detail="Unauthorized" + ) + token = req.headers["Authorization"].replace("Bearer ", "") + if token not in AUTH_KEYS: + raise HTTPException( + status_code=401, + detail="Unauthorized" + ) + return True + base = "/autogen/" prefix = base + "api/v1" openapi_url = prefix + "/openapi.json" @@ -35,7 +53,7 @@ async def get_models(): @app.post(prefix + "/chat/completions") -async def route_query(model_input: Input): +async def route_query(model_input: Input, authorized: bool = Depends(authorization)): # model_services = { # model_info.name: serve_autogen, # } diff --git a/docker-compose.yml b/docker-compose.yml index 7399b30..b937fe5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,6 +4,7 @@ services: volumes: - ./:/app environment: + - AUTH_KEYS=YOUR_KEYS - LLM_MODEL=qwen-plus - BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1 - API_KEY=YOUR_API_KEY From 02568fce311f389d84c390732b664d7449b6b377 Mon Sep 17 00:00:00 2001 From: 0000 Date: Wed, 16 Oct 2024 22:05:46 +0800 Subject: [PATCH 5/7] merge new branch and make tiny optimization --- app/main.py | 2 +- docker-compose.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 09f35ae..2aba0a8 100644 --- a/app/main.py +++ b/app/main.py @@ -10,7 +10,7 @@ load_dotenv() # 读取配置文件中的keys -AUTH_KEYS: os.getenv("AUTH_KEYS").split(",") +AUTH_KEYS = os.getenv("AUTH_KEYS").split(",") # 验证请求头中是否有正确的api_key def authorization(req: Request): diff --git a/docker-compose.yml b/docker-compose.yml index b937fe5..4187349 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,7 @@ services: volumes: - ./:/app environment: - - AUTH_KEYS=YOUR_KEYS + - AUTH_KEYS=YOUR_HTTP_AUTHORIZATION_KEYS_SEPARATED_BY_COMMA - LLM_MODEL=qwen-plus - BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1 - API_KEY=YOUR_API_KEY From 2d05bcafb9cceefa891233b6352baf63dd8bb8b8 Mon Sep 17 00:00:00 2001 From: 0000 Date: Wed, 16 Oct 2024 22:13:05 +0800 Subject: [PATCH 6/7] doc: Update readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1bd5e8a..e74efc6 100755 --- a/README.md +++ b/README.md @@ -14,11 +14,11 @@ docker compose up Navigate to http://localhost:8000/autogen to see the docs. ## Call different agent groups with different model definitions -We can define different agent groups for different purposes. +We can define different agent groups for different purposes in a single json file (app/agent_configs.json). After defined, we can call the agent group with the there id as model parameter. For example we have a definition as follows: -```python -agent_configs = { +```json +{ "article_writer": { "name": "ArticleWriter", # other parameters ... From dc05237637db351d22ab839826dd441bbc6437cb Mon Sep 17 00:00:00 2001 From: root Date: Thu, 31 Oct 2024 09:11:12 +0800 Subject: [PATCH 7/7] fix: Timeout when combining the final article --- .gitignore | 2 ++ README.md | 3 ++- app/autogen_agents.py | 9 ++++----- app/autogen_server.py | 2 +- app/main.py | 2 +- docker-compose.yml | 1 + 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 7b6caf3..1c1a859 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,5 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. .idea/ + +/dev-compose.yml \ No newline at end of file diff --git a/README.md b/README.md index e74efc6..c9ba450 100755 --- a/README.md +++ b/README.md @@ -31,12 +31,13 @@ curl -X 'POST' \ 'http://localhost:8000/autogen/api/v1/chat/completions' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ + -H 'Authorization: Bearer 976707f2ab39ebee343034b4b33db6f9' \ -d '{ "model": "article_writer", "messages": [ { "role": "user", - "content": "写一篇向小学生介绍宋朝历史的文章,涵盖宋朝的建立、兴盛、衰落的过程,大约2000字。" + "content": "写一篇向小学生介绍明朝历史的文章,涵盖明朝的建立、兴盛、衰落的过程,大约3000字。" } ], "temperature": 1, diff --git a/app/autogen_agents.py b/app/autogen_agents.py index f5462d0..5142c00 100644 --- a/app/autogen_agents.py +++ b/app/autogen_agents.py @@ -11,7 +11,7 @@ "price" : [0.004, 0.012] }] -llm_config = {"config_list": config_list, "cache_seed": 42} +llm_config = {"config_list": config_list, "cache_seed": 42, "timeout": 180} # 读取 JSON 文件,初始化agent_configs def load_agent(file_path): @@ -22,7 +22,7 @@ def load_agent(file_path): agent['llm_config'] = llm_config return agent_configs -agent_configs = load_agent('agent_configs.json') +agent_configs = load_agent('/app/app/agent_configs.json') models = {} @@ -58,13 +58,12 @@ def build_agents(agent_id): human_input_mode="NEVER" )) for config in agent_configs[agent_id]["agents"]: - agents.append( - ConversableAgent( + _agent = ConversableAgent( name=config["name"], system_message=config["system_message"], human_input_mode=config["human_input_mode"], code_execution_config=config["code_execution_config"], llm_config=config["llm_config"], ) - ) + agents.append(_agent) return agents \ No newline at end of file diff --git a/app/autogen_server.py b/app/autogen_server.py index edef07f..434dcde 100644 --- a/app/autogen_server.py +++ b/app/autogen_server.py @@ -59,7 +59,7 @@ def return_streaming_response(inp: Input, queue: Queue): usage=empty_usage, model=inp.model, ) - yield f"data: {json.dumps(chunk.model_dump())}\n\n" + yield f"data: {json.dumps(chunk.model_dump(), ensure_ascii=False)}\n\n" queue.task_done() diff --git a/app/main.py b/app/main.py index 2aba0a8..6d11676 100644 --- a/app/main.py +++ b/app/main.py @@ -9,7 +9,7 @@ load_dotenv() -# 读取配置文件中的keys +# 读取环境变量中的keys AUTH_KEYS = os.getenv("AUTH_KEYS").split(",") # 验证请求头中是否有正确的api_key diff --git a/docker-compose.yml b/docker-compose.yml index 4187349..0a6e1bf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,7 @@ services: build: . volumes: - ./:/app + - ~/.cache:/root/.cache environment: - AUTH_KEYS=YOUR_HTTP_AUTHORIZATION_KEYS_SEPARATED_BY_COMMA - LLM_MODEL=qwen-plus