$api_keys = array( '1234567890', '0987654321', 'qwertyuiop', 'asdfghjkl', 'zxcvbnm', ); function assign_random_api_key( $query ) { global $api_keys; $index = array_rand( $api_keys ); $query->setApiKey( $api_keys[$index] ); return $query; } add_filter( 'mwai_ai_query', 'assign_random_api_key' );怎么把上面的代码加入下方<?php class Meow_MWAI_AI { private $core = null; private $localApiKey = null; public function __construct( $core ) { $this->core = $core; $this->localApiKey = $this->core->get_option( 'openai_apikey' ); } public function runTranscribe( $query ) { if ( empty( $query->apiKey ) ) { $query->apiKey = $this->localApiKey; } $openai = new Meow_MWAI_OpenAI( $this->core ); $fields = array( 'prompt' => $query->prompt, 'model' => $query->model, 'response_format' => 'text', 'file' => basename( $query->url ), 'data' => file_get_contents( $query->url ) ); $modeEndpoint = $query->mode === 'translation' ? 'translations' : 'transcriptions'; $data = $openai->run( 'POST', '/audio/' . $modeEndpoint, null, $fields, false ); if ( empty( $data ) ) { throw new Exception( 'Invalid data for transcription.' ); } //$usage = $data['usage']; //$this->core->record_tokens_usage( $query->model, $usage['prompt_tokens'] ); $answer = new Meow_MWAI_Answer( $query ); //$answer->setUsage( $usage ); $answer->setChoices( $data ); return $answer; } public function runEmbedding( $query ) { if ( empty( $query->apiKey ) ) { $query->apiKey = $this->localApiKey; } $openai = new Meow_MWAI_OpenAI( $this->core ); $body = array( 'input' => $query->prompt, 'model' => $query->model ); $data = $openai->run( 'POST', '/embeddings', $body ); if ( empty( $data ) || !isset( $data['data'] ) ) { throw new Exception( 'Invalid data for embedding.' ); } $usage = $data['usage']; $this->core->record_tokens_usage( $query->model, $usage['prompt_tokens'] ); $answer = new Meow_MWAI_Answer( $query ); $answer->setUsage( $usage ); $answer->setChoices( $data['data'] ); return $answer; } public function runCompletion( $query ) { if ( empty( $query->apiKey ) ) { $query->apiKey = $this->localApiKey; } $url = ""; $body = array( "model" => $query->model, "stop" => $query->stop, "n" => $query->maxResults, "max_tokens" => $query->maxTokens, "temperature" => $query->temperature, ); if ( $query->mode === 'chat' ) { $url = 'https://api.openai.com/v1/chat/completions'; $body['messages'] = $query->messages; } else if ( $query->mode === 'completion' ) { $url = 'https://api.openai.com/v1/completions'; $body['prompt'] = $query->prompt; } else { throw new Exception( 'Unknown mode for query: ' . $query->mode ); } $options = array( "headers" => "Content-Type: application/json\r\n" . "Authorization: Bearer " . $query->apiKey . "\r\n", "method" => "POST", "timeout" => 120, "body" => json_encode( $body ), "sslverify" => false );
时间: 2023-03-28 07:00:57 浏览: 227
这是一个 PHP 代码段,用于调用 OpenAI API 进行语音转写、文本嵌入和文本自动补全等操作。要将此代码段添加到 PHP 文件中,只需将其复制并粘贴到 <?php 和 ?> 标记之间即可。请确保在添加代码之前,您已经安装了 Meow AI 插件,并将其配置为使用 OpenAI API。
相关问题
ESP8266,4针0.96寸oled,I2C协议,我所在的城市是四川德阳,WIFI名称是MERCURY_AB66,WIFI密码是qwertyuiop123网络天气时钟代码
很好,以下是你需要的ESP8266 + 0.96寸OLED I2C网络天气时钟代码。你只需要将WIFI名称和密码替换为你的即可。
```
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <JsonListener.h>
#include <JsonStreamingParser.h>
// Replace with your network credentials
const char* ssid = "MERCURY_AB66";
const char* password = "qwertyuiop123";
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Initialize the OLED display using Adafruit SSD1306 library
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 display(OLED_RESET);
// Weather API information
String apiKey = "YOUR_API_KEY";
String city = "YOUR_CITY";
// Variables to hold weather data
float temp;
float humidity;
float pressure;
String weatherDescription;
// Helper function to get weather data from OpenWeatherMap API
void getWeatherData() {
WiFiClient client;
const int httpPort = 80;
if (!client.connect("api.openweathermap.org", httpPort)) {
Serial.println("connection failed");
return;
}
client.println("GET /data/2.5/weather?q=" + city + "&units=metric&appid=" + apiKey);
client.println("Host: api.openweathermap.org");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
String line;
while (client.connected()) {
line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String jsonPayload = "";
while (client.available()) {
jsonPayload += (char)client.read();
}
client.stop();
// Parse JSON payload and extract weather data
JsonStreamingParser parser;
JsonListener *listener = new JsonListener();
parser.setListener(listener);
char jsonChar[jsonPayload.length()+1];
jsonPayload.toCharArray(jsonChar, jsonPayload.length()+1);
parser.parse(jsonChar, strlen(jsonChar));
temp = listener->temp;
humidity = listener->humidity;
pressure = listener->pressure;
weatherDescription = listener->weatherDescription;
}
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
// Initialize and clear the display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
// Initialize NTP client
timeClient.begin();
timeClient.setTimeOffset(7200);
// Get current time from NTP server
while(!timeClient.update()) {
timeClient.forceUpdate();
}
// Get initial weather data
getWeatherData();
}
void loop() {
// Update time from NTP server every 10 minutes
if (timeClient.getMinutes() % 10 == 0) {
timeClient.update();
}
// Get weather data every hour
if (timeClient.getMinutes() == 0) {
getWeatherData();
}
// Set up display
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(0,0);
// Print time and date
String timeString = String(hour()) + ":" + String(minute()) + ":" + String(second());
String dateString = String(day()) + "/" + String(month()) + "/" + String(year());
display.setTextSize(2);
display.print(timeString);
display.setTextSize(1);
display.print(" ");
display.println(dateString);
// Print weather data
display.setCursor(0, 30);
display.setTextSize(1);
display.print("Temperature: ");
display.println(temp, 1);
display.print("Humidity: ");
display.println(humidity, 1);
display.print("Pressure: ");
display.println(pressure, 1);
display.print("Weather: ");
display.println(weatherDescription);
// Display everything
display.display();
// Wait for one second before updating display again
delay(1000);
}
```
请注意,你需要将YOUR_API_KEY和YOUR_CITY替换为你的OpenWeatherMap API密钥和城市。另外,代码中的时间偏移量设置为7200秒,适用于UTC + 2时区,如果你所在的时区不同,请相应地更改。
using namespace std; #include "string.h" #include "iostream" // 该文件包含标准输入输出流cout和cin以及setw()等 typedef char KeyType; // 定义关键字类型为整型 typedef struct { KeyType key; }ElemType; // 表中数据元素类型 typedef ElemType TElemType; // 二叉排序树中结点类型 # include "stdlib.h" // 该文件包含malloc()、realloc()和free()等函数 typedef struct BiTNode { TElemType data; // 结点的值 int count_; struct BiTNode *lchild; // 左孩子指针 struct BiTNode *rchild; // 右孩子指针 }BiTNode,*BiTree;int main() { BiTree r,f,p; ElemType a[]="qwertyuiop"; KeyType key; int len = strlen(a); CreateBST(r,a,len); cout<<"中序遍历二叉树:" InOrderBiTree(r,Visit); }这段代码中Elemtype数据类型是?
在这段代码中,`ElemType`数据类型定义为:
```c
typedef char KeyType; // 定义关键字类型为字符型
typedef struct {
KeyType key; // 表中数据元素类型为字符型
}ElemType;
```
因此,`ElemType`数据类型是一个包含一个字符型成员变量`key`的结构体类型。
阅读全文