Skip to content

Commit

Permalink
ai work
Browse files Browse the repository at this point in the history
  • Loading branch information
ilude committed Apr 5, 2024
1 parent db89836 commit e0dca31
Show file tree
Hide file tree
Showing 7 changed files with 482 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/ansible/setup-container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
docker_compose_package_state: present

docker_users:
- "{{ ansible_env.SUDO_USER }}"
- "{{ lookup('env', 'USER') }}"
roles:
- role: setup-container
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.env
__pycache__/
app/notebook.ipynb
app/feed_cache
gmail/
*-spike/
Expand Down
Empty file added app/configs/.keep
Empty file.
1 change: 1 addition & 0 deletions app/processors/instapundit.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def process(self, widget):
widget['articles'].remove(article)
next
if self.ollama_url:
#print("{title': '" + article['original_title'] + "', 'summary': '" + article['original_summary'] + "'},")
title = self.chain.invoke({"title": article['original_title'], "summary": article['original_summary']})
title = title.strip().strip('""')
article['title'] = title
Expand Down
2 changes: 1 addition & 1 deletion app/rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self):
async def load_feed(self, widget):
start_time = time.time()

cached_widget = self.feed_cache.get(widget['name'])
cached_widget = None # self.feed_cache.get(widget['name'])

# check if feed is in self.feeds and that the last updated time is less than 15 minutes ago
if cached_widget and 'last_updated' in cached_widget and (start_time - cached_widget['last_updated']) < 60 * 15:
Expand Down
149 changes: 149 additions & 0 deletions notebooks/ai_prompt.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from langchain_community.llms import Ollama\n",
"from langchain.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate\n",
"from langchain_core.messages import SystemMessage, HumanMessage\n",
"from langchain.output_parsers import ResponseSchema, StructuredOutputParser"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"ollama_url = os.getenv('OLLAMA_URL')\n",
"model = Ollama(base_url=ollama_url, model=\"dolphin-mistral\", keep_alive=5, temperature=0.0)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"title_schema = [ResponseSchema(name=\"title\", description=\"Title for new story\")]\n",
" \n",
"output_parser = StructuredOutputParser.from_response_schemas(title_schema)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"prompt = PromptTemplate(\n",
"\ttemplate=\"Title: {title}\\nSummary: {summary}\\n{format_instructions}\\n\",\n",
"\tinput_variables=[\"title\", \"summary\"],\n",
"\tpartial_variables={\"format_instructions\": output_parser.get_format_instructions()},\n",
")\n",
"\n",
"# format chat prompt\n",
"system_prompt = SystemMessage(content=(\"\"\"\n",
"\t\tYou are and news article title editor that reviews and provides a concise and accurate title when given an existing Title and article Summary. \n",
"\t\tRemove all links from the title.\n",
"\t\tTitle should be as short as possible, aim to be less that 70 characters long.\n",
"\t\tTitle should have an absolute minimum of punctuation.\n",
"\t\tDo your best to keep the existing title if possible.\n",
"\t\t\"\"\"))\n",
"user_prompt = HumanMessagePromptTemplate(prompt=prompt)\n",
"\n",
"chat_prompt = ChatPromptTemplate.from_messages([system_prompt, user_prompt])"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"chain = chat_prompt | model"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"items = [\n",
"{'title': 'OPEN THREAD: Just do it.', 'summary': 'OPEN THREAD: Just do it.'},\n",
"{'title': 'THIS PARAGRAPH CAPTURES THE ESSENCE OF THE ATLANTIC’S SAD PIECE ON THE WORLD’S BIGGEST CRUISE SHIP:', 'summary': 'THIS PARAGRAPH CAPTURES THE ESSENCE OF THE ATLANTIC&#8217;S SAD PIECE ON THE WORLD&#8217;S BIGGEST CRUISE SHIP: &#8220;A real journalist or a more intrepid conversationalist would have gone up to the couple and asked them to explain the longevity of their marriage vis-à-vis their love of cruising. But instead I head to my mall suite, take [&#8230;]'},\n",
"{'title': 'THE NEW SPACE RACE: China launches first of a new series of Yaogan reconnaissance satellites.', 'summary': 'THE NEW SPACE RACE: China launches first of a new series of Yaogan reconnaissance satellites.'},\n",
"{'title': 'WHY IS THE BIDEN ADMINISTRATION TRYING SO HARD TO KEEP HAMAS FROM LOSING? Biden Calls for Ceasefire', 'summary': 'WHY IS THE BIDEN ADMINISTRATION TRYING SO HARD TO KEEP HAMAS FROM LOSING? Biden Calls for Ceasefire During Phone Call with Netanyahu.'},\n",
"{'title': 'BOOK RECOMMENDATION: So I just finished up The Naval Odyssey of Prof. James Brand, so far as it goe', 'summary': 'BOOK RECOMMENDATION: So I just finished up The Naval Odyssey of Prof. James Brand, so far as it goes to date, and quite enjoyed it. It&#8217;s a World War II series with plenty of action, but its central character is Prof. James Brand, a wunderkind physicist/operations analyst who zips around solving problems with everything from [&#8230;]'},\n",
"{'title': 'TUNED MASS DAMPERS FOR THE WIN: How a steel ball protected Taiwan’s tallest skyscraper in an eart', 'summary': 'TUNED MASS DAMPERS FOR THE WIN: How a steel ball protected Taiwan’s tallest skyscraper in an earthquake.'},\n",
"{'title': 'SCIENCE: Thinking You Look Younger Has A Curious Link to How You Age.', 'summary': 'SCIENCE: Thinking You Look Younger Has A Curious Link to How You Age.'},\n",
"{'title': 'ATTEMPT NO LANDINGS THERE: Jupiter’s Moon Europa Beckons As NASA’s Clipper Prepares for the Unk', 'summary': 'ATTEMPT NO LANDINGS THERE: Jupiter’s Moon Europa Beckons As NASA’s Clipper Prepares for the Unknown.'},\n",
"{'title': 'FLASHBACK: Time for Consequences: The “Cabal” who bragged about rigging the 2020 election stuck us', 'summary': 'FLASHBACK: Time for Consequences: The &#8220;Cabal&#8221; who bragged about rigging the 2020 election stuck us with an incapable president at a time of crisis. Examples need to be made.'},\n",
"{'title': 'MICROBIOME NEWS: Gut microbes may influence odds for obesity.', 'summary': 'MICROBIOME NEWS: Gut microbes may influence odds for obesity.'},\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Just Do It: Open Thread\n",
"World's Biggest Cruise Ship: Couple's Long Marriage and Love of Cruising\n",
"China Launches First Yaogan Recon Satellite in New Series\n",
"Biden Calls for Ceasefire, Biden Admin Supports Hamas\n",
"Naval Odyssey: Enjoyed WWII Series with Prof. James Brand\n",
"Taiwan's Tallest Skyscraper Protected by Tuned Mass Dampers in Earthquake\n",
"Thinking Younger Linked to Aging Process\n",
"NASA's Clipper to Explore Europa\n",
"Cabal Rigged 2020 Election, Led to Incapable President in Crisis\n",
"Gut Microbes Influence Odds For Obesity\n"
]
}
],
"source": [
"for item in items:\n",
" output = output_parser.parse(chain.invoke({\"title\": item['title'], \"summary\": item['summary']}))\n",
" print(output['title'])\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit e0dca31

Please sign in to comment.