Skip to content

Commit 35e5b46

Browse files
add doc string
1 parent 57a3941 commit 35e5b46

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

pyalgotrading/algobulls/api.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class AlgoBullsAPI:
2222
AlgoBulls API
2323
"""
2424
SERVER_ENDPOINT = 'http://localhost:7003/'
25+
2526
# SERVER_ENDPOINT = 'https://api.algobulls.com/'
2627

2728
def __init__(self, connection):
@@ -484,12 +485,37 @@ def get_reports(self, strategy_code: str, trading_type: TradingType, report_type
484485
return response
485486

486487
def set_genai_api_key(self, genai_api_key):
488+
"""
489+
Set GenAI Api key
490+
491+
This API is used to set GenAI API key.
492+
493+
Args:
494+
genai_api_key: GenAI api key
495+
Returns:
496+
response
497+
498+
Info: ENDPOINT
499+
`GET` v1/build/python/genai/key
500+
"""
487501
endpoint = 'v1/build/python/genai/key'
488502
json_data = {"openaiApiKey": genai_api_key}
489503
response = self._send_request(method='post', endpoint=endpoint, json_data=json_data)
490504
return response
491505

492506
def get_genai_api_key_status(self):
507+
"""
508+
Gen GenAI Api key status
509+
510+
This API is used to check if user has set GenAI API key.
511+
512+
Args:
513+
Returns:
514+
response
515+
516+
Info: ENDPOINT
517+
`GET` v1/build/python/genai/key
518+
"""
493519
endpoint = f'v1/build/python/genai/key'
494520
response = self._send_request(endpoint=endpoint)
495521
return response
@@ -547,6 +573,16 @@ def handle_genai_response_timeout(self):
547573
return response
548574

549575
def get_genai_sessions(self):
576+
"""
577+
Fetch GenAI sessions.
578+
579+
Args:
580+
Returns:
581+
GenAI sessions for the customer.
582+
583+
Info: ENDPOINT
584+
`GET` v1/build/python/genai/sessions
585+
"""
550586
endpoint = 'v1/build/python/genai/sessions'
551587
params = {'session_id': self.genai_session_id, 'pageSize': GENAI_SESSION_SIZE}
552588
response = self._send_request(endpoint=endpoint, params=params)
@@ -555,6 +591,16 @@ def get_genai_sessions(self):
555591
return response['data']
556592

557593
def get_genai_session_history(self, session_id):
594+
"""
595+
Fetch GenAI session history.
596+
597+
Args:
598+
Returns:
599+
GenAI session history for the customer.
600+
601+
Info: ENDPOINT
602+
`GET` v1/build/python/genai/session/history
603+
"""
558604
endpoint = 'v1/build/python/genai/session/history'
559605
params = {'sessionId': self.genai_sessions_map[session_id - 1]['id'], 'pageSize': GENAI_SESSION_HISTORY_SIZE}
560606
response = self._send_request(endpoint=endpoint, params=params)

pyalgotrading/algobulls/connection.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,28 @@ def set_generative_ai_keys(self, genai_api_key):
8787
8888
Args:
8989
genai_api_key: GenAI API key
90+
91+
Returns:
92+
None
9093
"""
9194
assert isinstance(genai_api_key, str), f'Argument "api_key" should be a string'
9295
self.api.set_genai_api_key(genai_api_key)
9396

9497
def get_genai_response_pooling(self, no_of_tries, user_prompt=None, chat_gpt_model=None):
98+
"""
99+
Method to get GenAI response
100+
101+
During first execution get_genai_response API is fired and for next consecutive calls handle_genai_response_timeout API is fired
102+
till we get a response other than AlgoBullsAPIGatewayTimeoutErrorException or GENAI_RESPONSE_POOLING_LIMIT is reached.
103+
104+
Args:
105+
no_of_tries: No of times this function is called recursively
106+
user_prompt: User question
107+
chat_gpt_model: OpenAI chat model name
108+
109+
Returns:
110+
GenAI response
111+
"""
95112
if no_of_tries < GENAI_RESPONSE_POOLING_LIMIT:
96113
try:
97114
if no_of_tries > 1:
@@ -107,7 +124,7 @@ def get_genai_response_pooling(self, no_of_tries, user_prompt=None, chat_gpt_mod
107124

108125
def display_genai_sessions(self):
109126
"""
110-
display previous sessions
127+
Display previous sessions
111128
Returns:
112129
available sessions
113130
"""
@@ -122,6 +139,12 @@ def display_genai_sessions(self):
122139
return customer_genai_sessions
123140

124141
def continue_from_previous_sessions(self):
142+
"""
143+
Let user select from displayed sessions
144+
145+
Returns:
146+
None
147+
"""
125148
customer_genai_sessions = self.display_genai_sessions()
126149
while True:
127150
user_input = int(input("Enter session number"))
@@ -136,6 +159,15 @@ def continue_from_previous_sessions(self):
136159
print("Please select a valid session number.")
137160

138161
def display_session_chat_history(self, session_id):
162+
"""
163+
Display Chat history for given session
164+
165+
Args:
166+
session_id: session id
167+
168+
Returns:
169+
None
170+
"""
139171
if not self.api.genai_sessions_map:
140172
self.api.get_genai_sessions()
141173

@@ -148,6 +180,26 @@ def display_session_chat_history(self, session_id):
148180
print(f"No available chat history for session id: {session_id}")
149181

150182
def start_chat(self, start_fresh=None, session_id=None, chat_gpt_model=None):
183+
"""
184+
Start chat with GenAI
185+
186+
If start_fresh is True -
187+
New session is started
188+
If start_fresh is False -
189+
If session_id is None
190+
All available sessions are displayed and user is can select from available sessions.
191+
If session_id is given
192+
Session linked with given session id is used
193+
194+
Args:
195+
start_fresh: strategy name
196+
session_id: strategy python code
197+
chat_gpt_model: OpenAI chat model name
198+
199+
Returns:
200+
None
201+
"""
202+
151203
response = self.api.get_genai_api_key_status()
152204
assert response['key_available'], f"Please set your GenAI key using set_generative_ai_keys()"
153205

@@ -187,6 +239,23 @@ def start_chat(self, start_fresh=None, session_id=None, chat_gpt_model=None):
187239
print(f"\nGenAI: {response['message']}", end=f"\n\n{'-' * 50}\n\n")
188240

189241
def save_last_generated_strategy(self, strategy_name=None, strategy_code=None):
242+
"""
243+
Method to save last generated genai response as strategy
244+
245+
User can either pass strategy code as a parameter our use last saved genai response to save strategy.
246+
247+
All strategies are unique by name, per customer.
248+
If customer tries to upload strategy with the same name as an already existing strategy
249+
- AlgoBullsAPIBadRequest Exception will be thrown. No change would be done in the backend database.
250+
251+
Args:
252+
strategy_name: strategy name
253+
strategy_code: strategy python code
254+
255+
Returns:
256+
Dictionary containing strategy name, strategy_id and cstc_id
257+
"""
258+
190259
if self.recent_genai_response or strategy_code:
191260
strategy_name = strategy_name or f'GenAI Strategy-{time.time():.0f}'
192261
strategy_details = strategy_code or self.recent_genai_response

0 commit comments

Comments
 (0)