考虑到未来属于AI的世界,学会python还是蛮有必要的,了解python的语法,新建一个openAI的python项目,并运行该项目,向AI提问
>>> 8 / 5
1.6
>>> 17 / 3
5.666666666666667
>>> 17 // 3
5
>>> 5 ** 2
25
>>> 2 ** 7
128
>>> word = 'python'
>>> word[0]
'p'
>>> word[2]
't'
>>> word[-1]
'n'
a, b = 0, 1 #a,b各自赋值为0和1
>>> while a < 10:
... print(a, b)
... a, b = b, a+b
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print(n, 'equals', x, '*', n//x)
break
else:
# loop fell through without finding a factor
print(n, 'is a prime number')
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
def fib(n): # write fibonacci series up to n
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
# call function
fib(2000)
def ask_ok(prompt, retries=4, reminder='Please try again!'):
# 方法体代码忽略
# 下面的调用都可以
ask_ok('Do you really want to quit?')
ask_ok('OK to overwrite the file?', 2)
ask_ok(prompt='test', retries=3)
sound/ Top-level package
__init__.py Initialize the sound package
formats/ Subpackage for file format conversions
__init__.py
wavread.py
wavwrite.py
aiffread.py
aiffwrite.py
auread.py
auwrite.py
year = 2016
event = 'Referendum'
f'Results of the {year} {event}'
class PageRequest:
def __init__(self, page_num=1, page_size=500, return_total_num=True):
"""
分页查询请求对象
:param page_num: 页码
:param page_size: 每页大小
:return:
"""
self.pageNum = page_num
self.pageSize = page_size
self.returnTotalNum = return_total_num
def set_page_num(self, page_num):
self.pageNum = page_num
def get_page_num(self):
return self.pageNum
__init__方法是class被初始化调用的
接下来,用python调用openAI的SDK,发起对话
这里用的IDE是vsCode,新新建一个空的文件夹,用vsCode打开,新建两个文件 main.py 和 requirements.txt,在requirements引入openAI的SDK
openai==1.30.5
然后打开命令行面板(Option+Command+P),输入Python: Create Environment,选中这个命令
接着选中第一个Venv,这个是专门针对这个项目的一个虚拟的python环境,保证跟其他项目互不冲突
然后选择一个python的版本,我这里选择最新的3.12.3
接着选择依赖文件,就是requirement.txt,选择完,系统会自动运行,加载对应的环境信息和依赖的库,更新完成后,可以看到项目目录下,有一个.venv文件
到此,空白的python项目创建好了
在main.py增加入口方法
if __name__ == '__main__':
print("Hello World!")
根目录下,新增一个chat文件夹,下面新增两个文件
init.py表示这个是一个python模块,空文件,chat.py里面处理真实的openAI接口调用
根据官方文档,集成用openAI的SDK,去调用接口
from openai import AzureOpenAI
def testChat():
client = AzureOpenAI(
api_key="xxx",
api_version="2023-12-01-preview",
azure_endpoint="xxxxx"
)
model = "gpt-4o"
response = client.chat.completions.create(
model="gpt-4o", # gpt-4o model
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "it is difficult to learn python"},
]
)
print(response.choices[0].message.content)
接口调用,需要全部返回后,才会返回结果,这里要等几秒钟,返回的结果部分如下
Learning Python can be challenging, especially if you're new to programming. However, many people find Python to be one of the more accessible programming languages due to its simple and readable syntax. Here are some tips that might make learning Python easier for you:
1. **Start with the Basics**: Understand fundamental concepts like variables, data types, operators, control structures (if statements, loops), and functions.
2. **Use Online Resources**: There are many free and paid resources available to learn Python. Websites like Codecademy, Coursera, Udemy, and edX offer courses tailored to beginners.
python的入门还是很简单的,尤其是有其他语言的基础,再来学习就更简单了,目前整个AI的生态都是搭建在python上面,学会使用python,还是有必要的