Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
schorndorfer committed Nov 13, 2023
1 parent fcbc45f commit 084503e
Show file tree
Hide file tree
Showing 7 changed files with 503 additions and 367 deletions.
161 changes: 86 additions & 75 deletions _sources/augmented-generation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,24 @@
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OPENAI_API_KEY: sk-2...\n"
]
}
],
"source": [
"# set api key\n",
"with open('openai.txt', 'r') as file:\n",
" api_key = file.read().strip()\n",
"import os\n",
"from dotenv import load_dotenv # pip install python-dotenv\n",
"\n",
"# load the .env file containing your API key\n",
"load_dotenv()\n",
"\n",
"openai.api_key = api_key"
"# display (obfuscated) API key\n",
"print(f\"OPENAI_API_KEY: {os.getenv('OPENAI_API_KEY')[:4]}...\")"
]
},
{
Expand All @@ -95,18 +106,33 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Answer: \n",
"Answer: In the latest version of Python Selenium, you can find an element by class name using the `find_element_by_class_name` method. Here's an example of how to do this:\n",
"\n",
"```python\n",
"from selenium import webdriver\n",
"\n",
"# Create a new instance of the Firefox driver\n",
"driver = webdriver.Firefox()\n",
"\n",
"# Navigate to a webpage\n",
"driver.get('https://www.example.com')\n",
"\n",
"# Find an element by class name\n",
"element = driver.find_element_by_class_name('example-class')\n",
"\n",
"You can find an element by class name using the find_element_by_class_name() method in the latest version of Python Selenium. An example of this usage is as follows:\n",
"# Do something with the element\n",
"element.click()\n",
"\n",
"element = driver.find_element_by_class_name(\"class_name\")\n"
"# Close the browser\n",
"driver.quit()\n",
"```\n"
]
}
],
Expand All @@ -115,15 +141,24 @@
"prompt = \"How do I find an element by class name in the latest version of python selenium?\"\n",
"\n",
"# Generate response using GPT-3\n",
"response = openai.Completion.create(\n",
" engine=\"text-davinci-003\", # Choose the appropriate engine\n",
" prompt=prompt,\n",
" max_tokens=100, \n",
" temperature=0.7, \n",
"client = openai.OpenAI()\n",
"\n",
"response = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo-1106\",\n",
" messages=[\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": \"You are a helfpul assistant.\"\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": prompt\n",
" }\n",
" ],\n",
")\n",
"\n",
"# Display the generated text\n",
"generated_text = response[\"choices\"][0][\"text\"]\n",
"generated_text = response.choices[0].message.content\n",
"print(f\"Answer: {generated_text}\")"
]
},
Expand Down Expand Up @@ -167,24 +202,10 @@
]
},
{
"cell_type": "code",
"execution_count": 6,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Created a chunk of size 4540, which is longer than the specified 1500\n",
"Created a chunk of size 1584, which is longer than the specified 1500\n",
"Created a chunk of size 52413, which is longer than the specified 1500\n",
"Created a chunk of size 1920, which is longer than the specified 1500\n",
"Created a chunk of size 3412, which is longer than the specified 1500\n",
"Created a chunk of size 3707, which is longer than the specified 1500\n"
]
}
],
"source": [
"```python\n",
"# Fetch the content from the Selenium troubleshooting webpage\n",
"url = \"https://www.selenium.dev/documentation/webdriver/troubleshooting/upgrade_to_selenium_4/\"\n",
"response = requests.get(url)\n",
Expand All @@ -195,15 +216,16 @@
"chunks = text_splitter.split_text(webpage_content)\n",
"\n",
"# Generate vector representations\n",
"embeddings = OpenAIEmbeddings(openai_api_key=api_key)\n",
"embeddings = OpenAIEmbeddings(openai_api_key=os.getenv(\"OPENAI_API_KEY\"))\n",
"metadata = [{\"source\": url} for _ in range(len(chunks))] # Metadata for each chunk\n",
"\n",
"# Create a FAISS vector store and save it\n",
"store = FAISS.from_texts(chunks, embeddings, metadatas=metadata)\n",
"faiss.write_index(store.index, \"selenium_docs.index\")\n",
"store.index = None\n",
"with open(\"selenium_docs.pkl\", \"wb\") as f:\n",
" pickle.dump(store, f)\n"
"with open(\"./selenium_docs.pkl\", \"wb\") as f:\n",
" pickle.dump(store, f)\n",
"```\n"
]
},
{
Expand All @@ -214,25 +236,15 @@
]
},
{
"cell_type": "code",
"execution_count": 10,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Answer: In the latest version of python selenium, you can find an element by class name using the following syntax: driver.findElement(By.className(\"className\")) or driver.findElement(By.cssSelector(\".className\")).\n",
"\n"
]
}
],
"source": [
"```python\n",
"# Load the FAISS index\n",
"index = faiss.read_index(\"selenium_docs.index\") # Assuming the name of the index file is 'selenium_docs.index'\n",
"\n",
"# Load the vector store\n",
"with open(\"selenium_docs.pkl\", \"rb\") as f:\n",
"with open(\"./selenium_docs.pkl\", \"rb\") as f:\n",
" store = pickle.load(f)\n",
"\n",
"# Merge the index and store\n",
Expand All @@ -246,7 +258,8 @@
"result = chain({\"question\": question})\n",
"\n",
"# Print the answer.\n",
"print(f\"Answer: {result['answer']}\")\n"
"print(f\"Answer: {result['answer']}\")\n",
"```\n"
]
},
{
Expand All @@ -259,21 +272,10 @@
]
},
{
"cell_type": "code",
"execution_count": 14,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Answer: \n",
"\n",
"The first elephant to land on the moon was a female elephant named Ellie. She was born in captivity in Africa and was brought to the United States when she was two years old. Ellie spent the majority of her life performing in circuses and zoos. In 1962, she was sent to the National Zoo in Washington, D.C. where she lived for the rest of her life. Ellie died in 1988 at the age of 36.\n"
]
}
],
"source": [
"```python\n",
"prompt = \"Can you tell me more about the first elephant that landed on the moon?\"\n",
"\n",
"# Generate response using GPT-3\n",
Expand All @@ -286,7 +288,8 @@
"\n",
"# Display the generated text\n",
"generated_text = response[\"choices\"][0][\"text\"]\n",
"print(f\"Answer: {generated_text}\")\n"
"print(f\"Answer: {generated_text}\")\n",
"```"
]
},
{
Expand All @@ -296,6 +299,15 @@
"Just note that the newest version of the GPT model will not produce this same hallucination. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer: \n",
"\n",
"The first elephant to land on the moon was a female elephant named Ellie. She was born in captivity in Africa and was brought to the United States when she was two years old. Ellie spent the majority of her life performing in circuses and zoos. In 1962, she was sent to the National Zoo in Washington, D.C. where she lived for the rest of her life. Ellie died in 1988 at the age of 36."
]
},
{
"cell_type": "code",
"execution_count": 78,
Expand Down Expand Up @@ -363,19 +375,10 @@
]
},
{
"cell_type": "code",
"execution_count": 76,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Answer: I don't know.\n"
]
}
],
"source": [
"```python\n",
"# Load the FAISS index\n",
"index = faiss.read_index(\"elephant_docs.index\") \n",
"\n",
Expand All @@ -394,7 +397,15 @@
"result = chain({\"question\": question})\n",
"\n",
"# Print the answer.\n",
"print(f\"Answer: {result['answer']}\")\n"
"print(f\"Answer: {result['answer']}\")\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer: I don't know.\n"
]
}
],
Expand All @@ -414,7 +425,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 084503e

Please sign in to comment.