Spaces:
Running
on
T4
Running
on
T4
oceansweep
commited on
Commit
•
0c16438
1
Parent(s):
85aa3a1
Upload LLM_API_Calls.py
Browse files
App_Function_Libraries/LLM_API_Calls.py
CHANGED
@@ -333,26 +333,29 @@ def chat_with_anthropic(api_key, input_data, model, custom_prompt_arg, max_retri
|
|
333 |
|
334 |
# Summarize with Cohere
|
335 |
def chat_with_cohere(api_key, input_data, model, custom_prompt_arg, system_prompt=None):
|
336 |
-
global cohere_api_key
|
337 |
-
cohere_api_key = api_key
|
338 |
loaded_config_data = load_and_log_configs()
|
|
|
|
|
|
|
339 |
try:
|
340 |
# API key validation
|
341 |
-
if
|
342 |
-
logging.info("
|
343 |
-
logging.info("
|
344 |
-
cohere_api_key = loaded_config_data
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
return "cohere: API Key Not Provided/Found in Config file or is empty"
|
349 |
|
350 |
-
logging.debug(f"
|
351 |
|
352 |
-
logging.debug(f"Cohere: Loaded data: {input_data}")
|
353 |
-
logging.debug(f"Cohere: Type of data: {type(input_data)}")
|
354 |
|
355 |
-
|
|
|
|
|
|
|
356 |
|
357 |
headers = {
|
358 |
'accept': 'application/json',
|
@@ -360,49 +363,65 @@ def chat_with_cohere(api_key, input_data, model, custom_prompt_arg, system_promp
|
|
360 |
'Authorization': f'Bearer {cohere_api_key}'
|
361 |
}
|
362 |
|
363 |
-
|
364 |
-
|
365 |
-
pass
|
366 |
-
else:
|
367 |
system_prompt = "You are a helpful assistant"
|
|
|
368 |
|
369 |
-
cohere_prompt =
|
370 |
-
|
371 |
-
|
372 |
-
logging.debug(f"
|
373 |
|
374 |
data = {
|
375 |
"chat_history": [
|
376 |
-
{"role": "SYSTEM", "message":
|
377 |
],
|
378 |
-
"message":
|
379 |
"model": model,
|
380 |
"connectors": [{"id": "web-search"}]
|
381 |
}
|
|
|
382 |
|
383 |
-
logging.debug("cohere: Submitting request to API endpoint")
|
384 |
-
print("cohere: Submitting request to API endpoint")
|
385 |
-
|
386 |
-
|
387 |
-
|
|
|
|
|
|
|
|
|
388 |
|
389 |
if response.status_code == 200:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
390 |
if 'text' in response_data:
|
391 |
chat_response = response_data['text'].strip()
|
392 |
-
logging.debug("
|
393 |
-
print("Chat request processed successfully.")
|
394 |
return chat_response
|
395 |
else:
|
396 |
-
logging.error("Expected
|
397 |
-
return "Expected data not found in API response."
|
398 |
else:
|
399 |
-
logging.error(f"
|
400 |
-
print(f"Failed to process
|
401 |
-
return f"
|
402 |
|
403 |
except Exception as e:
|
404 |
-
logging.error("
|
405 |
-
return f"
|
406 |
|
407 |
|
408 |
# https://console.groq.com/docs/quickstart
|
@@ -635,55 +654,65 @@ def chat_with_openrouter(api_key, input_data, custom_prompt_arg, temp=None, syst
|
|
635 |
|
636 |
|
637 |
# FIXME: This function is not yet implemented properly
|
638 |
-
def chat_with_huggingface(api_key, input_data, custom_prompt_arg, system_prompt=None):
|
639 |
loaded_config_data = load_and_log_configs()
|
640 |
-
|
641 |
-
logging.debug(f"huggingface: Summarization process starting...")
|
642 |
try:
|
643 |
# API key validation
|
644 |
-
if not api_key:
|
645 |
-
logging.info("HuggingFace: API key not provided as parameter")
|
646 |
-
logging.info("HuggingFace: Attempting to use API key from config file")
|
647 |
-
huggingface_api_key = loaded_config_data['api_keys']['openai']
|
648 |
if not api_key or api_key.strip() == "":
|
649 |
-
logging.
|
650 |
-
|
651 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
652 |
headers = {
|
653 |
-
"Authorization": f"Bearer {
|
654 |
}
|
655 |
|
656 |
# Setup model
|
657 |
huggingface_model = loaded_config_data['models']['huggingface']
|
658 |
|
659 |
-
API_URL = f"https://api-inference.huggingface.co/models/{huggingface_model}"
|
660 |
-
if
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
|
666 |
-
huggingface_prompt = f"{input_data}\n\n\n\n{custom_prompt_arg}"
|
667 |
-
logging.debug("huggingface: Prompt being sent is {huggingface_prompt}")
|
668 |
data = {
|
669 |
-
"
|
670 |
-
"
|
|
|
|
|
|
|
671 |
}
|
672 |
-
logging.debug("huggingface: Submitting request...")
|
673 |
|
|
|
674 |
response = requests.post(API_URL, headers=headers, json=data)
|
675 |
-
logging.debug(f"Full API response data: {response}")
|
|
|
676 |
if response.status_code == 200:
|
677 |
-
|
678 |
-
|
679 |
-
|
680 |
-
|
|
|
|
|
|
|
|
|
|
|
681 |
else:
|
682 |
-
logging.error(
|
683 |
-
|
|
|
684 |
except Exception as e:
|
685 |
-
logging.error("
|
686 |
-
print(f"Error occurred while processing chat request with huggingface: {str(e)}")
|
687 |
return None
|
688 |
|
689 |
|
|
|
333 |
|
334 |
# Summarize with Cohere
|
335 |
def chat_with_cohere(api_key, input_data, model, custom_prompt_arg, system_prompt=None):
|
|
|
|
|
336 |
loaded_config_data = load_and_log_configs()
|
337 |
+
if api_key is not None:
|
338 |
+
logging.debug(f"Cohere Chat: API Key from parameter: {api_key[:3]}...{api_key[-3:]}")
|
339 |
+
logging.debug(f"Cohere Chat: Cohere API Key from config: {loaded_config_data['api_keys']['cohere']}")
|
340 |
try:
|
341 |
# API key validation
|
342 |
+
if api_key is None:
|
343 |
+
logging.info("Cohere Chat: API key not provided as parameter")
|
344 |
+
logging.info("Cohere Chat: Attempting to use API key from config file")
|
345 |
+
cohere_api_key = loaded_config_data.get('api_keys', {}).get('cohere')
|
346 |
+
if not cohere_api_key:
|
347 |
+
logging.error("Cohere Chat: API key not found or is empty")
|
348 |
+
return "Cohere Chat: API Key Not Provided/Found in Config file or is empty"
|
|
|
349 |
|
350 |
+
logging.debug(f"Cohere Chat: Using API Key: {cohere_api_key[:3]}...{cohere_api_key[-3:]}")
|
351 |
|
352 |
+
logging.debug(f"Cohere Chat: Loaded data: {input_data}")
|
353 |
+
logging.debug(f"Cohere Chat: Type of data: {type(input_data)}")
|
354 |
|
355 |
+
# Ensure model is set
|
356 |
+
if not model:
|
357 |
+
model = loaded_config_data['models']['cohere']
|
358 |
+
logging.debug(f"Cohere Chat: Using model: {model}")
|
359 |
|
360 |
headers = {
|
361 |
'accept': 'application/json',
|
|
|
363 |
'Authorization': f'Bearer {cohere_api_key}'
|
364 |
}
|
365 |
|
366 |
+
# Ensure system_prompt is set
|
367 |
+
if not system_prompt:
|
|
|
|
|
368 |
system_prompt = "You are a helpful assistant"
|
369 |
+
logging.debug(f"Cohere Chat: System Prompt being sent is: '{system_prompt}'")
|
370 |
|
371 |
+
cohere_prompt = input_data
|
372 |
+
if custom_prompt_arg:
|
373 |
+
cohere_prompt += f"\n\n{custom_prompt_arg}"
|
374 |
+
logging.debug(f"Cohere Chat: User Prompt being sent is: '{cohere_prompt}'")
|
375 |
|
376 |
data = {
|
377 |
"chat_history": [
|
378 |
+
{"role": "SYSTEM", "message": system_prompt},
|
379 |
],
|
380 |
+
"message": cohere_prompt,
|
381 |
"model": model,
|
382 |
"connectors": [{"id": "web-search"}]
|
383 |
}
|
384 |
+
logging.debug(f"Cohere Chat: Request data: {json.dumps(data, indent=2)}")
|
385 |
|
386 |
+
logging.debug("cohere chat: Submitting request to API endpoint")
|
387 |
+
print("cohere chat: Submitting request to API endpoint")
|
388 |
+
|
389 |
+
try:
|
390 |
+
response = requests.post('https://api.cohere.ai/v1/chat', headers=headers, json=data)
|
391 |
+
logging.debug(f"Cohere Chat: Raw API response: {response.text}")
|
392 |
+
except requests.RequestException as e:
|
393 |
+
logging.error(f"Cohere Chat: Error making API request: {str(e)}")
|
394 |
+
return f"Cohere Chat: Error making API request: {str(e)}"
|
395 |
|
396 |
if response.status_code == 200:
|
397 |
+
try:
|
398 |
+
response_data = response.json()
|
399 |
+
except json.JSONDecodeError:
|
400 |
+
logging.error("Cohere Chat: Failed to decode JSON response")
|
401 |
+
return "Cohere Chat: Failed to decode JSON response"
|
402 |
+
|
403 |
+
if response_data is None:
|
404 |
+
logging.error("Cohere Chat: No response data received.")
|
405 |
+
return "Cohere Chat: No response data received."
|
406 |
+
|
407 |
+
logging.debug(f"cohere chat: Full API response data: {json.dumps(response_data, indent=2)}")
|
408 |
+
|
409 |
if 'text' in response_data:
|
410 |
chat_response = response_data['text'].strip()
|
411 |
+
logging.debug("Cohere Chat: Chat request successful")
|
412 |
+
print("Cohere Chat request processed successfully.")
|
413 |
return chat_response
|
414 |
else:
|
415 |
+
logging.error("Cohere Chat: Expected 'text' key not found in API response.")
|
416 |
+
return "Cohere Chat: Expected data not found in API response."
|
417 |
else:
|
418 |
+
logging.error(f"Cohere Chat: API request failed with status code {response.status_code}: {response.text}")
|
419 |
+
print(f"Cohere Chat: Failed to process chat response, status code {response.status_code}: {response.text}")
|
420 |
+
return f"Cohere Chat: API request failed: {response.text}"
|
421 |
|
422 |
except Exception as e:
|
423 |
+
logging.error(f"Cohere Chat: Error in processing: {str(e)}", exc_info=True)
|
424 |
+
return f"Cohere Chat: Error occurred while processing chat request with Cohere: {str(e)}"
|
425 |
|
426 |
|
427 |
# https://console.groq.com/docs/quickstart
|
|
|
654 |
|
655 |
|
656 |
# FIXME: This function is not yet implemented properly
|
657 |
+
def chat_with_huggingface(api_key, input_data, custom_prompt_arg, system_prompt=None, temp=None):
|
658 |
loaded_config_data = load_and_log_configs()
|
659 |
+
logging.debug(f"huggingface Chat: Chat request process starting...")
|
|
|
660 |
try:
|
661 |
# API key validation
|
|
|
|
|
|
|
|
|
662 |
if not api_key or api_key.strip() == "":
|
663 |
+
logging.info("HuggingFace Chat: API key not provided as parameter")
|
664 |
+
logging.info("HuggingFace Chat: Attempting to use API key from config file")
|
665 |
+
|
666 |
+
huggingface_api_key = loaded_config_data['api_keys'].get('huggingface')
|
667 |
+
logging.debug(f"HuggingFace Chat: API key from config: {huggingface_api_key[:5]}...{huggingface_api_key[-5:]}")
|
668 |
+
|
669 |
+
if huggingface_api_key is None or huggingface_api_key.strip() == "":
|
670 |
+
logging.error("HuggingFace Chat: API key not found or is empty")
|
671 |
+
return "HuggingFace Chat: API Key Not Provided/Found in Config file or is empty"
|
672 |
+
if huggingface_api_key:
|
673 |
+
logging.info("HuggingFace Chat: Using API key from config file")
|
674 |
headers = {
|
675 |
+
"Authorization": f"Bearer {huggingface_api_key}"
|
676 |
}
|
677 |
|
678 |
# Setup model
|
679 |
huggingface_model = loaded_config_data['models']['huggingface']
|
680 |
|
681 |
+
API_URL = f"https://api-inference.huggingface.co/models/{huggingface_model}/v1/chat/completions"
|
682 |
+
if temp is None:
|
683 |
+
temp = 1.0
|
684 |
+
temp = float(temp)
|
685 |
+
huggingface_prompt = f"{custom_prompt_arg}\n\n\n{input_data}"
|
686 |
+
logging.debug(f"HuggingFace chat: Prompt being sent is {huggingface_prompt}")
|
|
|
|
|
|
|
687 |
data = {
|
688 |
+
"model": f"{huggingface_model}",
|
689 |
+
"messages": [{"role": "user", "content": f"{huggingface_prompt}"}],
|
690 |
+
"max_tokens": 4096,
|
691 |
+
"stream": False,
|
692 |
+
"temperature": temp
|
693 |
}
|
|
|
694 |
|
695 |
+
logging.debug("HuggingFace Chat: Submitting request...")
|
696 |
response = requests.post(API_URL, headers=headers, json=data)
|
697 |
+
logging.debug(f"Full API response data: {response.text}")
|
698 |
+
|
699 |
if response.status_code == 200:
|
700 |
+
response_json = response.json()
|
701 |
+
if "choices" in response_json and len(response_json["choices"]) > 0:
|
702 |
+
generated_text = response_json["choices"][0]["message"]["content"]
|
703 |
+
logging.debug("HuggingFace Chat: Chat request successful")
|
704 |
+
print("HuggingFace Chat: Chat request successful.")
|
705 |
+
return generated_text.strip()
|
706 |
+
else:
|
707 |
+
logging.error("HuggingFace Chat: No generated text in the response")
|
708 |
+
return "HuggingFace Chat: No generated text in the response"
|
709 |
else:
|
710 |
+
logging.error(
|
711 |
+
f"HuggingFace Chat: Chat request failed with status code {response.status_code}: {response.text}")
|
712 |
+
return f"HuggingFace Chat: Failed to process chat request, status code {response.status_code}: {response.text}"
|
713 |
except Exception as e:
|
714 |
+
logging.error(f"HuggingFace Chat: Error in processing: {str(e)}")
|
715 |
+
print(f"HuggingFace Chat: Error occurred while processing chat request with huggingface: {str(e)}")
|
716 |
return None
|
717 |
|
718 |
|