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 +