Added my contributions to community-contributions

This commit is contained in:
Elijah Rwothoromo
2025-08-05 21:22:27 +03:00
parent 9da9692a9b
commit df046d1780

View File

@@ -19,9 +19,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# imports\n", "# imports\n",
"import os, re, requests, json, ollama\n", "\n",
"from typing import List\n", "import re, requests, ollama\n",
"from dotenv import load_dotenv\n",
"from bs4 import BeautifulSoup\n", "from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display, update_display\n", "from IPython.display import Markdown, display, update_display\n",
"from openai import OpenAI" "from openai import OpenAI"
@@ -84,6 +83,12 @@
"# yield from {book.get(\"author\") for book in books if book.get(\"author\")}\n", "# yield from {book.get(\"author\") for book in books if book.get(\"author\")}\n",
"# \"\"\"\n", "# \"\"\"\n",
"\n", "\n",
"# question = \"\"\"\n",
"# Please explain what this code does and why:\n",
"# yield from {book.get(\"author\") for book in books if book.get(\"author\")}\n",
"# Popular dev site https://projecteuler.net/\n",
"# \"\"\"\n",
"\n",
"question = \"\"\"\n", "question = \"\"\"\n",
"How good at Software Development is Elijah Rwothoromo? \\\n", "How good at Software Development is Elijah Rwothoromo? \\\n",
"He has a Wordpress site https://rwothoromo.wordpress.com/. \\\n", "He has a Wordpress site https://rwothoromo.wordpress.com/. \\\n",
@@ -103,6 +108,9 @@
"\n", "\n",
"# Extract all URLs from the question string using regular expressions\n", "# Extract all URLs from the question string using regular expressions\n",
"urls = re.findall(r'https?://[^\\s)]+', question)\n", "urls = re.findall(r'https?://[^\\s)]+', question)\n",
"# print(urls)\n",
"\n",
"if len(urls) > 0:\n",
" \n", " \n",
" # Fetch the content for each URL using the Website class\n", " # Fetch the content for each URL using the Website class\n",
" scraped_content = []\n", " scraped_content = []\n",
@@ -120,7 +128,7 @@
" all_scraped_text = \"\\n\".join(scraped_content)\n", " all_scraped_text = \"\\n\".join(scraped_content)\n",
" \n", " \n",
" # Update the question with the scraped content\n", " # Update the question with the scraped content\n",
"augmented_question = f\"\"\"\n", " updated_question = f\"\"\"\n",
" Based on the following information, please answer the user's original question.\n", " Based on the following information, please answer the user's original question.\n",
" \n", " \n",
" --- TEXT FROM WEBSITES ---\n", " --- TEXT FROM WEBSITES ---\n",
@@ -130,16 +138,20 @@
" --- ORIGINAL QUESTION ---\n", " --- ORIGINAL QUESTION ---\n",
" {question}\n", " {question}\n",
" \"\"\"\n", " \"\"\"\n",
"else:\n",
" updated_question = question\n",
"\n",
"# print(updated_question)\n",
"\n", "\n",
"# system prompt to be more accurate for AI to just analyze the provided text.\n", "# system prompt to be more accurate for AI to just analyze the provided text.\n",
"system_prompt = \"You are an expert assistant. \\\n", "system_prompt = \"You are an expert assistant. \\\n",
"Analyze the user's question and the provided text from relevant websites to synthesize a comprehensive answer in markdown format.\\\n", "Analyze the user's question and the provided text from relevant websites to synthesize a comprehensive answer in markdown format.\\\n",
"Provides a short summary, ignoring text that might be navigation-related.\"\n", "Provide a short summary, ignoring text that might be navigation-related.\"\n",
"\n", "\n",
"# Create the messages list with the new augmented prompt\n", "# Create the messages list with the newly updated prompt\n",
"messages = [\n", "messages = [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n", " {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": augmented_question},\n", " {\"role\": \"user\", \"content\": updated_question},\n",
"]\n" "]\n"
] ]
}, },
@@ -153,8 +165,6 @@
"# Get gpt-4o-mini to answer, with streaming\n", "# Get gpt-4o-mini to answer, with streaming\n",
"\n", "\n",
"def get_gpt_response(question):\n", "def get_gpt_response(question):\n",
" # return response.choices[0].message.content\n",
"\n",
" stream = openai.chat.completions.create(\n", " stream = openai.chat.completions.create(\n",
" model=MODEL_GPT,\n", " model=MODEL_GPT,\n",
" messages=messages,\n", " messages=messages,\n",
@@ -179,6 +189,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# Get Llama 3.2 to answer\n", "# Get Llama 3.2 to answer\n",
"\n",
"def get_llama_response(question):\n", "def get_llama_response(question):\n",
" response = ollama.chat(\n", " response = ollama.chat(\n",
" model=MODEL_LLAMA,\n", " model=MODEL_LLAMA,\n",