From 10cf55c4c6b8f6d6448ed08c7122c9f16dab7d68 Mon Sep 17 00:00:00 2001 From: Sermet Pekin Date: Wed, 21 Aug 2024 07:40:37 +0300 Subject: [PATCH] ResultChat class was modified --- evdschat/core/chat.py | 19 ++++++++------ evdschat/core/result.py | 52 ++++++++++++++++++++++++++++++++++++++ evdschat/model/chatters.py | 14 +++++++--- evdschat/src/getter.c | 15 ++++++++--- 4 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 evdschat/core/result.py diff --git a/evdschat/core/chat.py b/evdschat/core/chat.py index d689ade..e645ddf 100644 --- a/evdschat/core/chat.py +++ b/evdschat/core/chat.py @@ -16,8 +16,9 @@ from evdschat.model.chatters import ModelAbstract, OpenAI from ..common.github_actions import PytestTesting from typing import TypeVar, Tuple, Union +from .result import ResultChat -Result = TypeVar("Result") +# Result = TypeVar("Result") Notes = TypeVar("Notes") @@ -33,7 +34,7 @@ def chat( debug=False, test=False, force=False, -) -> Union[Tuple[Result, Notes], None]: +) -> Union[Tuple[ResultChat, Notes], None]: """ Function to process the chat prompt and return the result. @@ -53,7 +54,7 @@ def chat( if isinstance(res, type(None)): raise GotNoneFromGetter() if isinstance(res, str): - return res,"" + return res, "" if isinstance(res, bool): raise ValueError( """ @@ -68,7 +69,7 @@ def chat( Please try again later or check documentation for new API URLs""" ) - if not isinstance(res , tuple ) or len(res ) != 2 : + if not isinstance(res, tuple) or len(res) != 2: raise GotUndefinedResult() result, notes = res return result, notes @@ -92,10 +93,12 @@ def chat_console(): result, notes = chat(prompt=args.prompt, debug=args.debug, test=args.test) print(result) print(notes) - def correct(x:str): - if x.endswith('.xlsx') : return x - return f'{x}.xlsx' - + + def correct(x: str): + if x.endswith(".xlsx"): + return x + return f"{x}.xlsx" + result.to_excel(f"{correct(args.file_name)}") diff --git a/evdschat/core/result.py b/evdschat/core/result.py new file mode 100644 index 0000000..80afe62 --- /dev/null +++ b/evdschat/core/result.py @@ -0,0 +1,52 @@ +from dataclasses import dataclass +import pandas as pd + +from evdspy.EVDSlocal.index_requests.get_series_indexes_exp import Result + +from enum import Enum +from typing import Any + +class Status(Enum): + success= 1 + failed= 0 + unknown= 2 + + +@dataclass +class ResultChat(Result): + status: Status = Status.unknown + reason: str = "" + + def __str__(self): + content = super().__str__() + return f""" + ! get_series_exp function returns instance of this class + + status : {self.status.name} => Indicates the status of the result + reason : {self.reason} => Provides additional context if the status is not success + data : pd.DataFrame => Contains data [same as what get_series function returns] + metadata : pd.DataFrame => Contains metadata if available + write : Callable => Creates an Excel file with data and metadata in two sheets + to_excel : Callable => Same as write to meet pandas to_excel function + + """ + content + + +def create_result(result: Any, status: Status = None, reason: str = "") -> ResultChat: + if status is None: + status = Status.unknown + + if isinstance(result, Result): + return ResultChat(data=result.data, metadata=result.metadata, write=result.write, status=status, reason=reason) + + if isinstance(result, pd.DataFrame): + r = Result(data=result, metadata=pd.DataFrame(), write=None) # Replace None with a suitable write function if available + return ResultChat(data=r.data, metadata=r.metadata, write=r.write, status=status, reason=reason) + + if result is None: + r = Result(data=pd.DataFrame(), metadata=pd.DataFrame(), write=None) # Replace None with a suitable write function if available + return ResultChat(data=r.data, metadata=r.metadata, write=r.write, status=status, reason=reason) + + + +__all__ = ["ResultChat"] diff --git a/evdschat/model/chatters.py b/evdschat/model/chatters.py index 21efdfc..983f852 100644 --- a/evdschat/model/chatters.py +++ b/evdschat/model/chatters.py @@ -27,6 +27,13 @@ import os from pathlib import Path +import pandas as pd + +from evdschat.core.result import Result, ResultChat , create_result +from evdschat.core.result import Status + + + def get_myapi_url(): from dotenv import load_dotenv @@ -101,11 +108,12 @@ def eval(self, kw: dict, permitted=None) -> Union[Tuple[Any, str], None]: del kw["notes"] try: result = self.retrieve_fnc(kw["index"], **nkw) - - return result, notes + res = create_result(result) + return res, notes except Exception: traceback.print_exc() - return None + + return create_result(None, "Eval failed") , str("") def decide_caller(self): if self.test: diff --git a/evdschat/src/getter.c b/evdschat/src/getter.c index 0c06e4d..3b3fdd7 100644 --- a/evdschat/src/getter.c +++ b/evdschat/src/getter.c @@ -47,6 +47,7 @@ static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *use return realsize; } + const char *get_proxy_for_url(const char *url, const char *proxy_url) { if (proxy_url != NULL) @@ -54,18 +55,26 @@ const char *get_proxy_for_url(const char *url, const char *proxy_url) const char *https_proxy = getenv("HTTPS_PROXY"); const char *http_proxy = getenv("HTTP_PROXY"); - - if (strncmp(url, "https", 5) == 0 && https_proxy != NULL) + + // Check for lowercase environment variables as well + if (https_proxy == NULL) + https_proxy = getenv("https_proxy"); + if (http_proxy == NULL) + http_proxy = getenv("http_proxy"); + + if (strncmp(url, "https", 5) == 0) { return https_proxy; } - else if (http_proxy != NULL) + else { return http_proxy; } + return NULL; } + char *post_request(const post_params *params) {