-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: separate modules for summarization backends
- Loading branch information
1 parent
ee21e4c
commit 92c9af0
Showing
7 changed files
with
65 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import logging | ||
|
||
import cohere | ||
|
||
from app.core.utilities import COHERE_API_KEY | ||
|
||
co = cohere.Client(COHERE_API_KEY) | ||
|
||
|
||
def summarize(content: str, title: str) -> str: | ||
"""Summarize the content using Cohere's summarization API. | ||
https://docs.cohere.com/reference/summarize-2 | ||
""" | ||
|
||
logging.info(f"Summarizing '{title}' via Cohere ...") | ||
response = co.summarize( | ||
text=content, | ||
model="summarize-xlarge", | ||
temperature=0, | ||
length="auto", | ||
format="paragraph", | ||
extractiveness="auto", | ||
additional_command="in a manner suitable for reading as part of a podcast", | ||
) | ||
return response.summary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import logging | ||
|
||
from langchain import OpenAI, PromptTemplate | ||
|
||
from app.core.utilities import OPENAI_API_KEY | ||
|
||
llm = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY) | ||
|
||
|
||
def summarize(content: str, title: str) -> str: | ||
"""Summarize the content using OpenAI's language model.""" | ||
|
||
template = """ | ||
Please provide a very short, sweet, informative and engaging summary of the following news entry, in not more than two sentences. | ||
Please provide your output in a manner suitable for reading as part of a podcast. | ||
{entry} | ||
""" | ||
|
||
prompt = PromptTemplate(input_variables=["entry"], template=template) | ||
summary_prompt = prompt.format(entry=content) | ||
|
||
num_tokens = llm.get_num_tokens(summary_prompt) | ||
logging.info(f"'{title}' and its prompt has {num_tokens} tokens") | ||
|
||
return llm(summary_prompt) |