OpenAI ChatGPT 使用示例(程序员)

作为一个程序员,当知道ChatGPT出来之后或者GPT3出来的时候,我是有喜有忧,喜的是它可以帮我写代码,重构代码,写注释,写测试,,。哇,听起来好刺激,我可以从此以后不用绞尽脑汁写测试类了,不用每天为追逐80%的测试覆盖率而烦恼了,不用为写一些POC代码疯狂的谷歌了,不用为写代码注释苦苦的思索了。忧的是它帮我做这么多事情,我是不是已经没用了?像我这样的程序员是不是真的没用了?那让我们看看ChatGPT可以给我做什么吧。

1.编程应用

1.1. 生成例子代码(Coding Generation)

ChatGPT帮助我们生产我们需要的例子代码。而且准确率很高。即使你不懂某一种语言也没关系,一定程度上较低了程序员的的门槛。

我有三组数据,第一组是星期一到星期五,第二组是这一天的具体时间,第三组是用户的数量,请给我用python生成一个预测未来用户量的算法

 

1.2. 调试代码(Debugging)

ChatGPT的错误修复能力对程序员来说也是一个有价值的工具。它可以通过提出可能的错误原因和提出解决方法来协助调试代码。

经一步调试下面代码,chatgpt给我在count变量上加了0的检查,防止了除0的错误。

查一下下面代码错误
def calculate_average(numbers):
   total=0
   count=0
   for num in numbers
     total +=num
     count +=1
   average = total/count
   return average

1.3. 代码重构(Clean code)

扫描二维码关注公众号,回复: 15456582 查看本文章

 source 

help me refactor below python code 

def calculate_price(quantity, price):

     total = quantity * price
     if total > = 100:
        discount = 0.1
     else:
       discount =0 
    final_price = total * (1- discount)
    return final_price

1.4. 代码补全(Coding Completion)

ChatGPT可以帮助完成代码,根据上下文和当前的代码,预测下面几行或几段的代码。这对那些可能不记得他们所使用的编程语言的所有语法和功能的程序员来说特别有利,可以节省时间和精力

 1.5. 给代码加注释(Documentation)

当程序员将他们的代码输入ChatGPT时,它可以根据编程语言和被记录的代码种类提出合适的文档模板。例如,如果代码是一个函数,ChatGPT可以提出一个函数文档模板,包括参数、返回值和对函数目标的描述

 

给我把一下代码加注释

Can you debug below code 
def calculate_average(numbers):
   total = 0
   count = 0
   for num in numbers:
     total += num
     count += 1
   average = total / count
   return average

1.6 写测试代码(Writing Test cases)

感觉我可以不用为讨厌的code coverge 烦恼了。

generate python test case for below calculate_average method

def calculate_average(numbers):
    total = 0
    count = 0
    for num in numbers:
        total += num
        count += 1
    if count == 0:
        return 0  # To avoid division by zero if the list is empty
    average = total / count
    return average
 

1.7 解释编码技术和概念

ChatGPT可以提供关于编程概念、软件产品、语法和功能的解释和例子,这对学习和理解编程语言很有帮助。这对可能不熟悉编程概念的初级程序员或正在使用新编程语言的有经验的程序员特别有用。

2.  翻译

ChatGPT可用于翻译服务,它可以自动将文本从一种语言翻译成另一种语言。

3.  ChatGPT + Knowledge Base回答问题的例子

可以借助chatgpt模型强大的理解力和公司内部知识库实现自动回答用户关于公司内部相关问题。如果只用chatgpt,chatgpt回答不了公司内部特有的业务知识和问题。其次是只有知识库既不能很好的理解客户的问题也不能组织语言回答问题。

As an advanced chatbot named Skippy, your primary goal is to assist users to the best of your ability.
 
START CONTEXT
Login to VideoGram from Website
1. Open your web browser and go to the VideoGram website.
2. Click on the “Login” button located in the top right corner of the page.
3. On the login page, enter your VideoGram username and password.
4. Once you have entered your credentials, click on the “Login” button.
5. You should now be logged in to your VideoGram account.
 
Login to VideoGram from Mobile App
1. Open the VideoGram app on your mobile device.
2. On the main page, tap the “Login” button located in the bottom right corner.
3. On the login page, enter your VideoGram username and password.
4. Once you have entered your credentials, tap the “Login” button.
5. You should now be logged in to your VideoGram account.
END CONTEXT
 
SKIPPY: Hello, I’m Skippy! How can I help you?
USER: I can’t find the login button.

SKIPPY:

猜你喜欢

转载自blog.csdn.net/keeppractice/article/details/131257488