Build with E2E: Enhancing Python Code Generation with Updated Documentation Using Llama 3
Souvik Bose
Cloud Consultant specializing in NVIDIA Cloud GPU solutions at E2E Networks Limited
In this article, we will showcase how to use a hosted Llama 3 to generate code based on? API documentation provided by any platform. The method is fairly simple: use Ollama to create a Llama 3 endpoint, harness prompt engineering, and leverage the Beautiful Soup library to generate API methods from documentation pages. As an example, we will use NewsAPI, which allows you to search worldwide news with code.
The key thing to keep in mind here is - the open-source LLM will not browse, download, and parse content for you; instead, you need to create the scaffolding to get the content, clean it up, and provide it as part of the LLM prompt, in order to finally generate the documentation.?
Let’s get started.
Prerequisite: Launch a GPU Node on E2E Cloud
Since we'll be using Llama 3, we need a cloud GPU node. For this, you have two options on E2E Cloud.
First, sign up to E2E Cloud if you haven’t - https://myaccount.e2enetworks.com?
Next, launch a cloud GPU node. Pick one which has at least 16GB of GPU RAM available. If you are creating this setup for a large software dev company, you should go for H100 or A100 clusters.
To launch a cloud GPU node, click on ‘Compute’ on the left sidebar.
Guide to Building API Coding Assistant Using Llama 3
Prerequisites
pip install requests beautifulsoup4 python-dotenv ollama
The Python Script
Below is the complete Python script that can parse a documentation page, extract the? documentation content, and use Llama 3 to generate code snippets.
Explanation
import requests
from bs4 import BeautifulSoup
def scrape_webpage(url):
try:
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Create a BeautifulSoup object to parse the HTML content
soup = BeautifulSoup(response.text, 'html.parser')
# Extract the desired data from the parsed HTML
# For example, to extract all the text from the page:
text = soup.get_text()
return text
else:
print(f"Failed to retrieve the webpage. Status code: {response.status_code}")
return None
except requests.exceptions.RequestException as e:
print(f"An error occurred while making the request: {e}")
return None
import os
import requests
from bs4 import BeautifulSoup
import ollama
# Example usage:
url = "https://newsapi.org/docs/endpoints/everything" # Replace with the URL of the documentation website you want to scrape
# Extract text from the HTML content
text = scrape_webpage(url)
prompt = (
"Read the provided text (ignore the HTML code) from below and generate clear, well-documented example methods"
"that illustrate how to use the described APIs effectively in Python language only. Each example should include necessary code comments"
"and be ready to use in a real-world application. \n"
f"API_Documentation: {text} "
)
# Generate chat completion
response = ollama.chat(model='llama3', messages=[
{
'role': 'user',
'content': prompt,
},
])
print(response['message']['content'])
# Print the completion response
Running the Script
Execute the script to see the generated code snippet:
python script.py
Results
When you execute the above code, you will see that it will provide you with output on the lines of:
Output:
—-
Here are some example methods that illustrate how to use the described APIs effectively in Python language:
**Example 1: Searching for news articles using keywords**
```
import requests
# Set your API key and search query
api_key = "YOUR_API_KEY"
search_query = "bitcoin"
# Construct the URL with the API endpoint and parameters
url = f"https://newsapi.org/v2/everything?q={search_query}&apiKey={api_key}"
# Send a GET request to the API endpoint
response = requests.get(url)
# Check if the response was successful (200 OK)
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Print the total number of results
print(f"Total results: {data['totalResults']}")
# Iterate over the articles and print some information
for article in data["articles"]:
print(f"Source: {article['source']['name']}")
print(f"Title: {article['title']}")
print(f"URL: {article['url']}")
else:
print("Error:", response.status_code)
```
**Example 2: Searching for news articles using specific sources**
```
import requests
# Set your API key and search query
api_key = "YOUR_API_KEY"
source_ids = ["techcrunch.com", "thenextweb.com"]
# Construct the URL with the API endpoint and parameters
url = f"https://newsapi.org/v2/everything?domains={ ','.join(source_ids) }&apiKey={api_key}"
# Send a GET request to the API endpoint
response = requests.get(url)
# Check if the response was successful (200 OK)
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Print the total number of results
print(f"Total results: {data['totalResults']}")
# Iterate over the articles and print some information
for article in data["articles"]:
print(f"Source: {article['source']['name']}")
print(f"Title: {article['title']}")
print(f"URL: {article['url']}")
else:
print("Error:", response.status_code)
```
**Example 3: Searching for news articles using a specific date range**
```
import requests
# Set your API key and search query
api_key = "YOUR_API_KEY"
start_date = "2024-06-16T00:00:00"
end_date = "2024-06-17T23:59:59"
# Construct the URL with the API endpoint and parameters
url = f"https://newsapi.org/v2/everything?q=apple&from={start_date}&to={end_date}&sortBy=popularity&apiKey={api_key}"
# Send a GET request to the API endpoint
response = requests.get(url)
# Check if the response was successful (200 OK)
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Print the total number of results
print(f"Total results: {data['totalResults']}")
# Iterate over the articles and print some information
for article in data["articles"]:
print(f"Source: {article['source']['name']}")
print(f"Title: {article['title']}")
print(f"URL: {article['url']}")
else:
print("Error:", response.status_code)
```
These examples demonstrate how to use the News API to search for news articles based on keywords, specific sources, and date ranges. You can modify these examples to suit your specific use case and requirements.
Now, this was a simple response. Let’s see if the LLM can generate the exact function that we can use.?
领英推荐
To do this, we will modify the script.py slightly, so as to update the prompt.
# Question from the developer
question = "Show how to get all articles for a particular date"
prompt = (
"Read the Question and the provided API_Documentation text (ignore the HTML code) from below and generate clear, well-documented methods"
"that illustrate how to use the described APIs effectively in Python language only. It should include necessary code comments"
"and be ready to use in a real-world application. \n"
f"Question: {question}"
f"API_Documentation: {text} "
)
Using this, the result we get is the following:?
# Question from the developer
Here is a Python example that illustrates how to use the `newsapi` API to get all articles for a particular date:
```python
import requests
# Set your API key here
API_KEY = "YOUR_API_KEY_HERE"
def get_articles(date):
# Construct the API request URL
url = f"https://newsapi.org/v2/everything"
params = {
"apiKey": API_KEY,
"from": date, # Use the provided date format (e.g. 2024-06-17)
"to": date,
"sortBy": "publishedAt", # Sort by publication date
"pageSize": 100, # Limit the number of results per page
}
# Make the API request
response = requests.get(url, params=params)
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Extract the articles from the response
articles = data["articles"]
return articles
else:
print("Error:", response.status_code)
return None
# Example usage:
date = "2024-06-17"
articles = get_articles(date)
if articles is not None:
for article in articles:
print(article["title"])
```
This code defines a `get_articles` function that takes a date string as input, constructs the API request URL with the specified parameters (including the date), makes the request, and extracts the articles from the response. The example usage shows how to call this function with a specific date and print out the titles of the resulting articles.
Note that you should replace `YOUR_API_KEY_HERE` with your actual API key from NewsAPI.
This looks great. We have essentially built a system which can refer to a documentation endpoint and generate code based on questions you ask. And the best part is this - you control the entire stack, so no API costs, no leakage of sensitive data.?
Now, let’s move to the next step, where we integrate this with Visual Studio Code, the preferred development environment for many developers. For this, we will use the extension Continue.dev.?
Bonus Step: Integrate with VS Code
Continue.dev is an innovative AI-powered development tool designed to streamline and enhance the software development process. Continue.dev leverages LLMs to provide real-time code suggestions, error detection, and intelligent refactoring recommendations. This tool not only improves coding efficiency but also helps maintain high code quality by identifying potential issues early in the development cycle.?
To integrate this script into VSCode using the Continue.dev extension, follow these steps:
Integrating Llama 3 with Continue
Launch the Ollama server on 0.0.0.0 so that it can be accessed by external ips.
OLLAMA_HOST=0.0.0.0 ollama serve
The server will be hosted on the default port 11434. Now pull Llama 3 into your local server.
ollama pull llama3
Open Continue and click on the + sign and then open config.json.
Add the following to the model's list.?
Then we’ll write a custom slash command.?
This command will allow Continue to read the API documentation, and then generate sample methods on how to use the API. In the same config.json, add the following to the customCommands list.
"customCommands": [
{
"name": "example_api",
"prompt": "Read the provided API documentation (selected text) and generate clear, well-documented example methods that illustrate how to use the described APIs effectively. Each example should include necessary code comments and be ready to use in a real-world application.",
"description": "Write example functions from API documentation"
},
Now in the IDE, whenever you type /example_api along with the selected API documentation, the continue chat-interface will help you out with the example functions.?
Conclusion
By combining web scraping, real-time documentation extraction, and advanced language models like Llama 3, developers can significantly enhance their productivity. This approach not only ensures that the code is up-to-date but also allows for rapid prototyping and development. Try integrating this method into your workflow and experience the benefits of automated code generation.Open-source LLMs, like Llama 3 or Mistral 7, offer a powerful leverage: companies can fine-tune and create an endpoint, and use it to build AI assistants that are deeply integrated into their internal workflow. This strategy is being used to build AI assistants for developers, analysts, content creators, and others. In this regard, coding assistants are particularly powerful, as they reduce development time and increase developer productivity, thus giving the company a significant edge over competition.