Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull session_id from convert API response and send to calls to report and suggest endpoints. #85

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions log10/prompt_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def __init__(self, log10_config: Log10Config = None):
self._log10_config = log10_config or Log10Config()
self._http_client = httpx.Client()

# Set by `_convert`.
self.__session_id = None

def _post_request(self, url: str, json_payload: dict) -> httpx.Response:
headers = {"x-log10-token": self._log10_config.token, "Content-Type": "application/json"}
json_payload["organization_id"] = self._log10_config.org_id
Expand All @@ -46,18 +49,31 @@ def _post_request(self, url: str, json_payload: dict) -> httpx.Response:
raise

def _convert(self, prompt: str) -> dict:
res = self._post_request(self.convert_url, {"prompt": prompt})
converted = res.json()
json_payload = {"prompt": prompt}

# Pass session ID if it has already been set.
if self.__session_id:
json_payload["session_id"] = self.__session_id

res = self._post_request(self.convert_url, json_payload)
res_json = res.json()
converted = res_json.get("output")

# The convert API returns a session ID we can use to link together a session.
if not self.__session_id:
self.__session_id = res_json.get("session_id")

return converted

def _report(self, last_prompt: dict, current_prompt: dict, suggestions: dict) -> dict:
json_payload = {
"base_prompt": json.dumps(last_prompt),
"new_prompt": json.dumps(current_prompt),
"suggestions": json.dumps(suggestions),
"session_id": self.__session_id,
}
res = self._post_request(self.report_url, json_payload)
report = res.json()
report = res.json().get("output")
return report

def _suggest(self, prompt_json: json, report: dict | None = None) -> dict:
Expand All @@ -69,9 +85,10 @@ def _suggest(self, prompt_json: json, report: dict | None = None) -> dict:
json_payload = {
"base_prompt": prompt_json,
"report": report,
"session_id": self.__session_id,
}
res = self._post_request(self.suggestions_url, json_payload)
suggestion = res.json()
suggestion = res.json().get("output")
return suggestion

def analyze(self, prompt: str) -> dict:
Expand Down