implement framework

This commit is contained in:
usamimeri_renko 2024-04-25 12:00:27 +08:00
parent c779f6977e
commit fd4c0fde26
6 changed files with 258 additions and 1 deletions

View file

@ -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

192
metagpt/provider/bedrock/.gitignore vendored Normal file
View file

@ -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

View file

View file

@ -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()

View file

@ -0,0 +1,3 @@
from abc import ABC
class BaseBedrockProvider(ABC):
pass