From 1a0766cf32a4440c0c4fff4cc594cd1662c87fce Mon Sep 17 00:00:00 2001 From: Manu G Date: Wed, 6 Nov 2024 23:01:16 -0800 Subject: [PATCH] Update hugchat.py --- src/hugchat/hugchat.py | 117 +++++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 56 deletions(-) diff --git a/src/hugchat/hugchat.py b/src/hugchat/hugchat.py index 71b0e50..dc54528 100644 --- a/src/hugchat/hugchat.py +++ b/src/hugchat/hugchat.py @@ -395,6 +395,7 @@ def get_llm_from_name(self, name: str) -> Union[Model, None]: # Gives information such as name, websiteUrl, description, displayName, parameters, etc. # We can use it in the future if we need to get information about models + def get_remote_llms(self) -> list: """ Fetches all possible LLMs that could be used. Returns the LLMs in a list @@ -411,66 +412,70 @@ def get_remote_llms(self) -> list: f"Failed to get remote LLMs with status code: {r.status_code}" ) - data = r.json()["nodes"][0]["data"] - modelsIndices = data[data[0]["models"]] + import json model_list = [] - - def return_data_from_index( - index): return None if index == -1 else data[index] - - for modelIndex in modelsIndices: - model_data = data[modelIndex] - - # Model is unlisted, skip it - if data[model_data["unlisted"]]: - continue - - m = Model( - id=return_data_from_index(model_data["id"]), - name=return_data_from_index(model_data["name"]), - displayName=return_data_from_index(model_data["displayName"]), - preprompt=return_data_from_index(model_data["preprompt"]), - # promptExamples = return_data_from_index(model_data["promptExamples"]), - websiteUrl=return_data_from_index(model_data["websiteUrl"]), - description=return_data_from_index(model_data["description"]), - datasetName=return_data_from_index(model_data["datasetName"]), - datasetUrl=return_data_from_index(model_data["datasetUrl"]), - modelUrl=return_data_from_index(model_data["modelUrl"]), - # parameters = return_data_from_index(model_data["parameters"]), - ) - - prompt_list = return_data_from_index(model_data["promptExamples"]) - if prompt_list is not None: - _promptExamples = [ - return_data_from_index(index) for index in prompt_list - ] - m.promptExamples = [ - {"title": data[prompt["title"]], - "prompt": data[prompt["prompt"]]} - for prompt in _promptExamples - ] - - indices_parameters_dict = return_data_from_index( - model_data["parameters"]) - out_parameters_dict = {} - for key, value in indices_parameters_dict.items(): - if value == -1: - out_parameters_dict[key] = None - continue - - if isinstance(type(data[value]), list): - out_parameters_dict[key] = [data[index] - for index in data[value]] - continue - - out_parameters_dict[key] = data[value] - - m.parameters = out_parameters_dict - - model_list.append(m) + try: + # Splitting the response text by lines or another delimiter if appropriate + lines = r.text.splitlines() + + for line in lines: + try: + # Attempt to load each line as JSON individually + data = json.loads(line)["nodes"][0]["data"] + + # Rest of your processing with `data` as per your original logic + modelsIndices = data[data[0]["models"]] + def return_data_from_index(index): return None if index == -1 else data[index] + + for modelIndex in modelsIndices: + model_data = data[modelIndex] + if data[model_data["unlisted"]]: + continue + + m = Model( + id=return_data_from_index(model_data["id"]), + name=return_data_from_index(model_data["name"]), + displayName=return_data_from_index(model_data["displayName"]), + preprompt=return_data_from_index(model_data["preprompt"]), + websiteUrl=return_data_from_index(model_data["websiteUrl"]), + description=return_data_from_index(model_data["description"]), + datasetName=return_data_from_index(model_data["datasetName"]), + datasetUrl=return_data_from_index(model_data["datasetUrl"]), + modelUrl=return_data_from_index(model_data["modelUrl"]), + ) + + prompt_list = return_data_from_index(model_data["promptExamples"]) + if prompt_list is not None: + _promptExamples = [return_data_from_index(index) for index in prompt_list] + m.promptExamples = [ + {"title": data[prompt["title"]], "prompt": data[prompt["prompt"]]} + for prompt in _promptExamples + ] + + indices_parameters_dict = return_data_from_index(model_data["parameters"]) + out_parameters_dict = {} + for key, value in indices_parameters_dict.items(): + if value == -1: + out_parameters_dict[key] = None + continue + if isinstance(data[value], list): + out_parameters_dict[key] = [data[index] for index in data[value]] + continue + out_parameters_dict[key] = data[value] + + m.parameters = out_parameters_dict + model_list.append(m) + + except json.JSONDecodeError: + # Skip or log the error for this specific line + print(f"Skipping non-JSON or malformed line: {line}") + + except Exception as e: + print("An error occurred while parsing:", e) return model_list + def get_remote_conversations(self, replace_conversation_list=True): """ Returns all the remote conversations for the active account. Returns the conversations in a list.