I have a simple cloud function gen1 that accepts settings and returns a response from the “chat gpt” it sends, it is written in Python 3, which works great directly through the request, but when I try to do the same with the help of swift, it returns an error: Error: FunctionsErrorCode(rawValue: 3) , INVALID ARGUMENT, nil.
(everything worked when sending only the request text in the gpt configuration to the python function)
enter image description here
cloud function:
import openai
import requests
from flask import jsonify
openai_api_key = 'API_KEY'
url = "https://api.openai.com/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {openai_api_key}"
}
def chat_function(request):
try:
request_json = request.get_json(silent=True)
if not request_json:
return jsonify({"error": "Invalid request, JSON required"}), 400
# Перевіряємо, чи є ключ 'model' та 'messages' у запиті
model = request_json.get('model')
messages = request_json.get('messages')
if not model or not messages:
return jsonify({"error": "Model and messages are required"}), 400
data = {
"model": model,
"messages": messages
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
reply = response.json()['choices'][0]['message']['content']
return jsonify({"response": reply})
else:
return jsonify({"error": response.status_code, "message": response.text}), response.status_code
except Exception as e:
return jsonify({"error": str(e)}), 500
swift:
import UIKit
import FirebaseFunctions
class ViewController: UIViewController {
lazy var functions = Functions.functions()
override func viewDidLoad() {
super.viewDidLoad()
let data: [String: Any] = [
"model": "gpt-3.5-turbo",
"messages": [
["role": "system", "content": "You are a helpful assistant."],
["role": "user", "content": "Hello!"]
]
]
functions.httpsCallable("chat_function").call("data: {(data)} ") { result, error in
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
let code = FunctionsErrorCode(rawValue: error.code)
let message = error.localizedDescription
let details = error.userInfo[FunctionsErrorDetailsKey]
print("Error: (code ?? .unknown), (message), (String(describing: details))")
}
}
if let text = (result?.data as? [String: Any])?["response"] as? String {
print("Response from Cloud Function: (text)")
}
print(result?.data)
}
}
}
I tried different date formats:
// MARK: - DataClass
struct DataClass: Codable {
let model: String
let messages: [Message]
}
// MARK: - Message
struct Message: Codable {
let role, content: String
}
let messages = [
Message(role: "system", content: "You are a helpful assistant."),
Message(role: "user", content: "print(2) what is language?")
]
let dataClass = DataClass(model: "gpt-3.5-turbo", messages: messages)
also added {data: ….. before dataClass or tried to send a complete string.
FOX in glases is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.