-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summarizer is now oop meaning, abstract with inherits classes per mod…
…el, one for OpenAI, one for Claude and one for Perplexity.
- Loading branch information
Showing
11 changed files
with
161 additions
and
21 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from abc import ABC, abstractmethod | ||
from configparser import ConfigParser | ||
|
||
|
||
class BaseSummarizer(ABC): | ||
def __init__(self): | ||
self.config = ConfigParser() | ||
self.config.read('config.ini') | ||
|
||
@abstractmethod | ||
def summarize_text(self, text): | ||
pass |
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,41 @@ | ||
import requests | ||
|
||
from summarizers.BaseSummarizer import BaseSummarizer | ||
|
||
|
||
class ClaudeSummarizer(BaseSummarizer): | ||
def __init__(self): | ||
super().__init__() | ||
self.api_key = self.config.get('CLAUDE', 'api_key') | ||
|
||
def summarize_text(self, text): | ||
""" | ||
This function uses the Claude API to summarize provided text. | ||
:param text: the text which will be summarized | ||
:return: summarized text | ||
""" | ||
# Define the API endpoint | ||
url = "https://api.claude.ai/summarize" | ||
|
||
# Define the headers for the API request | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {self.api_key}" | ||
} | ||
|
||
# Define the body of the API request | ||
data = { | ||
"text": text | ||
} | ||
|
||
# Make the API request | ||
response = requests.post(url, headers=headers, json=data) | ||
|
||
# Check the response status code | ||
if response.status_code == 200: | ||
# If the request was successful, return the summarized text | ||
return response.json()["summary"] | ||
else: | ||
# If the request was not successful, raise an exception | ||
raise Exception(f"Claude API request failed with status code {response.status_code}") |
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,29 @@ | ||
import openai | ||
|
||
from summarizers.BaseSummarizer import BaseSummarizer | ||
|
||
|
||
class OpenAISummarizer(BaseSummarizer): | ||
def summarize_text(self, text): | ||
model = self.config.get('OPENAI', 'model') | ||
temperature = self.config.getfloat('OPENAI', 'temperature') | ||
max_tokens = self.config.getint('OPENAI', 'max_tokens') | ||
top_p = self.config.getint('OPENAI', 'top_p') | ||
|
||
response = openai.chat.completions.create( | ||
model=model, | ||
messages=[ | ||
{ | ||
"role": "system", | ||
"content": "Summarize content you are provided with for a second-grade student." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": f"Summarize the following text in one sentence:\n\n{text}" | ||
} | ||
], | ||
temperature=temperature, | ||
max_tokens=max_tokens, | ||
top_p=top_p | ||
) | ||
return response.choices[0].message.content |
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,41 @@ | ||
import requests | ||
|
||
from summarizers.BaseSummarizer import BaseSummarizer | ||
|
||
|
||
class PerplexitySummarizer(BaseSummarizer): | ||
def __init__(self): | ||
super().__init__() | ||
self.api_key = self.config.get('PERPLEXITY', 'api_key') | ||
|
||
def summarize_text(self, text): | ||
""" | ||
This function uses the Perplexity API to summarize provided text. | ||
:param text: the text which will be summarized | ||
:return: summarized text | ||
""" | ||
# Define the API endpoint | ||
url = "https://api.perplexity.ai/summarize" | ||
|
||
# Define the headers for the API request | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {self.api_key}" | ||
} | ||
|
||
# Define the body of the API request | ||
data = { | ||
"text": text | ||
} | ||
|
||
# Make the API request | ||
response = requests.post(url, headers=headers, json=data) | ||
|
||
# Check the response status code | ||
if response.status_code == 200: | ||
# If the request was successful, return the summarized text | ||
return response.json()["summary"] | ||
else: | ||
# If the request was not successful, raise an exception | ||
raise Exception(f"Perplexity API request failed with status code {response.status_code}") |
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,27 @@ | ||
# Summarizers | ||
|
||
This directory contains the classes responsible for text summarization in the project. Each class represents a different | ||
summarization API. | ||
|
||
## BaseSummarizer | ||
|
||
`BaseSummarizer` is an abstract base class that defines the common interface for all summarizers. It handles the common | ||
functionality such as reading from the `config.ini` file. Any class that inherits from `BaseSummarizer` must implement | ||
the `summarize_text` method. | ||
|
||
## [OpenAISummarizer](https://openai.com) | ||
|
||
`OpenAISummarizer` is a class that uses the OpenAI API for text summarization. It inherits from `BaseSummarizer` and | ||
implements the `summarize_text` method. | ||
|
||
## [ClaudeSummarizer](https://www.example.com) | ||
|
||
`ClaudeSummarizer` is a class that uses the Claude API for text summarization. It inherits from `BaseSummarizer` and | ||
implements the `summarize_text` method. The implementation is currently a placeholder and needs to be filled in with the | ||
actual code to call the Claude API and summarize the text. | ||
|
||
## [PerplexitySummarizer](https://www.perplexity.ai) | ||
|
||
`PerplexitySummarizer` is a class that uses the Perplexity API for text summarization. It inherits from `BaseSummarizer` and | ||
implements the `summarize_text` method. | ||
|
Empty file.