Skip to content

Commit

Permalink
add doc string
Browse files Browse the repository at this point in the history
  • Loading branch information
om-khade-algobulls committed Aug 19, 2023
1 parent 57a3941 commit 35e5b46
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
46 changes: 46 additions & 0 deletions pyalgotrading/algobulls/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AlgoBullsAPI:
AlgoBulls API
"""
SERVER_ENDPOINT = 'http://localhost:7003/'

# SERVER_ENDPOINT = 'https://api.algobulls.com/'

def __init__(self, connection):
Expand Down Expand Up @@ -484,12 +485,37 @@ def get_reports(self, strategy_code: str, trading_type: TradingType, report_type
return response

def set_genai_api_key(self, genai_api_key):
"""
Set GenAI Api key
This API is used to set GenAI API key.
Args:
genai_api_key: GenAI api key
Returns:
response
Info: ENDPOINT
`GET` v1/build/python/genai/key
"""
endpoint = 'v1/build/python/genai/key'
json_data = {"openaiApiKey": genai_api_key}
response = self._send_request(method='post', endpoint=endpoint, json_data=json_data)
return response

def get_genai_api_key_status(self):
"""
Gen GenAI Api key status
This API is used to check if user has set GenAI API key.
Args:
Returns:
response
Info: ENDPOINT
`GET` v1/build/python/genai/key
"""
endpoint = f'v1/build/python/genai/key'
response = self._send_request(endpoint=endpoint)
return response
Expand Down Expand Up @@ -547,6 +573,16 @@ def handle_genai_response_timeout(self):
return response

def get_genai_sessions(self):
"""
Fetch GenAI sessions.
Args:
Returns:
GenAI sessions for the customer.
Info: ENDPOINT
`GET` v1/build/python/genai/sessions
"""
endpoint = 'v1/build/python/genai/sessions'
params = {'session_id': self.genai_session_id, 'pageSize': GENAI_SESSION_SIZE}
response = self._send_request(endpoint=endpoint, params=params)
Expand All @@ -555,6 +591,16 @@ def get_genai_sessions(self):
return response['data']

def get_genai_session_history(self, session_id):
"""
Fetch GenAI session history.
Args:
Returns:
GenAI session history for the customer.
Info: ENDPOINT
`GET` v1/build/python/genai/session/history
"""
endpoint = 'v1/build/python/genai/session/history'
params = {'sessionId': self.genai_sessions_map[session_id - 1]['id'], 'pageSize': GENAI_SESSION_HISTORY_SIZE}
response = self._send_request(endpoint=endpoint, params=params)
Expand Down
71 changes: 70 additions & 1 deletion pyalgotrading/algobulls/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,28 @@ def set_generative_ai_keys(self, genai_api_key):
Args:
genai_api_key: GenAI API key
Returns:
None
"""
assert isinstance(genai_api_key, str), f'Argument "api_key" should be a string'
self.api.set_genai_api_key(genai_api_key)

def get_genai_response_pooling(self, no_of_tries, user_prompt=None, chat_gpt_model=None):
"""
Method to get GenAI response
During first execution get_genai_response API is fired and for next consecutive calls handle_genai_response_timeout API is fired
till we get a response other than AlgoBullsAPIGatewayTimeoutErrorException or GENAI_RESPONSE_POOLING_LIMIT is reached.
Args:
no_of_tries: No of times this function is called recursively
user_prompt: User question
chat_gpt_model: OpenAI chat model name
Returns:
GenAI response
"""
if no_of_tries < GENAI_RESPONSE_POOLING_LIMIT:
try:
if no_of_tries > 1:
Expand All @@ -107,7 +124,7 @@ def get_genai_response_pooling(self, no_of_tries, user_prompt=None, chat_gpt_mod

def display_genai_sessions(self):
"""
display previous sessions
Display previous sessions
Returns:
available sessions
"""
Expand All @@ -122,6 +139,12 @@ def display_genai_sessions(self):
return customer_genai_sessions

def continue_from_previous_sessions(self):
"""
Let user select from displayed sessions
Returns:
None
"""
customer_genai_sessions = self.display_genai_sessions()
while True:
user_input = int(input("Enter session number"))
Expand All @@ -136,6 +159,15 @@ def continue_from_previous_sessions(self):
print("Please select a valid session number.")

def display_session_chat_history(self, session_id):
"""
Display Chat history for given session
Args:
session_id: session id
Returns:
None
"""
if not self.api.genai_sessions_map:
self.api.get_genai_sessions()

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

def start_chat(self, start_fresh=None, session_id=None, chat_gpt_model=None):
"""
Start chat with GenAI
If start_fresh is True -
New session is started
If start_fresh is False -
If session_id is None
All available sessions are displayed and user is can select from available sessions.
If session_id is given
Session linked with given session id is used
Args:
start_fresh: strategy name
session_id: strategy python code
chat_gpt_model: OpenAI chat model name
Returns:
None
"""

response = self.api.get_genai_api_key_status()
assert response['key_available'], f"Please set your GenAI key using set_generative_ai_keys()"

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

def save_last_generated_strategy(self, strategy_name=None, strategy_code=None):
"""
Method to save last generated genai response as strategy
User can either pass strategy code as a parameter our use last saved genai response to save strategy.
All strategies are unique by name, per customer.
If customer tries to upload strategy with the same name as an already existing strategy
- AlgoBullsAPIBadRequest Exception will be thrown. No change would be done in the backend database.
Args:
strategy_name: strategy name
strategy_code: strategy python code
Returns:
Dictionary containing strategy name, strategy_id and cstc_id
"""

if self.recent_genai_response or strategy_code:
strategy_name = strategy_name or f'GenAI Strategy-{time.time():.0f}'
strategy_details = strategy_code or self.recent_genai_response
Expand Down

0 comments on commit 35e5b46

Please sign in to comment.