-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from LyzrCore/feat/summarizer-generator
Feat/summarizer and generator class
- Loading branch information
Showing
17 changed files
with
474 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from lyzr.generator.generator import Generator | ||
|
||
__all__ = ["Generator"] |
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,112 @@ | ||
import os | ||
from typing import Optional, Literal | ||
from lyzr.base.llms import LLM, get_model | ||
from lyzr.base.errors import MissingValueError | ||
|
||
|
||
class Generator: | ||
def __init__( | ||
self, | ||
api_key: Optional[str] = None, | ||
model_type: Optional[Literal["openai"]] = None, | ||
model: Optional[LLM] = None, | ||
): | ||
self.api_key = api_key or os.environ.get("OPENAI_API_KEY") | ||
if self.api_key is None: | ||
raise MissingValueError("API key") | ||
self.model = model or get_model( | ||
api_key=self.api_key, | ||
model_type=model_type or os.environ.get("MODEL_TYPE") or "openai", | ||
) # change get_model in lyzr.base.llms to accept **kwargs | ||
|
||
def generate( | ||
self, | ||
text:str, | ||
persona: Optional[str] = "Not Specified", | ||
style: Optional[str] = "Short Story" #Could be a Poem, a Children's story or even a tweet | ||
) -> str: | ||
''' | ||
Generates content in various styles such as a children's story, a poem, or even a tweet from the provided text using OpenAI's GPT-4 model. This function is designed to expand a byte sized prompt into a more elaborate format, according to the specified style. | ||
Parameters: | ||
- `text` (str): The substantial text or conversation input that needs to be elaborated or expanded. | ||
- `persona` (Optional[str], default = "Not Specified"): Specifies the persona or audience for which the content is tailored. This parameter helps in customizing the tone and style of the generated content to better resonate with the intended audience. | ||
- `style` (Optional[str], default = "Mini Essay"): Specifies the type of output desired. Options include "Mini Essay" for a detailed narrative, "Poem" for poetic content, "Children's Story" for content suitable for children, or "Tweet" for extremely concise content suitable for social media platforms like Twitter. This parameter influences the instruction set given to the AI, tailoring its approach to content generation. | ||
Return: | ||
- A string containing the generated content that effectively expands the essence of the original text into the desired format. This output aims to retain all pertinent information and key themes while presenting them in a clear, coherent, and stylistically appropriate manner. | ||
Example Usage: | ||
```python | ||
from lyzr import Generator | ||
# Initialize the content generator | ||
generator = Generator(api_key="your_openai_api_key") | ||
# Provide the text to be condensed and specify the desired style | ||
text = "Prompt or idea that you want to expand upon" | ||
story = generator.generate(text, style='story') | ||
print(story) | ||
# You can also specify the persona for which the content is tailored | ||
persona = "Tech Enthusiasts" | ||
condensed_content = generator.generate(text, persona=persona, style='Tweet') | ||
print(condensed_content) | ||
``` | ||
This functionality leverages advanced language model capabilities for creating concise and accurate representations of larger bodies of text, adjustable to various output styles and tailored to specific personas for enhanced utility in information processing and content creation scenarios. | ||
''' | ||
if self.model.model_name != "gpt-4": | ||
if self.model.model_type == "openai": | ||
self.model = get_model( | ||
api_key=self.api_key, | ||
model_type=self.model.model_type, | ||
model_name="gpt-4", | ||
) | ||
else: | ||
raise ValueError( | ||
"The text_to_notes function only works with the OpenAI's 'gpt-4' model." | ||
) | ||
|
||
# The system message acts as the prompt for the AI. | ||
system_message = f'''You are an Expert CONTENT CREATOR. Your task is to DEVELOP a VARIETY of TEXT-BASED CONTENT that could range from BLOGS to TWEETS. | ||
Persona of the content: {persona} | ||
Style of the content: {style} | ||
Here's how you can approach this task: | ||
1. IDENTIFY the target audience for whom you will be creating content. Understand their interests, needs, and preferences. | ||
2. CHOOSE the type of content you wish to create first—whether it's a blog post, tweet, article, or any other form of written communication. | ||
3. DECIDE on the topics that will RESONATE with your audience and align with your content strategy or goals. | ||
4. DRAFT an outline or key points for each piece of content to ensure STRUCTURE and FLOW. | ||
5. WRITE the initial draft focusing on ENGAGEMENT and VALUE delivery for your readers or followers. | ||
6. EMPLOY a conversational tone or formal style according to the platform and type of content you are creating. | ||
7. EDIT and REVISE your work to enhance CLARITY, GRAMMAR, and COHERENCE before publishing. | ||
You MUST maintain CONSISTENCY in quality across all platforms and types of content. | ||
Remember, I’m going to tip $300K for a BETTER SOLUTION! | ||
Now Take a Deep Breath.''' | ||
|
||
# Format the user's message that will be sent to the model. | ||
user_message = text | ||
self.model.set_messages( | ||
model_prompts=[ | ||
{"role": "system", "text": system_message}, | ||
{"role": "user", "text": user_message}, | ||
] | ||
) | ||
# Use the LLM instance to communicate with OpenAI's API. | ||
response = self.model.run() | ||
|
||
# Parse the response to extract the notes. | ||
notes = response.choices[0].message.content | ||
|
||
return notes |
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,3 @@ | ||
from lyzr.summarizer.summarizer import Summarizer | ||
|
||
__all__ = ["Summarizer"] |
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,102 @@ | ||
import os | ||
from typing import Optional, Literal | ||
from lyzr.base.llms import LLM, get_model | ||
from lyzr.base.errors import MissingValueError | ||
|
||
|
||
class Summarizer: | ||
def __init__( | ||
self, | ||
api_key: Optional[str] = None, | ||
model_type: Optional[Literal["openai"]] = None, | ||
model: Optional[LLM] = None, | ||
): | ||
self.api_key = api_key or os.environ.get("OPENAI_API_KEY") | ||
if self.api_key is None: | ||
raise MissingValueError("API key") | ||
self.model = model or get_model( | ||
api_key=self.api_key, | ||
model_type=model_type or os.environ.get("MODEL_TYPE") or "openai", | ||
) # change get_model in lyzr.base.llms to accept **kwargs | ||
|
||
def summarize( | ||
self, | ||
text:str, | ||
style: Optional[str] = "Summary" #Could be summary or notes or even a tweet | ||
) -> str: | ||
''' | ||
Generates a concise summary or notes from the provided text using a preconfigured Large Language Model (LLM), specifically targeting OpenAI's GPT-4 model. This function is designed to streamline extensive paragraphs or conversations into a more digestible format, according to the specified style. It internally configures a detailed instruction set for the AI, ensuring the output captures all critical information while omitting superfluous details. | ||
Parameters: | ||
- `text` (str): The substantial text or conversation input that needs to be summarized or condensed. | ||
- `style` (Optional[str], default = "Summary"): Specifies the type of output desired. Options include "Summary" for a straightforward summarization, "Notes" for a bullet-point or outlined form, or custom styles such as a "Tweet" for extremely concise content. This parameter influences the instruction set given to the AI, tailoring its approach to content generation. | ||
Return: | ||
- A string containing the generated summary or notes that effectively condense the essence of the original text into the desired format. This output aims to retain all pertinent information and key themes while presenting them in a clear, coherent, and logically structured manner. | ||
Example Usage: | ||
```python | ||
from lyzr import Summarizer | ||
summarizer = Summarizer(api_key="your_openai_api_key") | ||
text = "Big paragraphs or conversations that you wish to streamline or shorten" | ||
summary = summarizer.summarize(text) | ||
print(summary) | ||
# Or for a different style | ||
notes = summarizer.summarize(text, style='Notes') | ||
print(notes) | ||
``` | ||
This functionality leverages advanced language model capabilities for creating succinct and accurate representations of larger bodies of text, adjustable to various output styles for enhanced utility in information processing and content creation scenarios. | ||
''' | ||
if self.model.model_name != "gpt-4": | ||
if self.model.model_type == "openai": | ||
self.model = get_model( | ||
api_key=self.api_key, | ||
model_type=self.model.model_type, | ||
model_name="gpt-4", | ||
) | ||
else: | ||
raise ValueError( | ||
"The text_to_notes function only works with the OpenAI's 'gpt-4' model." | ||
) | ||
|
||
# The system message acts as the prompt for the AI. | ||
system_message = f'''You are an Expert SUMMARIZER with a keen ability to CAPTURE ESSENTIAL DETAILS from extensive conversations. Your task is to CREATE a CONCISE SUMMARY of the given content, ensuring that ALL CRITICAL INFORMATION is included. | ||
The style of the summary should be: {style} | ||
Here's your step-by-step guide: | ||
1. CAREFULLY READ through the entire conversation to fully understand the context and main points. | ||
2. IDENTIFY and HIGHLIGHT the KEY THEMES, decisions, questions, and any action items discussed in the conversation. | ||
3. ORGANIZE these points into a LOGICAL STRUCTURE that reflects the progression of the conversation. | ||
4. WRITE a CLEAR and COHERENT summary that seamlessly integrates all significant details without superfluous information. | ||
5. REVIEW your summary to VERIFY that it accurately represents the original conversation and includes all pertinent data. | ||
You MUST ensure that no important detail is left out from your summary. | ||
Remember, Im going to tip $300K for a BETTER SOLUTION! | ||
Now Take a Deep Breath.''' | ||
|
||
# Format the user's message that will be sent to the model. | ||
user_message = text | ||
self.model.set_messages( | ||
model_prompts=[ | ||
{"role": "system", "text": system_message}, | ||
{"role": "user", "text": user_message}, | ||
] | ||
) | ||
# Use the LLM instance to communicate with OpenAI's API. | ||
response = self.model.run() | ||
|
||
# Parse the response to extract the notes. | ||
notes = response.choices[0].message.content | ||
|
||
return notes |
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
@@ -1,24 +1,24 @@ | ||
asyncio | ||
beautifulsoup4==4.12.2 | ||
langchain==0.0.339 | ||
litellm==1.2.0 | ||
llama-index==0.9.4 | ||
llmsherpa | ||
matplotlib==3.8.2 | ||
nest_asyncio | ||
openai==1.3.4 | ||
pandas==2.0.2 | ||
litellm==1.2.0 | ||
llama-index==0.9.4 | ||
langchain==0.0.339 | ||
python-dotenv>=1.0.0 | ||
beautifulsoup4==4.12.2 | ||
pandas==2.0.2 | ||
weaviate-client==3.25.3 | ||
llmsherpa | ||
matplotlib==3.8.2 | ||
|
||
[data-analyzr] | ||
scikit-learn==1.4.0 | ||
statsmodels==0.14.1 | ||
chromadb==0.4.22 | ||
mysql-connector-python==8.2.0 | ||
openpyxl==3.1.2 | ||
tabulate==0.9.0 | ||
pmdarima==2.0.4 | ||
psycopg2-binary==2.9.9 | ||
openpyxl==3.1.2 | ||
redshift_connector==2.0.918 | ||
scikit-learn==1.4.0 | ||
mysql-connector-python==8.2.0 | ||
psycopg2-binary==2.9.9 | ||
snowflake-connector-python==3.6.0 | ||
statsmodels==0.14.1 | ||
tabulate==0.9.0 |
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,3 @@ | ||
from lyzr.generator.generator import Generator | ||
|
||
__all__ = ["Generator"] |
Oops, something went wrong.