Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

Added `OPENAI_API_BASE_URL` to openai_model reference (#338) (#341)

River Yan0e446e1f2306
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 6aa2b50d8..2ea367a7f 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -145,6 +145,7 @@ If you're developing with VSCode, make sure to create a `.env` file in the repos
```
OPENAI_API_KEY=sk-XXXXXXXX
+OPENAI_API_BASE_URL=https://XXXXXXXX (Should you utilize an OpenAI proxy service, kindly specify this)
```
## Documentation 📚
diff --git a/README.md b/README.md
index e2fd43649..1b1439234 100644
--- a/README.md
+++ b/README.md
@@ -129,6 +129,7 @@ First, you need to add your OpenAI API key to system environment variables. The
```bash
# Export your OpenAI API key
export OPENAI_API_KEY=<insert your OpenAI API key>
+OPENAI_API_BASE_URL=<inert your OpenAI API BASE URL> #(Should you utilize an OpenAI proxy service, kindly specify this)
```
**For Windows Command Prompt:**
@@ -136,6 +137,7 @@ export OPENAI_API_KEY=<insert your OpenAI API key>
```cmd
REM export your OpenAI API key
set OPENAI_API_KEY=<insert your OpenAI API key>
+set OPENAI_API_BASE_URL=<inert your OpenAI API BASE URL> #(Should you utilize an OpenAI proxy service, kindly specify this)
```
**For Windows PowerShell:**
@@ -143,6 +145,8 @@ set OPENAI_API_KEY=<insert your OpenAI API key>
```powershell
# Export your OpenAI API key
$env:OPENAI_API_KEY="<insert your OpenAI API key>"
+$env:OPENAI_API_BASE_URL="<inert your OpenAI API BASE URL>" #(Should you utilize an OpenAI proxy service, kindly specify this)
+
```
Replace `<insert your OpenAI API key>` with your actual OpenAI API key in each case. Make sure there are no spaces around the `=` sign.
diff --git a/camel/agents/task_agent.py b/camel/agents/task_agent.py
index 4cbaed118..f80d0d450 100644
--- a/camel/agents/task_agent.py
+++ b/camel/agents/task_agent.py
@@ -104,12 +104,13 @@ class TaskSpecifyAgent(ChatAgent):
task_msg = BaseMessage.make_user_message(role_name="Task Specifier",
content=task_specify_prompt)
specifier_response = self.step(task_msg)
- if len(specifier_response.msgs) == 0:
- raise RuntimeError("Got no specification message.")
- specified_task_msg = specifier_response.msgs[0]
if specifier_response.terminated:
raise RuntimeError("Task specification failed.")
+ if len(specifier_response.msgs) == 0:
+ raise RuntimeError("Got no specification message.")
+
+ specified_task_msg = specifier_response.msgs[0]
return TextPrompt(specified_task_msg.content)
@@ -172,10 +173,10 @@ class TaskPlannerAgent(ChatAgent):
task_response = self.step(task_msg)
- if len(task_response.msgs) == 0:
- raise RuntimeError("Got no task planning message.")
if task_response.terminated:
raise RuntimeError("Task planning failed.")
+ if len(task_response.msgs) == 0:
+ raise RuntimeError("Got no task planning message.")
sub_tasks_msg = task_response.msgs[0]
return TextPrompt(sub_tasks_msg.content)
@@ -281,10 +282,10 @@ Be concrete.
content=task_creation_prompt)
task_response = self.step(task_msg)
- if len(task_response.msgs) == 0:
- raise RuntimeError("Got no task creation message.")
if task_response.terminated:
raise RuntimeError("Task creation failed.")
+ if len(task_response.msgs) == 0:
+ raise RuntimeError("Got no task creation message.")
sub_tasks_msg = task_response.msgs[0]
return get_task_list(sub_tasks_msg.content)
@@ -372,10 +373,10 @@ with any other output.""")
task_response = self.step(task_msg)
- if len(task_response.msgs) == 0:
- raise RuntimeError("Got no task prioritization message.")
if task_response.terminated:
raise RuntimeError("Task prioritization failed.")
+ if len(task_response.msgs) == 0:
+ raise RuntimeError("Got no task prioritization message.")
sub_tasks_msg = task_response.msgs[0]
return get_task_list(sub_tasks_msg.content)
diff --git a/camel/models/openai_model.py b/camel/models/openai_model.py
index d0ba75c69..1722317f0 100644
--- a/camel/models/openai_model.py
+++ b/camel/models/openai_model.py
@@ -11,6 +11,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+import os
+import urllib
from types import GeneratorType
from typing import Any, Dict, List, Optional
@@ -65,7 +67,11 @@ class OpenAIModel(BaseModelBackend):
Dict[str, Any]: Response in the OpenAI API format.
"""
import openai
- openai.api_base = DEFAULT_API_BASE
+ url = os.environ.get('OPENAI_API_BASE_URL', DEFAULT_API_BASE)
+ openai.api_base = url if all([
+ urllib.parse.urlparse(url).scheme,
+ urllib.parse.urlparse(url).netloc
+ ]) else DEFAULT_API_BASE
messages_openai: List[OpenAIMessage] = messages
response = openai.ChatCompletion.create(messages=messages_openai,
diff --git a/examples/ai_society/babyagi_playing.py b/examples/ai_society/babyagi_playing.py
index 976670d95..c97ab14e7 100644
--- a/examples/ai_society/babyagi_playing.py
+++ b/examples/ai_society/babyagi_playing.py
@@ -18,7 +18,7 @@ from camel.societies import BabyAGI
from camel.utils import print_text_animated
-def main(model_type=None) -> None:
+def main(model_type=None, chat_turn_limit=15) -> None:
task_prompt = "Develop a trading bot for the stock market"
babyagi_session = BabyAGI(
@@ -39,7 +39,7 @@ def main(model_type=None) -> None:
print(Fore.RED +
f"Final task prompt:\n{babyagi_session.specified_task_prompt}\n")
- chat_turn_limit, n = 15, 0
+ n = 0
while n < chat_turn_limit:
n += 1
assistant_response = babyagi_session.step()
diff --git a/examples/test/test_babyagi_example.py b/examples/test/test_babyagi_example.py
index 8f4cb2c33..f36799ca3 100644
--- a/examples/test/test_babyagi_example.py
+++ b/examples/test/test_babyagi_example.py
@@ -24,4 +24,5 @@ parametrize = pytest.mark.parametrize('model', [
@parametrize
def test_ai_society_babyagi_playing_example(model):
- examples.ai_society.babyagi_playing.main(model_type=model)
+ examples.ai_society.babyagi_playing.main(model_type=model,
+ chat_turn_limit=2)