forked from sausheong/ghost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
132 lines (112 loc) · 3.4 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
from collections import namedtuple
from dotenv import load_dotenv, find_dotenv
def getSerpapiProviderConfig() -> str:
SerpapiConfig = namedtuple(
'SerpapiConfig',
[
'serpapi_api_key',
]
)
if not (serpapi_api_key := os.getenv('SERPAPI_API_KEY')):
return None
return SerpapiConfig(
serpapi_api_key=serpapi_api_key
)
def getOpenaiProviderConfig() -> dict:
OpenaiConfig = namedtuple(
'OpenaiConfig',
[
'provider',
'api_key',
'model_name',
'api_version',
'base_url',
],
)
return OpenaiConfig(
provider='openai',
api_key=os.getenv('OPENAI_API_KEY'),
model_name=os.getenv('OPENAI_MODEL'),
api_version=os.getenv('OPENAI_API_VERSION'),
base_url=os.getenv('OPENAI_API_BASE'),
)
def getAzureProviderConfig() -> dict:
AzureConfig = namedtuple(
'AzureConfig',
[
'provider',
'api_key',
'model_name',
'deployment_name',
'api_version',
'base_url',
],
)
return AzureConfig(
provider='azure',
api_key=os.getenv('AZURE_API_KEY'),
model_name=os.getenv('AZURE_MODEL'),
deployment_name=os.getenv('AZURE_DEPLOYMENT_NAME'),
api_version=os.getenv('AZURE_API_VERSION'),
base_url=os.getenv('AZURE_API_BASE'),
)
def getPalmProviderConfig() -> dict:
PalmConfig = namedtuple(
'PalmConfig',
[
'provider',
'model_name',
'location',
]
)
return PalmConfig(
provider='palm',
model_name=os.getenv('PALM_MODEL') or 'chat-bison',
location=os.getenv('PALM_LOCATION') or 'us-central1',
)
def getLlama2APIProviderConfig() -> dict:
Llama2APIConfig = namedtuple(
'Llama2APIConfig',
[
'provider',
'api_key',
'base_url',
'model_name',
]
)
return Llama2APIConfig(
provider='llama2api',
api_key=os.getenv('LLAMA2API_API_KEY'),
base_url=os.getenv('LLAMA2API_API_BASE'),
model_name = "Llama-2",
)
def getProviderConfig(provider_name: str) -> dict:
supported_providers = ('palm', 'azure', 'openai', 'llama2api')
if not isinstance(provider_name, str):
raise TypeError("Input must be a string.")
if provider_name not in supported_providers:
raise ValueError(
f"Invalid input. Supported providers: {supported_providers}.")
match provider_name:
case "openai":
return getOpenaiProviderConfig()
case "azure":
return getAzureProviderConfig()
case "palm":
return getPalmProviderConfig()
case "llama2api":
return getLlama2APIProviderConfig()
def getToolsConfig() -> dict:
supported_tools = ('serpapi')
return {
'serpapi': getSerpapiProviderConfig(),
}
# Get configurations
load_dotenv(find_dotenv())
specs_file = os.getenv('SPECS') or 'specs.md' # defaults to specs.md
output_file = os.getenv('OUTPUT_FILE') or 'output.md' # defaults to output.md
retries = int(os.getenv('MAX_RETRIES', 3) or 3) # defaults to 3
provider = os.getenv('PROVIDER') or 'openai' # defaults to openai
provider_config = getProviderConfig(provider_name=provider)
tools_config = getToolsConfig()