Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 支持修改API地址 #51

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ internal object MiraiOpenAiConfig : ReadOnlyPluginConfig(saveName = "openai"), O
@ValueDescription("API 超时时间, 如果出现 TimeoutException 时, 请尝试调大")
override val timeout: Long by value(30_000L)

@ValueName("api")
@ValueDescription("API 地址")
override val api: String by value("https://api.openai.com/v1")

@ValueName("token")
@ValueDescription("OPENAI_TOKEN https://platform.openai.com/account/api-keys")
override val token: String by value(System.getenv("OPENAI_TOKEN").orEmpty())
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/xyz/cssxsh/openai/OpenAiClientConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package xyz.cssxsh.openai
public interface OpenAiClientConfig {
public val proxy: String
public val doh: String
public val api: String
public val ipv6: Boolean
public val timeout: Long
public val token: String
Expand Down
7 changes: 3 additions & 4 deletions src/main/kotlin/xyz/cssxsh/openai/audio/AudioController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import xyz.cssxsh.openai.*
* [Audio](https://platform.openai.com/docs/api-reference/audio)
*/
public class AudioController(private val client: OpenAiClient) {

/**
* [Create speech](https://platform.openai.com/docs/api-reference/audio/createSpeech)
*/
public suspend fun speech(request: SpeechRequest): ByteArray {
val response = client.http.post("https://api.openai.com/v1/audio/speech") {
val response = client.http.post("${client.config.api}/audio/speech") {
contentType(ContentType.Application.Json)
setBody(request)
}
Expand All @@ -27,7 +26,7 @@ public class AudioController(private val client: OpenAiClient) {
* [Create transcription](https://platform.openai.com/docs/api-reference/audio/createTranscription)
*/
public suspend fun transcription(file: ByteArray, request: TranscriptionRequest): AudioInfo {
val response = client.http.submitFormWithBinaryData("https://api.openai.com/v1/audio/transcriptions", formData {
val response = client.http.submitFormWithBinaryData("${client.config.api}/audio/transcriptions", formData {
append("file", file)
append("model", request.model)
append("language", request.language)
Expand All @@ -54,7 +53,7 @@ public class AudioController(private val client: OpenAiClient) {
* [Create translation](https://platform.openai.com/docs/api-reference/audio/createTranslation)
*/
public suspend fun translation(file: ByteArray, request: TranscriptionRequest): AudioInfo {
val response = client.http.submitFormWithBinaryData("https://api.openai.com/v1/audio/translations", formData {
val response = client.http.submitFormWithBinaryData("${client.config.api}/audio/translations", formData {
append("file", file)
append("model", request.model)
append("prompt", request.prompt)
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/xyz/cssxsh/openai/chat/ChatController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import xyz.cssxsh.openai.*
* @since 1.2.0
*/
public class ChatController(private val client: OpenAiClient) {
public val url: String = "${client.config.api}/chat/completions"

/**
* [Create chat completion](https://platform.openai.com/docs/api-reference/chat/create)
*/
public suspend fun create(request: ChatRequest): ChatInfo {
val response = client.http.post("https://api.openai.com/v1/chat/completions") {
val response = client.http.post(url) {
contentType(ContentType.Application.Json)
setBody(request)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import xyz.cssxsh.openai.*
* [Completions](https://platform.openai.com/docs/api-reference/completions)
*/
public class CompletionController(private val client: OpenAiClient) {
public val url: String = "${client.config.api}/completions"

/**
* [Create completion](https://platform.openai.com/docs/api-reference/completions/create)
*/
public suspend fun create(request: CompletionRequest): CompletionInfo {
val response = client.http.post("https://api.openai.com/v1/completions") {
val response = client.http.post(url) {
contentType(ContentType.Application.Json)
setBody(request)
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/xyz/cssxsh/openai/edit/EditController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import xyz.cssxsh.openai.*
*/
@Deprecated("API Deprecated")
public class EditController(private val client: OpenAiClient) {
public val url: String = "${client.config.api}/edits"

/**
* [Create edit](https://platform.openai.com/docs/api-reference/edits/create)
*/
public suspend fun create(request: EditRequest): EditInfo {
val response = client.http.post("https://api.openai.com/v1/edits") {
val response = client.http.post(url) {
contentType(ContentType.Application.Json)
setBody(request)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import xyz.cssxsh.openai.*
* [Embeddings](https://platform.openai.com/docs/api-reference/embeddings)
*/
public class EmbeddingController(private val client: OpenAiClient) {
public val url: String = "${client.config.api}/embeddings"

/**
* [Create embeddings](https://platform.openai.com/docs/api-reference/embeddings/create)
*/
public suspend fun create(request: EmbeddingRequest): List<EmbeddingInfo> {
val response = client.http.post("https://api.openai.com/v1/embeddings") {
val response = client.http.post(url) {
contentType(ContentType.Application.Json)
setBody(request)
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/xyz/cssxsh/openai/file/FileController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class FileController(private val client: OpenAiClient) {
* [List files](https://platform.openai.com/docs/api-reference/files/list)
*/
public suspend fun list(): List<FileInfo> {
val response = client.http.get("https://api.openai.com/v1/files")
val response = client.http.get("${client.config.api}/files")
val body = response.body<ListWrapper<FileInfo>>()

return body.data
Expand All @@ -26,7 +26,7 @@ public class FileController(private val client: OpenAiClient) {
* [Upload file](https://platform.openai.com/docs/api-reference/files/upload)
*/
public suspend fun create(file: InputProvider, purpose: String): FileInfo {
val response = client.http.submitFormWithBinaryData("https://api.openai.com/v1/files", formData {
val response = client.http.submitFormWithBinaryData("${client.config.api}/files", formData {
append("file", file)
append("purpose", purpose)
})
Expand All @@ -38,7 +38,7 @@ public class FileController(private val client: OpenAiClient) {
* [Delete file](https://platform.openai.com/docs/api-reference/files/delete)
*/
public suspend fun delete(fileId: String): FileInfo {
val response = client.http.delete("https://api.openai.com/v1/files/${fileId}")
val response = client.http.delete("${client.config.api}/files/${fileId}")

return response.body()
}
Expand All @@ -47,7 +47,7 @@ public class FileController(private val client: OpenAiClient) {
* [Retrieve file](https://platform.openai.com/docs/api-reference/files/retrieve)
*/
public suspend fun retrieve(fileId: String): FileInfo {
val response = client.http.get("https://api.openai.com/v1/files/${fileId}")
val response = client.http.get("${client.config.api}/files/${fileId}")

return response.body()
}
Expand All @@ -56,7 +56,7 @@ public class FileController(private val client: OpenAiClient) {
* [Retrieve file content](https://platform.openai.com/docs/api-reference/files/retrieve-contents)
*/
public suspend fun download(fileId: String): ByteReadChannel {
val response = client.http.get("https://api.openai.com/v1/files/${fileId}/content")
val response = client.http.get("${client.config.api}/files/${fileId}/content")

return response.bodyAsChannel()
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/xyz/cssxsh/openai/finetune/FineTuneController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class FineTuneController(private val client: OpenAiClient) {
* [Create fine-tunes](https://platform.openai.com/docs/api-reference/fine-tunes/create)
*/
public suspend fun create(request: FineTuneRequest): FineTuneInfo {
val response = client.http.post("https://api.openai.com/v1/fine-tunes") {
val response = client.http.post("${client.config.api}/fine-tunes") {
contentType(ContentType.Application.Json)
setBody(request)
}
Expand All @@ -27,7 +27,7 @@ public class FineTuneController(private val client: OpenAiClient) {
* [List fine-tunes](https://platform.openai.com/docs/api-reference/fine-tunes/list)
*/
public suspend fun list(): List<FineTuneInfo> {
val response = client.http.get("https://api.openai.com/v1/fine-tunes")
val response = client.http.get("${client.config.api}/fine-tunes")
val body = response.body<ListWrapper<FineTuneInfo>>()

return body.data
Expand All @@ -37,7 +37,7 @@ public class FineTuneController(private val client: OpenAiClient) {
* [Retrieve fine-tune](https://platform.openai.com/docs/api-reference/fine-tunes/retrieve)
*/
public suspend fun retrieve(fineTuneId: String): FineTuneInfo {
val response = client.http.get("https://api.openai.com/v1/fine-tunes/${fineTuneId}")
val response = client.http.get("${client.config.api}/fine-tunes/${fineTuneId}")

return response.body()
}
Expand All @@ -46,7 +46,7 @@ public class FineTuneController(private val client: OpenAiClient) {
* [Cancel fine-tune](https://platform.openai.com/docs/api-reference/fine-tunes/cancel)
*/
public suspend fun cancel(fineTuneId: String): FineTuneInfo {
val response = client.http.post("https://api.openai.com/v1/fine-tunes/${fineTuneId}/cancel")
val response = client.http.post("${client.config.api}/fine-tunes/${fineTuneId}/cancel")

return response.body()
}
Expand All @@ -55,7 +55,7 @@ public class FineTuneController(private val client: OpenAiClient) {
* [List fine-tune events](https://platform.openai.com/docs/api-reference/fine-tunes/events)
*/
public suspend fun events(fineTuneId: String, stream: Boolean = false): List<FineTuneEvent> {
val response = client.http.get("https://api.openai.com/v1/fine-tunes/${fineTuneId}/events") {
val response = client.http.get("${client.config.api}/fine-tunes/${fineTuneId}/events") {
parameter("stream", stream)
}
val body = response.body<ListWrapper<FineTuneEvent>>()
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/xyz/cssxsh/openai/image/ImageController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ImageController(private val client: OpenAiClient) {
* [Create image](https://platform.openai.com/docs/api-reference/images/create)
*/
public suspend fun create(request: ImageRequest): ImageInfo {
val response = client.http.post("https://api.openai.com/v1/images/generations") {
val response = client.http.post("${client.config.api}/images/generations") {
contentType(ContentType.Application.Json)
setBody(request)
}
Expand All @@ -34,7 +34,7 @@ public class ImageController(private val client: OpenAiClient) {
* [Create image edit](https://platform.openai.com/docs/api-reference/images/createEdit)
*/
public suspend fun createEdit(image: InputProvider, mask: InputProvider? = null, request: ImageRequest): ImageInfo {
val response = client.http.submitFormWithBinaryData("https://api.openai.com/v1/images/edits", formData {
val response = client.http.submitFormWithBinaryData("${client.config.api}/images/edits", formData {
append("image", image)
append("prompt", request.prompt)
if (mask != null) append("mask", mask)
Expand Down Expand Up @@ -68,7 +68,7 @@ public class ImageController(private val client: OpenAiClient) {
* [Create image variation](https://platform.openai.com/docs/api-reference/images/createVariation)
*/
public suspend fun createVariation(image: InputProvider, request: ImageRequest): ImageInfo {
val response = client.http.submitFormWithBinaryData("https://api.openai.com/v1/images/variations", formData {
val response = client.http.submitFormWithBinaryData("${client.config.api}/images/variations", formData {
append("image", image)
append("model", request.model)
append("n", request.number)
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/xyz/cssxsh/openai/model/ModelController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import xyz.cssxsh.openai.*
* [Models](https://platform.openai.com/docs/api-reference/models)
*/
public class ModelController(private val client: OpenAiClient) {
public val url: String = "${client.config.api}/models"

/**
* [List models](https://platform.openai.com/docs/api-reference/models/list)
*/
public suspend fun list(): List<ModelInfo> {
val response = client.http.get("https://api.openai.com/v1/models")
val response = client.http.get(url)
val body = response.body<ListWrapper<ModelInfo>>()

return body.data
Expand All @@ -23,7 +24,7 @@ public class ModelController(private val client: OpenAiClient) {
* [Retrieve model](https://platform.openai.com/docs/api-reference/models/retrieve)
*/
public suspend fun retrieve(model: String): ModelInfo {
val response = client.http.get("https://api.openai.com/v1/models/$model")
val response = client.http.get("$url/$model")

return response.body()
}
Expand All @@ -32,7 +33,7 @@ public class ModelController(private val client: OpenAiClient) {
* [Delete fine-tune model](https://platform.openai.com/docs/api-reference/models/delete)
*/
public suspend fun cancel(model: String): ModelInfo {
val response = client.http.delete("https://api.openai.com/v1/models/$model")
val response = client.http.delete("$url/$model")

return response.body()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import xyz.cssxsh.openai.*
* [Moderations](https://platform.openai.com/docs/api-reference/moderations)
*/
public class ModerationController(private val client: OpenAiClient) {
public val url: String = "${client.config.api}/moderations"

/**
* [Create moderation](https://platform.openai.com/docs/api-reference/moderations/create)
*/
public suspend fun create(request: ModerationRequest): ModerationInfo {
val response = client.http.post("https://api.openai.com/v1/moderations") {
val response = client.http.post(url) {
contentType(ContentType.Application.Json)
setBody(request)
}
Expand Down