authors are vetted experts in their fields and write on topics in which they have demonstrated experience. All of our content is peer reviewed and validated by Toptal experts in the same field.
Tarek Mohamed Mehrez
Verified Expert in Engineering
10 Years of Experience

Tarek是一名专攻机器学习的软件和数据工程师, software architecture, DevOps, and back-end development. He has served as CTO of Thndr, a company backed by Y Combinator, and has published several research papers on machine learning. Tarek has a master’s degree in computational linguistics from the University of Stuttgart.

Previous Role

CTO

PREVIOUSLY AT

KlarnaMiroING
Share

In recent years, 人工智能已经主导了技术领域,并对几乎所有行业产生了变革性影响, from the creative arts to finance to management. Large language models (LLMs) such as OpenAI’s GPT and Google’s Gemini 正在以惊人的速度改进,并开始在软件工程师的工具包中发挥重要作用.

尽管当前这一代法学硕士无法取代软件工程师, 这些模型可以作为智能数字助手,帮助编写代码和调试一些简单的日常任务. In this article, 我利用我开发人工智能和机器学习解决方案的经验来解释使用llm生成能够与外部资源交互的代码的复杂性.

Defining Large Language Models

LLM是一种机器学习模型,它经过了大量文本数据的训练,目标是理解和生成人类语言. An LLM is typically built using transformers, 一种基于“自我关注机制”的神经网络架构,” meaning that entire input sequences are processed simultaneously rather than word by word. This allows the model to analyze entire sentences, 显著提高其对潜在语义的理解,即文本传达的潜在意义和意图. Essentially, LLMs understand context, making them effective in generating text in a humanlike style.

网络越深,它就越能捕捉到人类语言中的微妙含义. 现代LLM需要大量的训练数据,并且可能包含数十亿个参数(从训练数据中学习的元素),因为人们希望增加深度可以提高推理等任务的性能. For training GPT-3, the raw data scraped from the content in published books and the Internet was 45TB of compressed text. GPT-3包含大约1750亿个参数来实现其知识库.

Alongside GPT-3 and GPT-4, several other LLMs have made considerable advancements; these include Google’s PaLM 2 and LLaMa 2 from Meta.

Because their training data has included programming languages and software development, LLMs have learned to generate code as well. 现代法学硕士能够将自然语言文本提示转换为各种编程语言和技术堆栈中的工作代码, though leveraging this powerful capability requires a certain level of technical expertise.

The Benefits and Limitations of LLM Code Generation

While complex tasks and problem-solving will most likely always require the attention of human developers, LLMs can act as intelligent assistants, writing code for less complicated tasks. 将重复的任务交给LLM可以提高生产力并减少设计过程中的开发时间, 特别是对于早期阶段的任务,如原型和概念验证. Additionally, LLM可以通过解释代码和查找语法错误来为调试过程提供有价值的见解,这些语法错误在编写了一整天的代码后可能很难被发现.

That said, 法学硕士生成的任何代码都应该被视为一个起点,而不是一个成品——代码应该经常被审查和彻底测试. Developers should also be aware of the limitations of LLMs. 因为它们缺乏人类解决问题和即兴发挥的能力, LLMs struggle with complex business logic and challenges that require innovative solutions. Additionally, 法学硕士可能没有接受过适当的培训来处理特定领域的项目或使用专门的或专有的框架. Overall, LLMs can be effective assistants, 但人类开发人员仍然是开发过程的重要组成部分.

Generating Code With an LLM: A Weather API Call Example

One critical component of most modern applications is the requirement to interface with external resources. Developers are frequently required to call an API (application programming interface), 一组指令或协议,为应用程序之间的通信提供了一种标准化的方式. Writing the code to consume an API often involves a deep dive into its documentation, which details all of the requirements, including the naming conventions, the operations it supports, the data format of the input and response, and security requirements.

As long as it is given the required information, an LLM may be able to speed the process up, by helping to generate the code for an API request. 此示例演示了如何提示ChatGPT生成对 OpenWeather API 在Python中,以及如何避免错误,例如使用已弃用的函数. 然后,我们的示例展示了如何通过OpenAI的API发送相同的提示, demonstrating how changing parameters can affect an LLM’s output.

A prompt is sent to an LLM API, which generates code comprising a request to another external API. 该代码集成到生产代码中,并用于调用外部API.

请注意,使用OpenWeather API是有成本的, though the API can be called for free 1,000 times per day.

Turning ChatGPT Prompts Into API Calls

我们的示例应用程序的需求包括显示当前温度和天气描述. 为了了解ChatGPT如何帮助我们编写这段代码,我们提示如下:

编写一个Python脚本,使用Python“请求”库在端点“http://api”上向OpenWeather API执行GET请求.openweathermap.org/data/2.5/weather”. The API call should include an API key in the query string parameters under the key “appid.” The API should return data in JSON format. The function should return the current temperature in Fahrenheit.

The following Python code is returned:

import requests

# Replace "YOUR_API_KEY" with your actual OpenWeather API key.
api_key = "YOUR_API_KEY"
api_url = "http://api.openweathermap.org/data/2.5/weather"

# City for which you want to get weather information.
city_name = "London"

# Parameters for the API request.
params = {
    'q': city_name,
    'appid': api_key,
    “单位”:“英制”#用“公制”表示摄氏度,用“英制”表示华氏度.
}

try:
    # Make the GET request.
    response = requests.get(api_url, params=params)

    # Check if the request was successful (status code 200).
    if response.status_code == 200:
        # Parse and print the JSON response.
        weather_data = response.json()
        print("Weather Data for", city_name)
        print("Temperature:", weather_data['main']['temp'],"°F")
        print("Description:", weather_data['weather'][0]["description"])
    else:
        # Print an error message if the request was not successful.
        print("Error:", response.status_code, response.text)

except Exception as e:
    print("An error occurred:", e)

Note that in the prompt, the actual location was not specified; however, the generated code includes a variable named city_name, which has a default value of “London.” Don’t forget to replace YOUR_API_KEY with your personal OpenWeather API key.

To test the code, the Python interpreter must be installed on your machine, after which the code can be run using an IDE such as Visual Studio Code, or by invoking the script via the command line. The following weather data is output for the city of London:

Weather Data for London
Temperature: 53.08°F       
Description: overcast clouds

In many systems, the information that is retrieved from an API might need to be stored or input into another system, such as a database or even another API. In these cases, the information would need to be converted to a specific format, such as XML or CSV, or even incorporated in a SQL command that inserts or updates data in a database table. LLM可以通过生成自定义输出格式的代码来提供帮助. This sentence is added to the original prompt:

输出应该以SQL命令的形式返回,该命令将返回的数据插入到名为“current_weather”的表中。, where the columns are “city,” “temperature,” “weather_description,” and “update_date.”

ChatGPT returns the same code as above, with a few added import statements and an updated output format:

import requests
import json
from datetime import datetime

# …

try:
    # Make the GET request.
    response = requests.get(api_url, params=params)

    # Check if the request was successful (status code 200).
    if response.status_code == 200:
        # Parse and print the JSON response.
        weather_data = response.json()

        # Extract relevant information.
        city = city_name
        temperature = weather_data['main']['temp']
        weather_description = weather_data['weather'][0]['description']
        update_date = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')

        # Generate SQL command for insertion.
        sql_command = f"INSERT INTO current_weather (city, temperature, weather_description, update_date) VALUES ('{city}', {temperature}, '{weather_description}', '{update_date}')"
            
        # Print the SQL command.
        print(sql_command)
    else:
        # Print an error message if the request was not successful.
        print("Error:", response.status_code, response.text)

except Exception as e:
    print("An error occurred:", e)

Running this code results in the correct SQL command:

INSERT INTO current_weather (city, temperature, weather_description, update_date) VALUES ('London', 53.37, 'broken clouds', '2024-02-06 04:43:35')

However, a warning is returned due to the utcnow() function being deprecated:

DeprecationWarning: datetime.datetime.utknow()已弃用,并计划在将来的版本中删除. 使用时区感知对象来表示UTC: datetime中的日期时间.datetime.now(datetime.UTC).

为了防止ChatGPT使用已弃用的函数,我们在提示符中添加:

Please do not use any functions that are deprecated.

After adding this line, ChatGPT replaces the deprecated utcnow() function with the following:

# Use timezone-aware object for update_date.
update_date = datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S')

This code once again returns the correct SQL command. SQL命令可以使用各种ide(如Visual Studio Code)或数据库管理工具中的查询编辑器进行测试. In a typical web application, the SQL command would be run immediately after the API call, updating a table in the database in real time.

As long as they are given accurate guidance, 法学硕士能够将他们的输出结构成几乎任何格式, including SQL commands, JSON, or even a call to another API.

Using the OpenAI API Instead of ChatGPT

许多llm都有相应的API,使开发人员能够以编程方式与llm进行交互,并将其无缝集成到应用程序中. This enables you to create your own virtual AI assistant, with features such as code generation for autocompletion, refactoring, and optimization. User interfaces can be optimized for specific domains and customized to populate predefined prompt templates. 以编程方式集成LLM还允许安排或触发任务, facilitating the creation of an automated virtual assistant.

In this example, we’ll perform the same weather retrieval task, now using Python to interact with the OpenAI API instead of manually prompting ChatGPT for code. The API can be installed using the following command:

pip install openai

To use the OpenAI API, an API key is required. The API has a cost associated with it, however, new accounts have free access for a limited time frame.

安装OpenAI API后,可以将其库导入到代码中. The following code sends the prompt that was created above, omitting the instruction to output a SQL command. The model that is specified is GPT-3.5:

import openai

openai.api_key = "YOUR_API_KEY"  # Replace with your actual API key.

response = openai.chat.completions.create(
  model='gpt-3.5-turbo',
  max_tokens=1024, temperature=0.3, top_p=0.9,
  messages=[
        {'role': 'system', 'content': '你是一个智能人工智能助手.'},
        {'role': 'user', 'content': "编写一个Python脚本,使用Python 'requests'库在端点'http://api上向OpenWeather API执行GET请求.openweathermap.org/data/2.5/weather'. The API call should include an API key in the query string parameters under the key 'appid.' The data returned by the API is in JSON format. Please do not use any functions that are deprecated."},
    ],
)

response_message = response.choices[0].message.content
print(response_message )

The LLM generates the following Python code, which retrieves the temperature for London:

import requests

# API endpoint and API key.
url = "http://api.openweathermap.org/data/2.5/weather"
api_key = "YOUR_API_KEY"

# Query string parameters.
params = {
    'q': 'London,uk',  # Example city and country code.
    'appid': api_key
}

# Send GET request.
response = requests.get(url, params=params)

# Check if request was successful.
if response.status_code == 200:
    # Parse JSON response.
    data = response.json()

    # Print the temperature in Celsius.
    temperature = data['main']['temp'] - 273.15
    print(f"The temperature in London is {temperature:.2f}°C.")
else:
    print(f"Error: {response.status_code}")

Note that the instruction to retrieve the temperature in degrees Fahrenheit was also omitted. The LLM did not specify the units in the API call, but it chose to mathematically convert the units from Kelvins to Celsius when displaying the results.

Leveraging LLM-specific Parameters

When using the API, many of the LLM’s parameters can be adjusted, altering the responses that are generated. Some parameters change the level of randomness and creativity, while others focus on repetition. While parameters may have more of an influence when generating natural language text, adjusting them can also influence code generation.

在前面的代码中,可以在第7行调整GPT的参数:

max_tokens=1024, temperature=0.3, top_p=0.9,

The following parameters can be adjusted:

Parameter
Description
Code Generation Impact
temperature
温度参数调整生成文本的随机性, essentially the “creativity” of the response. A higher temperature increases randomness, while a lower temperature results in more predictable responses. The temperature can be set between 0 and 2. The default is either 0.7 or 1, depending on the model.
A lower temperature will produce safer code that follows the patterns and structures learned during training. 更高的温度可能会产生更多独特和非常规的代码, however, they may also introduce errors and inconsistencies.
max_tokens
The max_tokens parameter sets a limit on how many tokens the LLM will generate. If it is set too low, the response may only be a few words. Setting it too high may waste tokens, increasing costs.
Max tokens should be set high enough to include all the code that needs to be generated. 如果你不想从法学硕士那里得到任何解释,它可以减少.
top_p
Top P, or nucleus sampling, influences what the next word or phrase might be by limiting the choices that the LLM considers. top_p has a maximum value of 1 and a minimum value of 0. Setting top_p to 0.1告诉LLM将下一个令牌限制为最可能的前10%. Setting it to 0.5将其改变为前50%,产生更大范围的反应.
With a low top P value, 生成的代码将更具可预测性和上下文相关性, as only the most probable tokens will be used. Though raising top P allows more diversity in the output, it can lead to irrelevant or nonsensical code snippets.
frequency_penalty
The frequency_penalty 参数减少了LLM响应中单词或短语的重复. With a high frequency penalty, the LLM avoids repeating words that were used earlier. A lower frequency penalty allows more repetition. The frequency_penalty parameter has a maximum value of 2 and a minimum value of 0.
With a higher frequency penalty, the generated code will be less repetitive and potentially more innovative; however, 法学硕士可能会选择效率较低甚至不正确的元素. 使用较低的频率惩罚,代码可能无法探索替代方法. Experimentation might be needed to find the optimal value.
presence_penalty
The presence_penalty parameter is related to the frequency_penalty parameter in that they both encourage a more diverse word range. However, while frequency_penalty penalizes tokens that have appeared many times in the text, presence_penalty 惩罚一个已经出现的标记,无论其出现的频率如何. The net effect is that frequency_penalty tries to reduce repetition of words, while presence_penalty focuses on using entirely new words. The presence_penalty parameter has a maximum value of 2 and a minimum value of 0.
Similar to frequency penalty, a high presence penalty encourages the LLM to explore alternative solutions; however, the generated code may be less efficient or even incorrect. A lower presence penalty allows more code to be repeated, which can result in more concise and efficient code, especially when there are repetitive patterns.
stop
A stop sequence tells the LLM when to stop generating text. For example, setting stop to “/n” tells the LLM to stop after a single line.
停止序列防止LLM生成多余或不相关的代码. 停止序列应该是代码段的自然结束点, for example, the end of a function or a loop.

To see how these parameters affect code generation in action, we’ll experiment with adjusting the frequency_penalty and presence_penalty parameters.

When frequency_penalty was set to 1.5 (a higher value), the LLM generated additional code to catch exceptions, presumably in an effort to avoid repeating previous outputs:

#...

try:
    #发送带有参数的GET请求,并以JSON格式获取响应数据.
    response = requests.get(url, params=params)

    if response.status_code == 200:
        weather_data = response.json()

        # Print out some relevant information from the weather data.
        print("City:", weather_data['name'])
        print("Temperature:", weather_data['main']['temp'], "K")

except requests.exceptions.RequestException as e:
   # Handle any error that occurred during the HTTP request.
   print("Error:", e)

While the additional code is highly beneficial, it is important to note that results are unpredictable. 错误处理功能可防止程序在外部API出现问题时超时或崩溃, 而是因为我们没有要求LLM生成处理异常的代码, its addition was more akin to a lucky guess. Running the API call with identical parameters a second time would most likely yield a different result. 告诉LLM生成错误处理代码的唯一一致方法是将这些特定指令添加到初始提示符中.

Setting presence_penalty to 2.0 (the highest value) had a similar effect. LLM避免重复之前的输出,而是将对OpenWeather API的调用放在一个函数中, passing the API key as an argument:

import requests

def get_weather(api_key):
    url = "http://api.openweathermap.org/data/2.5/weather"
    params = {
        'q': 'London,uk',  # Example city and country code.
        'appid': api_key
    }


    if response.status_code == 200:
        data = response.json()
        return data
    else:
        print("Error:", response.status_code)

#将“YOUR_API_KEY”替换为OpenWeather中的实际API密钥.
api_key = "YOUR_API_KEY"

weather_data = get_weather(api_key)
print(weather_data)

虽然将API调用放在函数中是一个有用的调整, passing the API key as an argument is curious. In most use cases, the argument that should be passed is city. 通过调整初始提示,可以很容易地纠正这种异常.

Adjusting an LLM’s parameters will require experimentation, as the results are generally unpredictable, 而调整某些参数往往似乎没有效果. 更改参数通常不会对是否遵循最佳实践产生一致的影响. In general, 允许更多可预测的结果和重复的设置将产生更简洁和高效的代码. On the other hand, 促进创造力和惩罚重复的设置可能会产生更具创造性和创新性的解决方案, 尽管出现不正常或不相关代码的可能性更高.

Integrating LLMs Into Codebases

Once the LLM-generated code is working and optimized, the next step in the development process is integrating it into an existing codebase. Directly pasting the code into existing code might lead to unorganized formatting and maintenance issues. A more streamlined approach is to convert the code into reusable functions or objects. This can be done with the LLM as long as detailed instructions are added to the prompt. Arguments should be specified, as well as output format.

To have the LLM create a Weather object that contains the get_weather function, the following instruction can be added to the prompt:

The API call should be placed in an object called “weather,” which contains a function called “get_weather,” which takes in an argument called “city.”

基于此提示,LLM生成以下Python代码:

import requests

class Weather:
    def __init__(self, api_key):
        self.api_key = api_key
        self.temperature = None

    def get_weather(self, city):
        url = 'http://api.openweathermap.org/data/2.5/weather'
        params = {
            'q': city,
            'appid': self.api_key,
            'units': 'imperial'  # Request temperature in Fahrenheit.
        }

        response = requests.get(url, params=params)
        data = response.json()

        if response.status_code == 200:
            self.temperature = data['main']['temp']
            print(f"The temperature in {city} is {self.temperature}°F.")
        else:
            print(f"Error: {data['message']}")

#将“YOUR_API_KEY”替换为OpenWeather中的实际API密钥.
api_key = "YOUR_API_KEY"

weather = Weather(api_key)
weather.get_weather('London')

The LLM generates a working function in a Weather object. The object can now be integrated into another application, however, 应该结合错误处理,以确保应用程序不会崩溃或无意中显示敏感信息.

Performing Error Handling and Testing

In the real world, API calls can fail for a number of reasons. 为了确保可靠性,代码必须包含适当的错误处理. In many of the previous examples, llm生成的代码包含检查返回的HTTP状态码是否为200(成功响应),并显示任何其他响应的错误描述. 但是这种必要的检查并没有考虑到常见的错误场景,例如连接超时或DNS解析错误.

For reliable operation, the code should contain error handling mechanisms, such as using a try-catch block when making the API call. The following text was appended to the previous prompt:

对于错误处理,在进行API调用时合并一个try-catch块. 代码还应该确认HTTP响应状态为200,并在不是200时显示错误描述.

This prompt generated Python code containing a try-catch block to handle any potential errors:

class Weather:
    def __init__(self, api_key):
        self.api_key = api_key
        self.temperature = None

    def get_weather(self, city):
        try:
            # Construct the API URL with the provided city and API key.
            url = "http://api.openweathermap.org/data/2.5/weather"
            params = {'q': city, 
                      'appid': self.api_key,
                      'units': 'imperial'  # Get temperature in Fahrenheit.
                      }

            # Send GET request to the API.
            response = requests.get(url, params=params)

            # Check if the response status is 200 (OK).
            if response.status_code == 200:
                # Parse the JSON response
                data = response.json()

                # Extract the temperature in Fahrenheit from the response.
                self.temperature = data['main']['temp']
            else:
                #如果响应状态不为200,则显示错误描述.
                print(f'Error: {response.status_code} - {response.json()["message"]}')
        except requests.exceptions.RequestException as e:
            # Handle any request exceptions.
            print(f'Error: {e}')

而由LLM生成的代码提供了一个很好的起点, further modifications are typically required for additional functionality or customization. 例如,应该记录错误,并且配置错误(例如.g., API身份验证错误或“服务不可用”状态)应该生成通知给管理员. Additionally, developers might add code to access a backup API if the primary API is down.

Once the code does everything it’s supposed to, the next crucial step is to test and confirm that it will hold up in real-life situations. Testing should be comprehensive, with a diverse array of test cases that include potential error conditions and edge cases. 为了提高可靠性和更快的反馈,您可以自动化测试. To assess real-world performance, measuring performance metrics such as execution time, memory usage, 资源消耗可以帮助识别系统中的潜在瓶颈. Insights derived from continuous testing and monitoring can help refine prompts and fine-tune LLM parameters.

The Evolution of LLMs

While LLMs are in no way a replacement for human expertise, 它们生成代码的能力是一种变革性的创新,可以为开发人员提供有价值的帮助. Not only can LLMs speed up the development cycle, an LLM-based smart virtual assistant can quickly generate multiple variations of the code, letting developers choose the optimal version. 将更简单的任务委托给LLM可以提高开发人员的生产力, letting them focus on complicated tasks that require specialized knowledge and human thought, 比如解决问题和设计下一代应用程序. With clear prompts and comprehensive testing, 开发人员可以利用api将LLM的功能添加到应用程序中.

With more and more developers discovering the benefits of AI, the technology will improve very quickly; however, it is important to keep in mind responsible and ethical usage. Just like all generative AI users, 软件开发人员有责任密切关注数据隐私的侵犯, intellectual property, security concerns, unintended output, and potential biases in LLM training. LLMs are currently being heavily researched, and as the technology advances, 它们将演变成无缝集成的智能虚拟助手.

Understanding the basics

  • What are the benefits of using the OpenAI API over ChatGPT?

    The OpenAI API allows programmatic access to the GPT model, enabling it to be integrated into new and existing applications. Additionally, it allows further customization over what is available with the ChatGPT interface.

  • What does an AI virtual assistant do?

    人工智能虚拟助手响应人类语言提示,为用户提供个性化帮助. An AI virtual assistant can help in a variety of ways, including with task automation, answering questions, navigation, weather forecasts, and scheduling.

  • How do I use an LLM to generate code?

    To generate code, 首先选择你想要使用的法学硕士,并决定是否要通过编程或通过可用的用户界面,如OpenAI的ChatGPT或微软的Copilot访问它. The LLM should be given a prompt detailing the programming language and desired functionality. The prompt should be as specific as possible.

Hire a Toptal expert on this topic.
Hire Now
Tarek Mohamed Mehrez

Tarek Mohamed Mehrez

Verified Expert in Engineering
10 Years of Experience

Amsterdam, Netherlands

Member since September 23, 2021

About the author

Tarek是一名专攻机器学习的软件和数据工程师, software architecture, DevOps, and back-end development. He has served as CTO of Thndr, a company backed by Y Combinator, and has published several research papers on machine learning. Tarek has a master’s degree in computational linguistics from the University of Stuttgart.

authors are vetted experts in their fields and write on topics in which they have demonstrated experience. All of our content is peer reviewed and validated by Toptal experts in the same field.

Previous Role

CTO

PREVIOUSLY AT

KlarnaMiroING

World-class articles, delivered weekly.

By entering your email, you are agreeing to our privacy policy.

World-class articles, delivered weekly.

By entering your email, you are agreeing to our privacy policy.

Toptal Developers

Join the Toptal® community.