Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add azure openai #49

Merged
merged 3 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

**AgentVerse** offers a versatile framework that streamlines the process of creating custom multi-agent environments for large language models (LLMs). Designed to facilitate swift development and customization with minimal effort, our framework empowers researchers to concentrate on their research, rather than being bogged down by implementation details.

⚠️⚠️⚠️ We're presently in the process of refactoring our code to offer you the flexibility to construct both simulation environments (without a predefined goal) and task-solving environments (with a specific goal). Please note that our README is currently outdated, we will update it very soon. If you require a stable version of our code that exclusively supports simulation environments, you can find it in our [`release-1.0`](https://github.com/OpenBMB/AgentVerse/tree/release-1.0) branch.
⚠️⚠️⚠️ We're refactoring the code, and the goal is to provide a flexibility to construct simulation(without a predefined goal) and task-solving(with a specific goal) environments. Please note that this README is outdated, we will update it soon. If you require a stable version that exclusively supports simulation environments, you can use [`release-1.0`](https://github.com/OpenBMB/AgentVerse/tree/release-1.0) branch.

---

Expand Down Expand Up @@ -79,7 +79,7 @@ In the NLP class, the professor and students engage in interactive communication

Use the following command to launch the NLP Classroom example:
```bash
python main_demo.py --task nlp_classroom_9players
python main_simulation_gui.py --task nlp_classroom_9players
```

https://github.com/OpenBMB/AgentVerse/assets/11704492/6ea07850-595e-4a28-a82e-f863011353c2
Expand All @@ -90,7 +90,7 @@ A prisoner's Dilemma is a thought experiment that challenges two completely rati

Use the following command to launch the Prisoner Dilemma example:
```bash
python main_demo.py --task prisoner_dilemma
python main_simulation_cli.py --task prisoner_dilemma
```

https://github.com/OpenBMB/AgentVerse/assets/11704492/017c46e5-c738-4fca-9352-b008e2d518bd
Expand All @@ -112,7 +112,7 @@ https://github.com/OpenBMB/AgentVerse/assets/11704492/5058066a-abee-490d-8659-b4
In the database diagnosis scenario, the Chief DBA monitors the system anomalies (e.g., slow queries, locks, crash down). If detected, the domain experts are alerted to analyze root causes, share insights, and suggest optimization solutions together. The Chief DBA then provides a summarized report to the user.

```bash
python main_demo.py --task db_diag
python main_simulation_gui.py --task db_diag
```

https://github.com/OpenBMB/AgentVerse/assets/11704492/c633419d-afbb-47d4-bb12-6bb512e7af3a
Expand Down Expand Up @@ -195,10 +195,19 @@ pip install -r requirements.txt
```
Some users have reported problems installing the `orjson` required by `gradio`. One simple workaround is to install it with Anaconda `conda install -c conda-forge orjson`.

You also need to export your OpenAI API key as follows
You also need to export your OpenAI API key as follows
```bash
# Export your OpenAI API key
export OPENAI_API_KEY="your_api_key_here"
# Or if you are using Azure
export AZURE_OPENAI_API_KEY="your_api_key_here"
export AZURE_OPENAI_API_BASE="your_api_base_here"
```

If you want use Azure OpenAI services, pleas export your Azure OpenAI key and OpenAI API base as follows:
```bash
export AZURE_OPENAI_API_KEY="your_api_key_here"
export AZURE_OPENAI_API_BASE="your_api_base_here"
```

If you want to use the tools provided by BMTools, you need to install BMTools as follows:
Expand Down Expand Up @@ -231,7 +240,7 @@ python3 main.py --task nlp_classroom_9players
We also provide a local website demo for this environment. You can launch it with

```shell
python3 main_demo.py --task nlp_classroom_9players
python3 main_simulation_gui.py --task nlp_classroom_9players
```
After successfully launching the local server, you can visit [http://127.0.0.1:7860/](http://127.0.0.1:7860/) to view the classroom environment.

Expand Down
5 changes: 5 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ pip install -r requirements.txt
# 导出你的OpenAI API密钥
export OPENAI_API_KEY="your_api_key_here"
```
或者您想使用 Azure OpenAI 服务,请按照以下方式配置 OpenAI API 密钥和 API base:
```bash
export AZURE_OPENAI_API_KEY="your_api_key_here"
export AZURE_OPENAI_API_BASE="your_api_base_here"
```

如果您想使用BMTools提供的工具,您需要按如下方式安装BMTools:
```bash
Expand Down
38 changes: 23 additions & 15 deletions agentverse/llms/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,27 @@
is_openai_available = False
logging.warning("openai package is not installed")
else:
openai.api_key = os.environ.get("OPENAI_API_KEY")
# openai.proxy = os.environ.get("http_proxy")
# if openai.proxy is None:
# openai.proxy = os.environ.get("HTTP_PROXY")
if openai.api_key is None:
if os.environ.get("OPENAI_API_KEY")!=None:
openai.api_key = os.environ.get("OPENAI_API_KEY")
is_openai_available = True
elif os.environ.get("AZURE_OPENAI_API_KEY")!=None:
openai.api_type = "azure"
openai.api_key = os.environ.get("AZURE_OPENAI_API_KEY")
openai.api_base = os.environ.get("AZURE_OPENAI_API_BASE")
openai.api_version = "2023-05-15"
is_openai_available = True
else:
logging.warning(
"OpenAI API key is not set. Please set the environment variable OPENAI_API_KEY"
)
is_openai_available = False
else:
is_openai_available = True

is_openai_available = False

class OpenAIChatArgs(BaseModelArgs):
model: str = Field(default="gpt-3.5-turbo")
deployment_id: str = Field(default="gpt-35-turbo")
max_tokens: int = Field(default=2048)
temperature: float = Field(default=1.0)
top_p: int = Field(default=1)
Expand Down Expand Up @@ -85,7 +91,7 @@ class OpenAIChatArgs(BaseModelArgs):
# total_tokens=response["usage"]["total_tokens"],
# )


@llm_registry.register("gpt-35-turbo")
@llm_registry.register("gpt-3.5-turbo")
@llm_registry.register("gpt-4")
class OpenAIChat(BaseChatModel):
Expand All @@ -94,21 +100,19 @@ class OpenAIChat(BaseChatModel):
def __init__(self, max_retry: int = 3, **kwargs):
args = OpenAIChatArgs()
args = args.dict()

for k, v in args.items():
args[k] = kwargs.pop(k, v)
if len(kwargs) > 0:
logging.warning(f"Unused arguments: {kwargs}")
super().__init__(args=args, max_retry=max_retry)

# def _construct_messages(self, history: List[Message]):
# return history + [{"role": "user", "content": query}]

@retry(
stop=stop_after_attempt(20),
wait=wait_exponential(multiplier=1, min=4, max=10),
reraise=True,
)

def generate_response(
self,
prepend_prompt: str = "",
Expand All @@ -118,10 +122,9 @@ def generate_response(
) -> LLMResult:
messages = self.construct_messages(prepend_prompt, history, append_prompt)
logger.log_prompt(messages)

try:
# Execute function call
if functions != []:
if functions != []:
response = openai.ChatCompletion.create(
messages=messages,
functions=functions,
Expand Down Expand Up @@ -283,9 +286,14 @@ def construct_messages(
def get_embedding(text: str, attempts=3) -> np.array:
try:
text = text.replace("\n", " ")
embedding = openai.Embedding.create(
input=[text], model="text-embedding-ada-002"
)["data"][0]["embedding"]
if openai.api_type=="azure":
embedding = openai.Embedding.create(
input=[text], deployment_id="text-embedding-ada-002"
)["data"][0]["embedding"]
else:
embedding = openai.Embedding.create(
input=[text], model="text-embedding-ada-002"
)["data"][0]["embedding"]
return tuple(embedding)
except Exception as e:
attempt += 1
Expand Down