Skip to content

Commit

Permalink
Update hugchat.py
Browse files Browse the repository at this point in the history
  • Loading branch information
manuthecoder authored Nov 7, 2024
1 parent 5ad19bd commit 1a0766c
Showing 1 changed file with 61 additions and 56 deletions.
117 changes: 61 additions & 56 deletions src/hugchat/hugchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 1a0766c

Please sign in to comment.