Skip to content

Commit 40a1089

Browse files
authored
feat(dashscope): support to load headers from the environment (#1096)
1 parent e584987 commit 40a1089

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/agentscope/model/_dashscope_model.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
"""The dashscope API model classes."""
33
import collections
4+
import json
5+
import os
46
import warnings
57
from datetime import datetime
68
from http import HTTPStatus
@@ -100,6 +102,25 @@ def __init__(
100102

101103
dashscope.base_http_api_url = base_http_api_url
102104

105+
# Load headers from environment variable if exists
106+
headers = os.getenv("DASHSCOPE_API_HEADERS")
107+
if headers:
108+
try:
109+
headers = json.loads(str(headers))
110+
if not isinstance(headers, dict):
111+
raise json.JSONDecodeError("", "", 0)
112+
113+
if self.generate_kwargs.get("headers"):
114+
headers.update(self.generate_kwargs["headers"])
115+
116+
self.generate_kwargs["headers"] = headers
117+
118+
except json.JSONDecodeError:
119+
logger.warning(
120+
"Failed to parse DASHSCOPE_API_HEADERS environment "
121+
"variable as JSON. It should be a JSON object.",
122+
)
123+
103124
@trace_llm
104125
async def __call__(
105126
self,

0 commit comments

Comments
 (0)