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

Fix system prompts + add 'system_message' arg to API calls #156

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
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
Binary file modified .gitignore
Binary file not shown.
38 changes: 20 additions & 18 deletions App_Function_Libraries/Article_Summarization_Lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def scrape_and_summarize_multiple(urls, custom_prompt_arg, api_name, api_key, ke
return combined_output


def scrape_and_summarize(url, custom_prompt_arg, api_name, api_key, keywords, custom_article_title):
def scrape_and_summarize(url, custom_prompt_arg, api_name, api_key, keywords, custom_article_title, system_message=None):
try:
# Step 1: Scrape the article
article_data = scrape_article(url)
Expand All @@ -156,8 +156,10 @@ def scrape_and_summarize(url, custom_prompt_arg, api_name, api_key, keywords, cu

print(f"Title: {title}, Author: {author}, Content Length: {len(content)}") # Debugging statement

# Custom system prompt for the article
system_message = system_message or "Act as a professional summarizer and summarize this article."
# Custom prompt for the article
article_custom_prompt = custom_prompt_arg or "Summarize this article."
article_custom_prompt = custom_prompt_arg or "Act as a professional summarizer and summarize this article."

# Step 2: Summarize the article
summary = None
Expand All @@ -175,64 +177,64 @@ def scrape_and_summarize(url, custom_prompt_arg, api_name, api_key, keywords, cu
try:
if api_name.lower() == 'openai':
# def summarize_with_openai(api_key, input_data, custom_prompt_arg)
summary = summarize_with_openai(api_key, json_file_path, article_custom_prompt)
summary = summarize_with_openai(api_key, json_file_path, article_custom_prompt, system_message)

elif api_name.lower() == "anthropic":
# def summarize_with_anthropic(api_key, input_data, model, custom_prompt_arg, max_retries=3, retry_delay=5):
summary = summarize_with_anthropic(api_key, json_file_path, article_custom_prompt)
summary = summarize_with_anthropic(api_key, json_file_path, article_custom_prompt, system_message)
elif api_name.lower() == "cohere":
# def summarize_with_cohere(api_key, input_data, model, custom_prompt_arg)
summary = summarize_with_cohere(api_key, json_file_path, article_custom_prompt)
summary = summarize_with_cohere(api_key, json_file_path, article_custom_prompt, system_message)

elif api_name.lower() == "groq":
logging.debug(f"MAIN: Trying to summarize with groq")
# def summarize_with_groq(api_key, input_data, model, custom_prompt_arg):
summary = summarize_with_groq(api_key, json_file_path, article_custom_prompt)
summary = summarize_with_groq(api_key, json_file_path, article_custom_prompt, system_message)

elif api_name.lower() == "openrouter":
logging.debug(f"MAIN: Trying to summarize with OpenRouter")
# def summarize_with_openrouter(api_key, input_data, custom_prompt_arg):
summary = summarize_with_openrouter(api_key, json_file_path, article_custom_prompt)
summary = summarize_with_openrouter(api_key, json_file_path, article_custom_prompt, system_message)

elif api_name.lower() == "deepseek":
logging.debug(f"MAIN: Trying to summarize with DeepSeek")
# def summarize_with_deepseek(api_key, input_data, custom_prompt_arg):
summary = summarize_with_deepseek(api_key, json_file_path, article_custom_prompt)
summary = summarize_with_deepseek(api_key, json_file_path, article_custom_prompt, system_message)

elif api_name.lower() == "mistral":
summary = summarize_with_mistral(api_key, json_file_path, article_custom_prompt)
summary = summarize_with_mistral(api_key, json_file_path, article_custom_prompt, system_message)

elif api_name.lower() == "llama.cpp":
logging.debug(f"MAIN: Trying to summarize with Llama.cpp")
# def summarize_with_llama(api_url, file_path, token, custom_prompt)
summary = summarize_with_llama(json_file_path, article_custom_prompt)
summary = summarize_with_llama(json_file_path, article_custom_prompt, system_message)

elif api_name.lower() == "kobold":
logging.debug(f"MAIN: Trying to summarize with Kobold.cpp")
# def summarize_with_kobold(input_data, kobold_api_token, custom_prompt_input, api_url):
summary = summarize_with_kobold(json_file_path, api_key, article_custom_prompt)
summary = summarize_with_kobold(json_file_path, api_key, article_custom_prompt, system_message)

elif api_name.lower() == "ooba":
# def summarize_with_oobabooga(input_data, api_key, custom_prompt, api_url):
summary = summarize_with_oobabooga(json_file_path, api_key, article_custom_prompt)
summary = summarize_with_oobabooga(json_file_path, api_key, article_custom_prompt, system_message)

elif api_name.lower() == "tabbyapi":
# def summarize_with_tabbyapi(input_data, tabby_model, custom_prompt_input, api_key=None, api_IP):
summary = summarize_with_tabbyapi(json_file_path, article_custom_prompt)
summary = summarize_with_tabbyapi(json_file_path, article_custom_prompt, system_message)

elif api_name.lower() == "vllm":
logging.debug(f"MAIN: Trying to summarize with VLLM")
# def summarize_with_vllm(api_key, input_data, custom_prompt_input):
summary = summarize_with_vllm(json_file_path, article_custom_prompt)
summary = summarize_with_vllm(json_file_path, article_custom_prompt, system_message)

elif api_name.lower() == "local-llm":
logging.debug(f"MAIN: Trying to summarize with Local LLM")
summary = summarize_with_local_llm(json_file_path, article_custom_prompt)
summary = summarize_with_local_llm(json_file_path, article_custom_prompt, system_message)

elif api_name.lower() == "huggingface":
logging.debug(f"MAIN: Trying to summarize with huggingface")
# def summarize_with_huggingface(api_key, input_data, custom_prompt_arg):
summarize_with_huggingface(api_key, json_file_path, article_custom_prompt)
summarize_with_huggingface(api_key, json_file_path, article_custom_prompt, system_message)
# Add additional API handlers here...
except requests.exceptions.ConnectionError as e:
logging.error(f"Connection error while trying to summarize with {api_name}: {str(e)}")
Expand All @@ -259,7 +261,7 @@ def scrape_and_summarize(url, custom_prompt_arg, api_name, api_key, keywords, cu
return f"Failed to process URL {url}: {str(e)}"


def ingest_unstructured_text(text, custom_prompt, api_name, api_key, keywords, custom_article_title):
def ingest_unstructured_text(text, custom_prompt, api_name, api_key, keywords, custom_article_title, system_message=None):
title = custom_article_title.strip() if custom_article_title else "Unstructured Text"
author = "Unknown"
ingestion_date = datetime.now().strftime('%Y-%m-%d')
Expand All @@ -271,7 +273,7 @@ def ingest_unstructured_text(text, custom_prompt, api_name, api_key, keywords, c
json.dump([{'text': text}], json_file, indent=2)

if api_name.lower() == 'openai':
summary = summarize_with_openai(api_key, json_file_path, custom_prompt)
summary = summarize_with_openai(api_key, json_file_path, custom_prompt, system_message)
# Add other APIs as needed
else:
summary = "Unsupported API."
Expand Down
Loading