内搜索:

ChatGPT和巨大洞穴探险

布鲁斯通
2024年2月19日

 

像很多人一样, 布鲁斯通, 麦克卢尔学院和MITS校友, has been dabbling with ChatGPT and was looking to get a little experience with the ChatGPT Python Application Programming Interface (API). Bruce希望使用Python程序与ChatGPT进行交互. For that, he needed some reason for a Python program to require predictive text.

 

这就是 巨大洞穴探险 进入画面. It is a text-based game dating back to 1976, commonly found on Unix systems. 它在Linux上是免费的, and Bruce knew he could make it available as a network service using another classic Unix service called "xinetd." Among xinetd's features is the ability to turn a program that reads from standard input and writes to standard output files and turn it into a networked service. 几乎所有命令行newbb电子程序都这样做. 通过配对 巨大洞穴探险 with xinetd, the classic adventure game becomes accessible via a network and even on the Internet.

 

The next step was to write a Python script that could connect to, and exchange text with, the 巨大洞穴探险. In effect, what was needed was a telnet client written in Python. 听起来工作量很大啊, 但它主要只是读取和写入文本到“套接字”," which is kind of like reading and writing to a file but is instead a network connection. Bruce促使ChatGPT编写这个脚本, saying "Write a Python script to open a TCP socket and send and receive text." The resulting script would do so but only for one transaction. A further prompt was needed: "Modify the script so that sending and receiving data are done in a loop until Control-C is typed." Further prompts had ChatGPT modify the structure of the script, breaking more things into isolated functions and moving hard-coded values into variables. 这是一个复杂的问题 巨大洞穴探险 每个包发送一封信, so the script needed to implement its own buffering instead of being able to rely on the operating system or the runtime environment to do that.

 

All of those modifications were applied via ChatGPT prompts but were in preparation for the next step, which was asking ChatGPT to add in the ChatGPT API so that the script could act as a middle-man between 这个游戏 and ChatGPT. This next step is when the script's job is to take text from 这个游戏 and send it to ChatGPT and then take ChatGPT's responses and send them to 这个游戏. 通过这种方式,ChatGPT可以 这个游戏. ChatGPT delivered, inserting the necessary calls to its own API.

 

此时此刻, the script was getting large enough that continuing to ask ChatGPT to make the changes was becoming cumbersome. The script needed numerous small tweaks and lots of debugging to iron out the interface between the two systems. For instance, ChatGPT likes to use complete sentences, but 这个游戏 expects two-word commands. ChatGPT uses mixed case and punctuation; 这个游戏 does not. 并且,作为即将出现的问题的标志,ChatGPT版本3.5 liked to forget its initial instructions and would start apologizing to 这个游戏 for issuing unknown commands. 那些道歉会, 当然, 也可以是未知命令, leading to an endless loop of polite but unproductive communication.

 

This brings us to what Bruce found to be the most interesting aspect of the effort. 脚本必须管理对话的历史记录. The various versions of ChatGPT all have a limited buffer size. 后期版本比早期版本具有更大的缓冲区. This means they can deal with longer conversation histories, but all of them have limits. And, processing a larger conversation consumes more "tokens," costing more. 在规模上,这些成本可能会变得很大.

 

在Python中,对话是一个字典列表. 考虑以下几点:

 

对话= [

            { "role" : "system", "content" : "YOU'RE PLAYING THE GAME ADVENTURE." },

            { "role" : "system", "content" : "COMMANDS ARE ONE OR TWO WORDS." },

            {"role": "user", "content": "WELCOME TO ADVENTURE ...." },

            {"role": "assistant", "content": "go north"}

]

 

“system”角色用于向ChatGPT提供指令. “user”角色是用户想要发送给ChatGPT的内容. 在这种情况下,游戏就是用户. ChatGPT回复的是“助理”角色. 当对话发生时, 您的程序可能会不断添加到这个列表中, so the list eventually grows to be larger than ChatGPT's buffer. 然后,ChatGPT开始忽略部分对话. Your script must include something to manage the conversation. 你可能会, 例如, 把你的列表当作一个队列, 或者先进先出(FIFO)数据结构, but that will eventually mean your instructions to ChatGPT are removed from the conversation. Managing the growth of the list might involve not leaving error conditions in the conversation because they may not assist ChatGPT. You may have to reassemble the conversation for each transaction to be able to keep your instructions to ChatGPT as part of the conversation.

 

那么ChatGPT 3的最终效果是什么呢.5 .玩游戏 巨大洞穴探险? ChatGPT mostly just wandered around the woods picking up and dropping objects. 还记得, it is a predictive text system and does not formulate strategy or even recognize there are objectives in 这个游戏. 这很像“无限猴定理”," which suggests that a monkey hitting keys on a keyboard for an infinite time will eventually type the complete works of Shakespeare.

 

As for using ChatGPT to write Python scripts, it is a handy tool. ChatGPT is unable to write larger programs with many requirements. It will be better at writing targeted functions to satisfy well-written prompts. The better you can communicate like a programmer, the more likely ChatGPT will deliver what you want. 你如何学会像程序员一样交流? 你学习编程. 术语是可以教的. 如何从ChatGPT函数中组装更大的程序? 经验. You need to have written programs yourself and come to an understanding about how best to design and integrate the associated logic. That's something you have to teach yourself, but it can be coached.

 


The 麦克卢尔学校 of Emerging Communication Technologies strives to offer the best academic programs in the IT(信息技术),网络安全,游戏开发和 虚拟现实/增强现实 (VR / AR)行业. Our programs and certificates cover numerous aspects of the rapidly changing industries of information networking, 网络安全、资料私隐; 游戏开发,数字动画和学术方面 的电子竞技.