From 8c07be0e564c497e64d86f96b1f4d05e7f533971 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E5=87=8C=E9=A3=8E?= Date: Fri, 21 Jul 2023 13:17:26 -0400 Subject: [PATCH 1/4] add AnthropicAPI --- config/config.yaml | 3 +++ examples/llm_hello_world.py | 5 +++-- metagpt/config.py | 2 ++ metagpt/llm.py | 3 ++- metagpt/provider/anthropic_api.py | 34 +++++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 metagpt/provider/anthropic_api.py diff --git a/config/config.yaml b/config/config.yaml index 30168d81e..3bca12afa 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -10,6 +10,9 @@ OPENAI_API_MODEL: "gpt-4" MAX_TOKENS: 1500 RPM: 10 +#### if Anthropic +#Anthropic_API_KEY: "YOUR_API_KEY" + #### if AZURE, check https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb #OPENAI_API_TYPE: "azure" diff --git a/examples/llm_hello_world.py b/examples/llm_hello_world.py index e62f8dc3c..6b42ef5ef 100644 --- a/examples/llm_hello_world.py +++ b/examples/llm_hello_world.py @@ -9,11 +9,12 @@ import asyncio from metagpt.logs import logger from metagpt.llm import LLM - +from metagpt.llm import Claude async def main(): llm = LLM() - + claude = Claude() + logger.info(await claude.aask('你好,请进行自我介绍')) logger.info(await llm.aask('hello world')) logger.info(await llm.aask_batch(['hi', 'write python hello world.'])) diff --git a/metagpt/config.py b/metagpt/config.py index e60bc1927..84867258e 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -54,6 +54,8 @@ class Config(metaclass=Singleton): self.max_tokens_rsp = self._get('MAX_TOKENS', 2048) self.deployment_id = self._get('DEPLOYMENT_ID') + self.claude_api_key = self._get('Anthropic_API_KEY') + self.serpapi_api_key = self._get('SERPAPI_API_KEY') self.serper_api_key = self._get('SERPER_API_KEY') self.google_api_key = self._get('GOOGLE_API_KEY') diff --git a/metagpt/llm.py b/metagpt/llm.py index 098190eb0..ec345f49f 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -7,9 +7,10 @@ """ from metagpt.provider.openai_api import OpenAIGPTAPI as LLM +from metagpt.provider.anthropic_api import Claude2 as Claude DEFAULT_LLM = LLM() - +Claude_LLM = Claude() async def ai_func(prompt): """使用LLM进行QA""" diff --git a/metagpt/provider/anthropic_api.py b/metagpt/provider/anthropic_api.py new file mode 100644 index 000000000..ef1c21188 --- /dev/null +++ b/metagpt/provider/anthropic_api.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/7/21 11:15 +@Author : Leo Xiao +@File : anthropic_api.py +""" + +import asyncio +import anthropic +from anthropic import Anthropic +from metagpt.config import CONFIG + +class Claude2: + def ask(self, prompt): + client = Anthropic(api_key=claude_api_key) + + res = client.completions.create( + model="claude-2", + prompt=f"{anthropic.HUMAN_PROMPT} {prompt} {anthropic.AI_PROMPT}", + max_tokens_to_sample=1000, + ) + return res.completion + + async def aask(self, prompt): + client = Anthropic(api_key="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") + + res = client.completions.create( + model="claude-2", + prompt=f"{anthropic.HUMAN_PROMPT} {prompt} {anthropic.AI_PROMPT}", + max_tokens_to_sample=1000, + ) + return res.completion + From 227c837d0a26b9a1bb5c4ece322aa8ae051c8d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E5=87=8C=E9=A3=8E?= Date: Fri, 21 Jul 2023 13:44:58 -0400 Subject: [PATCH 2/4] Your commit message --- llm_hello_world.py | 31 +++++++++++++++++++++++++++++++ metagpt/provider/anthropic_api.py | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 llm_hello_world.py diff --git a/llm_hello_world.py b/llm_hello_world.py new file mode 100644 index 000000000..6b42ef5ef --- /dev/null +++ b/llm_hello_world.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/5/6 14:13 +@Author : alexanderwu +@File : llm_hello_world.py +""" +import asyncio + +from metagpt.logs import logger +from metagpt.llm import LLM +from metagpt.llm import Claude + +async def main(): + llm = LLM() + claude = Claude() + logger.info(await claude.aask('你好,请进行自我介绍')) + logger.info(await llm.aask('hello world')) + logger.info(await llm.aask_batch(['hi', 'write python hello world.'])) + + hello_msg = [{'role': 'user', 'content': 'count from 1 to 10. split by newline.'}] + logger.info(await llm.acompletion(hello_msg)) + logger.info(await llm.acompletion_batch([hello_msg])) + logger.info(await llm.acompletion_batch_text([hello_msg])) + + logger.info(await llm.acompletion_text(hello_msg)) + await llm.acompletion_text(hello_msg, stream=True) + + +if __name__ == '__main__': + asyncio.run(main()) diff --git a/metagpt/provider/anthropic_api.py b/metagpt/provider/anthropic_api.py index ef1c21188..fae4e53eb 100644 --- a/metagpt/provider/anthropic_api.py +++ b/metagpt/provider/anthropic_api.py @@ -23,7 +23,7 @@ class Claude2: return res.completion async def aask(self, prompt): - client = Anthropic(api_key="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") + client = Anthropic(api_key=claude_api_key) res = client.completions.create( model="claude-2", From 326082da728a3d3c6cfedabf0f68cb626f04b857 Mon Sep 17 00:00:00 2001 From: Leo Date: Fri, 21 Jul 2023 13:53:39 -0400 Subject: [PATCH 3/4] Add Anthropic Api --- llm_hello_world.py | 31 ------------------------------- metagpt/provider/anthropic_api.py | 4 ++-- 2 files changed, 2 insertions(+), 33 deletions(-) delete mode 100644 llm_hello_world.py diff --git a/llm_hello_world.py b/llm_hello_world.py deleted file mode 100644 index 6b42ef5ef..000000000 --- a/llm_hello_world.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/5/6 14:13 -@Author : alexanderwu -@File : llm_hello_world.py -""" -import asyncio - -from metagpt.logs import logger -from metagpt.llm import LLM -from metagpt.llm import Claude - -async def main(): - llm = LLM() - claude = Claude() - logger.info(await claude.aask('你好,请进行自我介绍')) - logger.info(await llm.aask('hello world')) - logger.info(await llm.aask_batch(['hi', 'write python hello world.'])) - - hello_msg = [{'role': 'user', 'content': 'count from 1 to 10. split by newline.'}] - logger.info(await llm.acompletion(hello_msg)) - logger.info(await llm.acompletion_batch([hello_msg])) - logger.info(await llm.acompletion_batch_text([hello_msg])) - - logger.info(await llm.acompletion_text(hello_msg)) - await llm.acompletion_text(hello_msg, stream=True) - - -if __name__ == '__main__': - asyncio.run(main()) diff --git a/metagpt/provider/anthropic_api.py b/metagpt/provider/anthropic_api.py index fae4e53eb..36cd8e27a 100644 --- a/metagpt/provider/anthropic_api.py +++ b/metagpt/provider/anthropic_api.py @@ -13,7 +13,7 @@ from metagpt.config import CONFIG class Claude2: def ask(self, prompt): - client = Anthropic(api_key=claude_api_key) + client = Anthropic(api_key=CONFIG.claude_api_key) res = client.completions.create( model="claude-2", @@ -23,7 +23,7 @@ class Claude2: return res.completion async def aask(self, prompt): - client = Anthropic(api_key=claude_api_key) + client = Anthropic(api_key=CONFIG.claude_api_key) res = client.completions.create( model="claude-2", From ccb2e1ab7b6d516ad9ece71bc0455987e422dd93 Mon Sep 17 00:00:00 2001 From: Leo Date: Fri, 21 Jul 2023 14:06:40 -0400 Subject: [PATCH 4/4] Add Anthropic Api --- metagpt/llm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/llm.py b/metagpt/llm.py index ec345f49f..c45dd9806 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -10,7 +10,7 @@ from metagpt.provider.openai_api import OpenAIGPTAPI as LLM from metagpt.provider.anthropic_api import Claude2 as Claude DEFAULT_LLM = LLM() -Claude_LLM = Claude() +CLAUDE_LLM = Claude() async def ai_func(prompt): """使用LLM进行QA"""