|
问题是:- text = 'Write a python function that calculates the distance between any two latitudes and longitudes on earth'
复制代码
首先我们询问 Gemini(最后一句要求它不要生成额外的文本)
- # ask Gemini
- model = genai.GenerativeModel('gemini-pro')
- chat = model.start_chat(history=[])
- response = chat.send_message('%s -- Please answer as concisely as you can, avoiding any extra conversation or text' % text)
- gemini_response = response.text
复制代码
他的回答是- import math
- def distance(lat1, lon1, lat2, lon2):
- """Calculates the distance between two latitudes and longitudes.
- Args:
- lat1: The latitude of the first point.
- lon1: The longitude of the first point.
- lat2: The latitude of the second point.
- lon2: The longitude of the second point.
-
- Returns:
- The distance between the two points in kilometers.
- """
- # Convert the latitudes and longitudes to radians.
- lat1 = math.radians(lat1)
- lon1 = math.radians(lon1)
- lat2 = math.radians(lat2)
- lon2 = math.radians(lon2)
-
- # Calculate the difference between the latitudes and longitudes.
- dlat = lat2 - lat1
- dlon = lon2 - lon1
-
- # Calculate the square of the sine of the half of the difference in latitude.
- sin_dlat_half = math.sin(dlat / 2) ** 2
-
- # Calculate the square of the sine of the half of the difference in longitude.
- sin_dlon_half = math.sin(dlon / 2) ** 2
-
- # Calculate the square of the great-circle distance between the two points.
- a = sin_dlat_half + math.cos(lat1) * math.cos(lat2) * sin_dlon_half
-
- # Calculate the great-circle distance between the two points.
- c = 2 * math.asin(math.sqrt(a))
-
- # Calculate the Earth's radius in kilometers.
- r = 6371
-
- # Calculate the distance between the two points in kilometers.
- distance = r * c
-
- return distance
复制代码 然后我们询问 ChatGPT(最后一句要求它不要生成额外的文本)
- # ask ChatGPT
- completion = client.chat.completions.create(
- model="gpt-3.5-turbo",
- messages=[
- {"role": "user", "content": '%s -- Please answer as concisely as you can, avoiding any extra conversation or text' % text}
- ]
- )
- openai_response = completion.choices[0].message.content
复制代码
他的回答是- import math
- def calculate_distance(lat1, lon1, lat2, lon2):
- lat1 = math.radians(lat1)
- lon1 = math.radians(lon1)
- lat2 = math.radians(lat2)
- lon2 = math.radians(lon2)
- # Calculate differences
- dlat = lat2 - lat1
- dlon = lon2 - lon1
- # Calculate distance using Haversine formula
- a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
- c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
- distance = 6371 * c # Earth's radius in kilometers
复制代码
这两个生成的脚本哪个正确? |
|