5
5
import requests
6
6
from langchain_core .callbacks import CallbackManagerForLLMRun
7
7
from langchain_core .language_models .llms import LLM
8
- from langchain_core .pydantic_v1 import Extra , Field , root_validator
9
- from langchain_core .utils import get_from_dict_or_env
8
+ from langchain_core .pydantic_v1 import Extra , Field , SecretStr , root_validator
9
+ from langchain_core .utils import convert_to_secret_str , get_from_dict_or_env
10
10
11
11
from langchain_community .llms .utils import enforce_stop_tokens
12
12
@@ -33,7 +33,7 @@ class StochasticAI(LLM):
33
33
"""Holds any model parameters valid for `create` call not
34
34
explicitly specified."""
35
35
36
- stochasticai_api_key : Optional [str ] = None
36
+ stochasticai_api_key : Optional [SecretStr ] = None
37
37
38
38
class Config :
39
39
"""Configuration for this pydantic object."""
@@ -61,8 +61,8 @@ def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]:
61
61
@root_validator ()
62
62
def validate_environment (cls , values : Dict ) -> Dict :
63
63
"""Validate that api key exists in environment."""
64
- stochasticai_api_key = get_from_dict_or_env (
65
- values , "stochasticai_api_key" , "STOCHASTICAI_API_KEY"
64
+ stochasticai_api_key = convert_to_secret_str (
65
+ get_from_dict_or_env ( values , "stochasticai_api_key" , "STOCHASTICAI_API_KEY" )
66
66
)
67
67
values ["stochasticai_api_key" ] = stochasticai_api_key
68
68
return values
@@ -107,7 +107,7 @@ def _call(
107
107
url = self .api_url ,
108
108
json = {"prompt" : prompt , "params" : params },
109
109
headers = {
110
- "apiKey" : f"{ self .stochasticai_api_key } " ,
110
+ "apiKey" : f"{ self .stochasticai_api_key . get_secret_value () } " ,
111
111
"Accept" : "application/json" ,
112
112
"Content-Type" : "application/json" ,
113
113
},
@@ -119,7 +119,7 @@ def _call(
119
119
response_get = requests .get (
120
120
url = response_post_json ["data" ]["responseUrl" ],
121
121
headers = {
122
- "apiKey" : f"{ self .stochasticai_api_key } " ,
122
+ "apiKey" : f"{ self .stochasticai_api_key . get_secret_value () } " ,
123
123
"Accept" : "application/json" ,
124
124
"Content-Type" : "application/json" ,
125
125
},
0 commit comments