diff --git a/metagpt/configs/llm_config.py b/metagpt/configs/llm_config.py index 222e116ee..2a04116f5 100644 --- a/metagpt/configs/llm_config.py +++ b/metagpt/configs/llm_config.py @@ -32,6 +32,7 @@ class LLMType(Enum): MISTRAL = "mistral" YI = "yi" # lingyiwanwu OPENROUTER = "openrouter" + AMAZON_BEDROCK = "amazon_bedrock" def __missing__(self, key): return self.OPENAI @@ -74,10 +75,14 @@ class LLMConfig(YamlModel): best_of: Optional[int] = None n: Optional[int] = None stream: bool = False - logprobs: Optional[bool] = None # https://cookbook.openai.com/examples/using_logprobs + # https://cookbook.openai.com/examples/using_logprobs + logprobs: Optional[bool] = None top_logprobs: Optional[int] = None timeout: int = 600 + # For Amazon Bedrock + region_name: str = None + # For Network proxy: Optional[str] = None diff --git a/metagpt/provider/bedrock/.gitignore b/metagpt/provider/bedrock/.gitignore new file mode 100644 index 000000000..971fcecb7 --- /dev/null +++ b/metagpt/provider/bedrock/.gitignore @@ -0,0 +1,192 @@ +### Python template + +# Byte-compiled / optimized / DLL files +__pycache__ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST +metagpt/tools/schemas/ +examples/data/search_kb/*.json + +# PyInstaller +# Usually these files are written by a python scripts from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ +unittest.txt + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +logs +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# report +allure-report +allure-results + +# idea / vscode / macos +.idea +.DS_Store +.vscode + +key.yaml +/data/ +data.ms +examples/nb/ +examples/default__vector_store.json +examples/docstore.json +examples/graph_store.json +examples/image__vector_store.json +examples/index_store.json +.chroma +*~$* +workspace/* +tmp +metagpt/roles/idea_agent.py +.aider* +*.bak +*.bk + +# output folder +output +tmp.png +.dependencies.json +tests/metagpt/utils/file_repo_git +tests/data/rsp_cache_new.json +*.tmp +*.png +htmlcov +htmlcov.* +cov.xml +*.dot +*.pkl +*.faiss +*-structure.csv +*-structure.json +*.dot +.python-version +# aws access key +config.py \ No newline at end of file diff --git a/metagpt/provider/bedrock/__init__.py b/metagpt/provider/bedrock/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/metagpt/provider/bedrock/amazon_bedrock_api.py b/metagpt/provider/bedrock/amazon_bedrock_api.py new file mode 100644 index 000000000..ecdee4154 --- /dev/null +++ b/metagpt/provider/bedrock/amazon_bedrock_api.py @@ -0,0 +1,57 @@ + +import json +from typing import Coroutine, Literal +from metagpt.const import USE_CONFIG_TIMEOUT +from metagpt.provider.llm_provider_registry import register_provider +from metagpt.configs.llm_config import LLMConfig, LLMType +from metagpt.provider.base_llm import BaseLLM +from metagpt.logs import log_llm_stream, logger +from botocore.config import Config +import boto3 + + +@register_provider([LLMType.AMAZON_BEDROCK]) +class AmazonBedrockLLM(BaseLLM): + def __init__(self, config: LLMConfig): + self.config = config + self.__client = self.__init_client("bedrock-runtime") + + def __init_client(self, service_name: Literal["bedrock-runtime", "bedrock"]): + # access key from https://us-east-1.console.aws.amazon.com/iam + self.__credentital_kwards = { + "aws_secret_access_key": self.config.secret_key, + "aws_access_key_id": self.config.access_key, + "region_name": self.config.region_name + } + session = boto3.Session(**self.__credentital_kwards) + client = session.client(service_name) + return client + + def list_models(self): + """see https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock/client/list_foundation_models.html""" + client = self.__init_client("bedrock") + # only output text-generation models + response = client.list_foundation_models(byOutputModality='TEXT') + summaries = [f'{summary.get("modelId", ""):50} Support Streaming:{summary.get("responseStreamingSupported","")}' + for summary in response.get("modelSummaries", {})] + logger.info("\n"+"\n".join(summaries)) + + def _achat_completion(self, messages: list[dict], timeout=USE_CONFIG_TIMEOUT): + pass + + def _achat_completion_stream(self, messages: list[dict], timeout=USE_CONFIG_TIMEOUT): + pass + + def completion(self, messages): + pass + + def acompletion(self, messages: list[dict]): + pass + + +if __name__ == '__main__': + from .config import my_config + prompt = "who are you?" + messages = [{"role": "user", "content": prompt}] + llm = AmazonBedrockLLM(my_config) + llm.list_models() diff --git a/metagpt/provider/bedrock/base_provider.py b/metagpt/provider/bedrock/base_provider.py new file mode 100644 index 000000000..eaedfe045 --- /dev/null +++ b/metagpt/provider/bedrock/base_provider.py @@ -0,0 +1,3 @@ +from abc import ABC +class BaseBedrockProvider(ABC): + pass \ No newline at end of file diff --git a/metagpt/provider/bedrock/bedrock_provide.py b/metagpt/provider/bedrock/bedrock_provide.py new file mode 100644 index 000000000..e69de29bb