├── .gitignore
├── .idea
├── .gitignore
├── .name
├── compiler.xml
├── gradle.xml
├── kotlinc.xml
├── misc.xml
└── vcs.xml
├── README.md
├── build.gradle
├── demo
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── yat3s
│ │ └── openai
│ │ └── api
│ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── yat3s
│ │ │ └── openai
│ │ │ └── demo
│ │ │ ├── MainActivity.kt
│ │ │ └── ui
│ │ │ └── theme
│ │ │ ├── Color.kt
│ │ │ ├── Theme.kt
│ │ │ └── Type.kt
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ ├── content_main.xml
│ │ ├── fragment_first.xml
│ │ └── fragment_second.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── navigation
│ │ └── nav_graph.xml
│ │ ├── values-land
│ │ └── dimens.xml
│ │ ├── values-night
│ │ └── themes.xml
│ │ ├── values-v29
│ │ └── themes.xml
│ │ ├── values-w1240dp
│ │ └── dimens.xml
│ │ ├── values-w600dp
│ │ └── dimens.xml
│ │ ├── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── themes.xml
│ │ └── xml
│ │ ├── backup_rules.xml
│ │ └── data_extraction_rules.xml
│ └── test
│ └── java
│ └── com
│ └── yat3s
│ └── openai
│ └── api
│ └── ExampleUnitTest.kt
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── openai
├── .gitignore
├── build.gradle
├── gradle.properties
├── proguard-rules.pro
└── src
│ ├── main
│ ├── AndroidManifest.xml
│ └── java
│ │ └── com
│ │ └── yat3s
│ │ └── openai
│ │ └── api
│ │ ├── Config.kt
│ │ ├── OpenAIClient.kt
│ │ ├── OpenAIClientBuilder.kt
│ │ ├── model
│ │ ├── ImageGenerationApiRequestBody.kt
│ │ ├── ImageGenerationApiResponse.kt
│ │ ├── ImageGenerationResponse.kt
│ │ ├── OpenAIRequestBody.kt
│ │ ├── TextCompletionApiRequestBody.kt
│ │ ├── TextCompletionApiResponse.kt
│ │ ├── TextCompletionError.kt
│ │ └── TextCompletionResponse.kt
│ │ ├── repository
│ │ └── OpenAIRepository.kt
│ │ └── service
│ │ └── OpenAIService.kt
│ └── test
│ └── java
│ └── com
│ └── yat3s
│ └── openai
│ └── openai
│ └── ExampleUnitTest.kt
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 | local.properties
16 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | openai-api
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
19 |
20 |
--------------------------------------------------------------------------------
/.idea/kotlinc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # OpenAI Android
2 | The project for OpenAI official APIs
3 | - [x] Text Completion
4 | - [x] Image Generation(DALL-E)
5 | - [ ] Playground
6 | - [ ] ChatGPT Web API(ChatGPT Plus)
7 | - [ ] New Bing AI API
8 |
9 | # Usages
10 | ```
11 | implementation 'com.yat3s.openai:openai-android:0.0.4'
12 | ```
13 |
14 | Or exclude okhttp if you're using a version < `okhttp3: 4.10.0` in your project.
15 | ```
16 | implementation 'com.yat3s.openai:openai-android:0.0.4' {
17 | exclude group: "com.squareup.okhttp3"
18 | }
19 | ```
20 |
21 | ## Text Completion
22 | ```
23 | val response = TextCompletionBuilder(BuildConfig.OPENAI_API_KEY).build().textCompletion("Your prompt)
24 | ```
25 |
26 | #### Advanced Configurations
27 | ```
28 | val response = TextCompletionBuilder(OPENAI_API_KEY)
29 | .maxTokens(1000) // 1 - 4000, default 500
30 | .temperature(0.6f) // 0 - 2f, default 1f
31 | .model("you model") // default "text-davinci-003"
32 | .build().textCompletion(text)
33 | ```
34 |
35 | Please check the definition on [Open AI text completion parameters](https://platform.openai.com/docs/api-reference/completions/create)
36 |
37 | ## Image Generation(DALL-E)
38 | ```
39 | val response = ImageGenerationBuilder(BuildConfig.OPENAI_API_KEY).build().imageGeneration("Your prompt")
40 | ```
41 | #### Advanced Configurations
42 | ```
43 | val response = ImageGenerationBuilder(OPENAI_API_KEY)
44 | .generateCount(2) // The image count to generate, default 1
45 | .imageSize("512x512") // "256x256", "512x512", "1024x1024" are supported, default "1024x1024"
46 | .imageResponseFormat("url") // "url", "b64_json" are supported, default "url"
47 | .build()
48 | .imageGeneration("Your prompt")
49 | ```
50 |
51 |
52 | # How to get API KEY
53 | Please go to [Open AI API KEY](https://platform.openai.com/account/api-keys) to create your API keys.
54 | 
55 |
56 |
57 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | ext {
3 | compose_version = '1.2.0-beta01'
4 | }
5 | repositories {
6 | google()
7 | mavenCentral()
8 | }
9 | }// Top-level build file where you can add configuration options common to all sub-projects/modules.
10 | plugins {
11 | id 'com.android.application' version '7.4.1' apply false
12 | id 'com.android.library' version '7.4.1' apply false
13 | id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
14 | }
--------------------------------------------------------------------------------
/demo/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/demo/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'org.jetbrains.kotlin.android'
4 | }
5 |
6 | def properties = new Properties()
7 | properties.load(project.rootProject.file("local.properties").newDataInputStream())
8 |
9 | android {
10 | namespace 'com.yat3s.openai.demo'
11 | compileSdk 33
12 |
13 | defaultConfig {
14 | applicationId "com.yat3s.openai.demo"
15 | minSdk 24
16 | targetSdk 33
17 | versionCode 1
18 | versionName "1.0"
19 |
20 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
21 | vectorDrawables {
22 | useSupportLibrary true
23 | }
24 |
25 | buildConfigField "String", "OPENAI_API_KEY", "\"${properties.getProperty("openaiApiKey", "")}\""
26 | }
27 |
28 | buildTypes {
29 | release {
30 | minifyEnabled false
31 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
32 | }
33 | }
34 | compileOptions {
35 | sourceCompatibility JavaVersion.VERSION_1_8
36 | targetCompatibility JavaVersion.VERSION_1_8
37 | }
38 | kotlinOptions {
39 | jvmTarget = '1.8'
40 | }
41 | buildFeatures {
42 | compose true
43 | viewBinding true
44 | }
45 | composeOptions {
46 | kotlinCompilerExtensionVersion '1.2.0-beta01'
47 | }
48 | packagingOptions {
49 | resources {
50 | excludes += '/META-INF/{AL2.0,LGPL2.1}'
51 | }
52 | }
53 | }
54 |
55 | dependencies {
56 | implementation 'com.yat3s.openai:openai-android:0.0.4'
57 | // implementation project(":openai")
58 | implementation 'com.squareup.picasso:picasso:2.8'
59 | implementation("com.squareup.okhttp3:okhttp:4.10.0")
60 | implementation("com.squareup.retrofit2:retrofit:2.9.0")
61 | implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
62 | implementation 'androidx.core:core-ktx:1.7.0'
63 | implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
64 | implementation 'androidx.activity:activity-compose:1.3.1'
65 | implementation "androidx.compose.ui:ui:$compose_version"
66 | implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
67 | implementation 'androidx.compose.material3:material3:1.0.0-alpha11'
68 | implementation 'com.google.android.material:material:1.5.0-alpha04'
69 | implementation 'androidx.appcompat:appcompat:1.6.0'
70 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
71 | implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1'
72 | implementation 'androidx.navigation:navigation-ui-ktx:2.4.1'
73 | testImplementation 'junit:junit:4.13.2'
74 | androidTestImplementation 'androidx.test.ext:junit:1.1.5'
75 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
76 | androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
77 | debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
78 | debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
79 | }
--------------------------------------------------------------------------------
/demo/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/demo/src/androidTest/java/com/yat3s/openai/api/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.yat3s.openai.api", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/demo/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
17 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yat3s/openai/demo/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.demo
2 |
3 | import android.os.Bundle
4 | import android.widget.ImageView
5 | import androidx.appcompat.app.AppCompatActivity
6 | import androidx.core.view.WindowCompat
7 | import androidx.core.view.marginEnd
8 | import androidx.core.view.updatePaddingRelative
9 | import com.squareup.picasso.Picasso
10 | import com.yat3s.openai.api.ImageGenerationBuilder
11 | import com.yat3s.openai.api.OpenAIClient
12 | import com.yat3s.openai.api.TextCompletionBuilder
13 | import com.yat3s.openai.demo.databinding.ActivityMainBinding
14 | import kotlinx.coroutines.Dispatchers
15 | import kotlinx.coroutines.GlobalScope
16 | import kotlinx.coroutines.launch
17 | import kotlinx.coroutines.withContext
18 |
19 | class MainActivity : AppCompatActivity() {
20 | private lateinit var binding: ActivityMainBinding
21 |
22 | override fun onCreate(savedInstanceState: Bundle?) {
23 | super.onCreate(savedInstanceState)
24 |
25 | binding = ActivityMainBinding.inflate(layoutInflater)
26 | setContentView(binding.root)
27 |
28 | binding.textCompletion.setOnClickListener {
29 | GlobalScope.launch {
30 | val response = TextCompletionBuilder(BuildConfig.OPENAI_API_KEY)
31 | .build()
32 | .textCompletion(binding.input.text.toString())
33 | withContext(Dispatchers.Main) {
34 | binding.textCompletionOutput.text = "Output: ${response?.text}"
35 | }
36 | }
37 | }
38 |
39 | binding.imageGeneration.setOnClickListener {
40 | GlobalScope.launch {
41 | val response = ImageGenerationBuilder(BuildConfig.OPENAI_API_KEY)
42 | .generateCount(2)
43 | .build()
44 | .imageGeneration(binding.input.text.toString())
45 |
46 | withContext(Dispatchers.Main) {
47 | val urls = response?.urls
48 | if (!urls.isNullOrEmpty()) {
49 | urls.forEach {
50 | val imageView = ImageView(this@MainActivity)
51 | binding.imageGenerationContainer.addView(imageView)
52 | imageView.layoutParams.apply {
53 | width = 500
54 | height = 500
55 | }
56 | imageView.updatePaddingRelative(end = 12)
57 |
58 | Picasso.get().load(it).fit()
59 | .centerInside()
60 | .into(imageView)
61 | }
62 | }
63 | }
64 | }
65 | }
66 | }
67 | }
--------------------------------------------------------------------------------
/demo/src/main/java/com/yat3s/openai/demo/ui/theme/Color.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.demo.ui.theme
2 |
3 | import androidx.compose.ui.graphics.Color
4 |
5 | val Purple80 = Color(0xFFD0BCFF)
6 | val PurpleGrey80 = Color(0xFFCCC2DC)
7 | val Pink80 = Color(0xFFEFB8C8)
8 |
9 | val Purple40 = Color(0xFF6650a4)
10 | val PurpleGrey40 = Color(0xFF625b71)
11 | val Pink40 = Color(0xFF7D5260)
--------------------------------------------------------------------------------
/demo/src/main/java/com/yat3s/openai/demo/ui/theme/Theme.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.demo.ui.theme
2 |
3 | import android.app.Activity
4 | import android.os.Build
5 | import androidx.compose.foundation.isSystemInDarkTheme
6 | import androidx.compose.material3.MaterialTheme
7 | import androidx.compose.material3.darkColorScheme
8 | import androidx.compose.material3.dynamicDarkColorScheme
9 | import androidx.compose.material3.dynamicLightColorScheme
10 | import androidx.compose.material3.lightColorScheme
11 | import androidx.compose.runtime.Composable
12 | import androidx.compose.runtime.SideEffect
13 | import androidx.compose.ui.graphics.toArgb
14 | import androidx.compose.ui.platform.LocalContext
15 | import androidx.compose.ui.platform.LocalView
16 | import androidx.core.view.ViewCompat
17 |
18 | private val DarkColorScheme = darkColorScheme(
19 | primary = Purple80,
20 | secondary = PurpleGrey80,
21 | tertiary = Pink80
22 | )
23 |
24 | private val LightColorScheme = lightColorScheme(
25 | primary = Purple40,
26 | secondary = PurpleGrey40,
27 | tertiary = Pink40
28 |
29 | /* Other default colors to override
30 | background = Color(0xFFFFFBFE),
31 | surface = Color(0xFFFFFBFE),
32 | onPrimary = Color.White,
33 | onSecondary = Color.White,
34 | onTertiary = Color.White,
35 | onBackground = Color(0xFF1C1B1F),
36 | onSurface = Color(0xFF1C1B1F),
37 | */
38 | )
39 |
40 | @Composable
41 | fun OpenaiapiTheme(
42 | darkTheme: Boolean = isSystemInDarkTheme(),
43 | // Dynamic color is available on Android 12+
44 | dynamicColor: Boolean = true,
45 | content: @Composable () -> Unit
46 | ) {
47 | val colorScheme = when {
48 | dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
49 | val context = LocalContext.current
50 | if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
51 | }
52 | darkTheme -> DarkColorScheme
53 | else -> LightColorScheme
54 | }
55 | val view = LocalView.current
56 | if (!view.isInEditMode) {
57 | SideEffect {
58 | (view.context as Activity).window.statusBarColor = colorScheme.primary.toArgb()
59 | ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = darkTheme
60 | }
61 | }
62 |
63 | MaterialTheme(
64 | colorScheme = colorScheme,
65 | typography = Typography,
66 | content = content
67 | )
68 | }
--------------------------------------------------------------------------------
/demo/src/main/java/com/yat3s/openai/demo/ui/theme/Type.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.demo.ui.theme
2 |
3 | import androidx.compose.material3.Typography
4 | import androidx.compose.ui.text.TextStyle
5 | import androidx.compose.ui.text.font.FontFamily
6 | import androidx.compose.ui.text.font.FontWeight
7 | import androidx.compose.ui.unit.sp
8 |
9 | // Set of Material typography styles to start with
10 | val Typography = Typography(
11 | bodyLarge = TextStyle(
12 | fontFamily = FontFamily.Default,
13 | fontWeight = FontWeight.Normal,
14 | fontSize = 16.sp,
15 | lineHeight = 24.sp,
16 | letterSpacing = 0.5.sp
17 | )
18 | /* Other default text styles to override
19 | titleLarge = TextStyle(
20 | fontFamily = FontFamily.Default,
21 | fontWeight = FontWeight.Normal,
22 | fontSize = 22.sp,
23 | lineHeight = 28.sp,
24 | letterSpacing = 0.sp
25 | ),
26 | labelSmall = TextStyle(
27 | fontFamily = FontFamily.Default,
28 | fontWeight = FontWeight.Medium,
29 | fontSize = 11.sp,
30 | lineHeight = 16.sp,
31 | letterSpacing = 0.5.sp
32 | )
33 | */
34 | )
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/demo/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/demo/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
16 |
17 |
24 |
25 |
33 |
34 |
39 |
40 |
47 |
48 |
55 |
56 |
59 |
60 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/demo/src/main/res/layout/content_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
19 |
--------------------------------------------------------------------------------
/demo/src/main/res/layout/fragment_first.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
23 | />
24 |
25 |
35 |
36 |
--------------------------------------------------------------------------------
/demo/src/main/res/layout/fragment_second.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
23 |
24 |
34 |
35 |
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-hdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yat3s/openai-android/4c26cde5fe1609b5e7656326ff9e7a6f14b75413/demo/src/main/res/mipmap-hdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-hdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yat3s/openai-android/4c26cde5fe1609b5e7656326ff9e7a6f14b75413/demo/src/main/res/mipmap-hdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-mdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yat3s/openai-android/4c26cde5fe1609b5e7656326ff9e7a6f14b75413/demo/src/main/res/mipmap-mdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-mdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yat3s/openai-android/4c26cde5fe1609b5e7656326ff9e7a6f14b75413/demo/src/main/res/mipmap-mdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yat3s/openai-android/4c26cde5fe1609b5e7656326ff9e7a6f14b75413/demo/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yat3s/openai-android/4c26cde5fe1609b5e7656326ff9e7a6f14b75413/demo/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yat3s/openai-android/4c26cde5fe1609b5e7656326ff9e7a6f14b75413/demo/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yat3s/openai-android/4c26cde5fe1609b5e7656326ff9e7a6f14b75413/demo/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yat3s/openai-android/4c26cde5fe1609b5e7656326ff9e7a6f14b75413/demo/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yat3s/openai-android/4c26cde5fe1609b5e7656326ff9e7a6f14b75413/demo/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/demo/src/main/res/navigation/nav_graph.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
17 |
18 |
23 |
24 |
27 |
28 |
--------------------------------------------------------------------------------
/demo/src/main/res/values-land/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 | 48dp
3 |
--------------------------------------------------------------------------------
/demo/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
--------------------------------------------------------------------------------
/demo/src/main/res/values-v29/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
--------------------------------------------------------------------------------
/demo/src/main/res/values-w1240dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 | 200dp
3 |
--------------------------------------------------------------------------------
/demo/src/main/res/values-w600dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 | 48dp
3 |
--------------------------------------------------------------------------------
/demo/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/demo/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 | 16dp
3 |
--------------------------------------------------------------------------------
/demo/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | openai-api
3 | MainActivity2
4 |
5 | First Fragment
6 | Second Fragment
7 | Next
8 | Previous
9 |
10 |
11 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam in scelerisque sem. Mauris
12 | volutpat, dolor id interdum ullamcorper, risus dolor egestas lectus, sit amet mattis purus
13 | dui nec risus. Maecenas non sodales nisi, vel dictum dolor. Class aptent taciti sociosqu ad
14 | litora torquent per conubia nostra, per inceptos himenaeos. Suspendisse blandit eleifend
15 | diam, vel rutrum tellus vulputate quis. Aliquam eget libero aliquet, imperdiet nisl a,
16 | ornare ex. Sed rhoncus est ut libero porta lobortis. Fusce in dictum tellus.\n\n
17 | Suspendisse interdum ornare ante. Aliquam nec cursus lorem. Morbi id magna felis. Vivamus
18 | egestas, est a condimentum egestas, turpis nisl iaculis ipsum, in dictum tellus dolor sed
19 | neque. Morbi tellus erat, dapibus ut sem a, iaculis tincidunt dui. Interdum et malesuada
20 | fames ac ante ipsum primis in faucibus. Curabitur et eros porttitor, ultricies urna vitae,
21 | molestie nibh. Phasellus at commodo eros, non aliquet metus. Sed maximus nisl nec dolor
22 | bibendum, vel congue leo egestas.\n\n
23 | Sed interdum tortor nibh, in sagittis risus mollis quis. Curabitur mi odio, condimentum sit
24 | amet auctor at, mollis non turpis. Nullam pretium libero vestibulum, finibus orci vel,
25 | molestie quam. Fusce blandit tincidunt nulla, quis sollicitudin libero facilisis et. Integer
26 | interdum nunc ligula, et fermentum metus hendrerit id. Vestibulum lectus felis, dictum at
27 | lacinia sit amet, tristique id quam. Cras eu consequat dui. Suspendisse sodales nunc ligula,
28 | in lobortis sem porta sed. Integer id ultrices magna, in luctus elit. Sed a pellentesque
29 | est.\n\n
30 | Aenean nunc velit, lacinia sed dolor sed, ultrices viverra nulla. Etiam a venenatis nibh.
31 | Morbi laoreet, tortor sed facilisis varius, nibh orci rhoncus nulla, id elementum leo dui
32 | non lorem. Nam mollis ipsum quis auctor varius. Quisque elementum eu libero sed commodo. In
33 | eros nisl, imperdiet vel imperdiet et, scelerisque a mauris. Pellentesque varius ex nunc,
34 | quis imperdiet eros placerat ac. Duis finibus orci et est auctor tincidunt. Sed non viverra
35 | ipsum. Nunc quis augue egestas, cursus lorem at, molestie sem. Morbi a consectetur ipsum, a
36 | placerat diam. Etiam vulputate dignissim convallis. Integer faucibus mauris sit amet finibus
37 | convallis.\n\n
38 | Phasellus in aliquet mi. Pellentesque habitant morbi tristique senectus et netus et
39 | malesuada fames ac turpis egestas. In volutpat arcu ut felis sagittis, in finibus massa
40 | gravida. Pellentesque id tellus orci. Integer dictum, lorem sed efficitur ullamcorper,
41 | libero justo consectetur ipsum, in mollis nisl ex sed nisl. Donec maximus ullamcorper
42 | sodales. Praesent bibendum rhoncus tellus nec feugiat. In a ornare nulla. Donec rhoncus
43 | libero vel nunc consequat, quis tincidunt nisl eleifend. Cras bibendum enim a justo luctus
44 | vestibulum. Fusce dictum libero quis erat maximus, vitae volutpat diam dignissim.
45 |
46 |
--------------------------------------------------------------------------------
/demo/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/demo/src/main/res/xml/backup_rules.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
13 |
--------------------------------------------------------------------------------
/demo/src/main/res/xml/data_extraction_rules.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
12 |
13 |
19 |
--------------------------------------------------------------------------------
/demo/src/test/java/com/yat3s/openai/api/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Kotlin code style for this project: "official" or "obsolete":
19 | kotlin.code.style=official
20 | # Enables namespacing of each library's R class so that its R class includes only the
21 | # resources declared in the library itself and none from the library's dependencies,
22 | # thereby reducing the size of the R class for that library
23 | android.nonTransitiveRClass=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yat3s/openai-android/4c26cde5fe1609b5e7656326ff9e7a6f14b75413/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Feb 11 16:43:56 CST 2023
2 | distributionBase=GRADLE_USER_HOME
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
4 | distributionPath=wrapper/dists
5 | zipStorePath=wrapper/dists
6 | zipStoreBase=GRADLE_USER_HOME
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/openai/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/openai/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'org.jetbrains.kotlin.android'
4 | id 'com.vanniktech.maven.publish' version "0.24.0"
5 | }
6 |
7 | android {
8 | namespace 'com.yat3s.openai.api'
9 | compileSdk 33
10 |
11 | defaultConfig {
12 | minSdk 23
13 | targetSdk 33
14 | }
15 |
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | }
23 |
24 | dependencies {
25 | implementation 'com.squareup.okhttp3:okhttp:4.10.0'
26 | implementation 'com.squareup.retrofit2:retrofit:2.9.0'
27 | implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
28 | }
29 |
--------------------------------------------------------------------------------
/openai/gradle.properties:
--------------------------------------------------------------------------------
1 | SONATYPE_HOST=DEFAULT
2 | RELEASE_SIGNING_ENABLED=true
3 |
4 | GROUP=com.yat3s.openai
5 | POM_ARTIFACT_ID=openai-android
6 | VERSION_NAME=0.0.4
7 |
8 | POM_NAME=OpenAI API
9 | POM_DESCRIPTION=An Android SDK for OpenAI API.
10 | POM_INCEPTION_YEAR=2023
11 | POM_URL=https://github.com/Yat3s/openai-android
12 |
13 | POM_LICENSE_NAME=The Apache Software License, Version 2.0
14 | POM_LICENSE_URL=https://www.apache.org/licenses/LICENSE-2.0.txt
15 | POM_LICENSE_DIST=repo
16 |
17 | POM_SCM_URL=https://github.com/Yat3s/openai-android/
18 | POM_SCM_CONNECTION=scm:git:git://github.com/Yat3s/openai-android.git
19 | POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/Yat3s/openai-android.git
20 |
21 | POM_DEVELOPER_ID=yat3s
22 | POM_DEVELOPER_NAME=Chris Ye
23 | POM_DEVELOPER_URL=https://github.com/yat3s/
--------------------------------------------------------------------------------
/openai/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/openai/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/openai/src/main/java/com/yat3s/openai/api/Config.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api
2 |
3 | object Config {
4 | const val BASE_URL = "https://api.openai.com/v1/"
5 | const val URL_TEXT_COMPLETION = "completions"
6 | const val URL_IMAGE_GENERATION = "images/generations"
7 | }
--------------------------------------------------------------------------------
/openai/src/main/java/com/yat3s/openai/api/OpenAIClient.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api
2 |
3 | import com.yat3s.openai.api.model.ImageGenerationApiRequestBody
4 | import com.yat3s.openai.api.model.ImageGenerationResponse
5 | import com.yat3s.openai.api.model.TextCompletionApiRequestBody
6 | import com.yat3s.openai.api.model.TextCompletionResponse
7 | import com.yat3s.openai.api.repository.OpenAIRepository
8 |
9 | class OpenAIClient internal constructor(
10 | private val builder: OpenAIClientBuilder
11 | ) {
12 | suspend fun textCompletion(prompt: String): TextCompletionResponse? {
13 | val apiResponse = OpenAIRepository().textCompletion(
14 | builder.apiKey, builder.toRequestBody(prompt) as TextCompletionApiRequestBody
15 | ) ?: return null
16 |
17 | apiResponse.error?.let {
18 | return TextCompletionResponse(
19 | errorMessage = it.message,
20 | apiResponseBody = apiResponse,
21 | )
22 | }
23 |
24 | val apiResponseChoices = apiResponse.choices
25 | if (apiResponseChoices.isNullOrEmpty()) {
26 | return TextCompletionResponse(
27 | errorMessage = "Has response but no choice.",
28 | apiResponseBody = apiResponse,
29 | )
30 | }
31 |
32 | return TextCompletionResponse(
33 | text = apiResponseChoices[0].text,
34 | apiResponseBody = apiResponse,
35 | )
36 | }
37 |
38 | suspend fun imageGeneration(
39 | prompt: String
40 | ): ImageGenerationResponse? {
41 | val apiResponse = OpenAIRepository().imageGeneration(
42 | builder.apiKey, builder.toRequestBody(prompt) as ImageGenerationApiRequestBody
43 | ) ?: return null
44 |
45 | apiResponse.error?.let {
46 | return ImageGenerationResponse(
47 | errorMessage = it.message,
48 | apiResponseBody = apiResponse,
49 | )
50 | }
51 |
52 | return ImageGenerationResponse(
53 | urls = apiResponse.data?.map {
54 | it.url
55 | },
56 | apiResponseBody = apiResponse,
57 | )
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/openai/src/main/java/com/yat3s/openai/api/OpenAIClientBuilder.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api
2 |
3 | import com.yat3s.openai.api.model.ImageGenerationApiRequestBody
4 | import com.yat3s.openai.api.model.ImageGenerationApiResponse
5 | import com.yat3s.openai.api.model.OpenAIRequestBody
6 | import com.yat3s.openai.api.model.TextCompletionApiRequestBody
7 |
8 | abstract class OpenAIClientBuilder(val apiKey: String) {
9 | abstract fun toRequestBody(prompt: String): OpenAIRequestBody
10 | }
11 |
12 | class TextCompletionBuilder(apiKey: String) : OpenAIClientBuilder(apiKey) {
13 | private var model: String = DEFAULT_MODEL_TEXT_COMPLETION
14 | private var temperature: Float = DEFAULT_TEMPERATURE
15 | private var maxTokens: Int = DEFAULT_MAX_TOKENS
16 |
17 | fun model(model: String) = apply {
18 | this.model = model
19 | }
20 |
21 | fun temperature(temperature: Float) = apply {
22 | this.temperature = temperature
23 | }
24 |
25 | fun maxTokens(maxTokens: Int) = apply {
26 | this.maxTokens = maxTokens
27 | }
28 |
29 | fun build(): OpenAIClient = OpenAIClient(this)
30 |
31 | override fun toRequestBody(prompt: String): OpenAIRequestBody {
32 | return TextCompletionApiRequestBody(
33 | prompt = prompt,
34 | model = model,
35 | temperature = temperature,
36 | maxTokens = maxTokens,
37 | )
38 | }
39 |
40 | companion object {
41 | private const val DEFAULT_MODEL_TEXT_COMPLETION = "text-davinci-003"
42 | private const val DEFAULT_TEMPERATURE = 1f
43 | private const val DEFAULT_MAX_TOKENS = 500
44 | }
45 | }
46 |
47 | class ImageGenerationBuilder(apiKey: String) : OpenAIClientBuilder(apiKey) {
48 | private var generateCount: Int = DEFAULT_IMAGE_COUNT
49 | private var imageSize: String = DEFAULT_IMAGE_SIZE
50 | private var imageResponseFormat: String = DEFAULT_IMAGE_RESPONSE_FORMAT
51 |
52 | fun generateCount(generateCount: Int) = apply {
53 | this.generateCount = generateCount
54 | }
55 |
56 | fun imageSize(imageSize: String) = apply {
57 | this.imageSize = imageSize
58 | }
59 |
60 | fun imageResponseFormat(imageResponseFormat: String) = apply {
61 | this.imageResponseFormat = imageResponseFormat
62 | }
63 |
64 | fun build(): OpenAIClient = OpenAIClient(this)
65 |
66 | override fun toRequestBody(prompt: String): OpenAIRequestBody {
67 | return ImageGenerationApiRequestBody(
68 | prompt = prompt,
69 | generateCount = generateCount,
70 | size = imageSize,
71 | responseFormat = imageResponseFormat,
72 | )
73 | }
74 |
75 | companion object {
76 | private const val DEFAULT_IMAGE_COUNT = 1
77 | private const val DEFAULT_IMAGE_SIZE = "1024x1024" // 256x256, 512x512
78 | private const val DEFAULT_IMAGE_RESPONSE_FORMAT = "url" // or b64_json
79 | }
80 | }
--------------------------------------------------------------------------------
/openai/src/main/java/com/yat3s/openai/api/model/ImageGenerationApiRequestBody.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api.model
2 |
3 | import com.google.gson.annotations.SerializedName
4 |
5 | internal data class ImageGenerationApiRequestBody internal constructor(
6 | val prompt: String,
7 | val size: String,
8 | @SerializedName("n") val generateCount: Int,
9 | @SerializedName("response_format") val responseFormat: String,
10 | ) : OpenAIRequestBody
--------------------------------------------------------------------------------
/openai/src/main/java/com/yat3s/openai/api/model/ImageGenerationApiResponse.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api.model
2 |
3 | data class ImageGenerationApiResponse(
4 | val created: Long? = null,
5 | val data: List? = null,
6 | var error: TextCompletionError? = null,
7 | )
8 |
9 | data class ImageGenerateImageData(
10 | val url: String
11 | )
--------------------------------------------------------------------------------
/openai/src/main/java/com/yat3s/openai/api/model/ImageGenerationResponse.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api.model
2 |
3 | data class ImageGenerationResponse(
4 | val urls: List? = null,
5 | val errorMessage: String? = null,
6 | val apiResponseBody: ImageGenerationApiResponse?,
7 | )
--------------------------------------------------------------------------------
/openai/src/main/java/com/yat3s/openai/api/model/OpenAIRequestBody.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api.model
2 |
3 | interface OpenAIRequestBody
--------------------------------------------------------------------------------
/openai/src/main/java/com/yat3s/openai/api/model/TextCompletionApiRequestBody.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api.model
2 |
3 | import com.google.gson.annotations.SerializedName
4 |
5 | internal data class TextCompletionApiRequestBody internal constructor(
6 | val prompt: String,
7 | val model: String,
8 | val temperature: Float,
9 | @SerializedName("max_tokens") val maxTokens: Int,
10 | ) : OpenAIRequestBody
--------------------------------------------------------------------------------
/openai/src/main/java/com/yat3s/openai/api/model/TextCompletionApiResponse.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api.model
2 |
3 | data class TextCompletionApiResponse(
4 | var id: String? = null,
5 | var created: String? = null,
6 | var choices: List? = null,
7 | var error: TextCompletionError? = null,
8 | )
9 |
10 | data class TextCompletionChoice(
11 | val text: String?,
12 | val index: Int?,
13 | val finish_reason: String?,
14 | )
--------------------------------------------------------------------------------
/openai/src/main/java/com/yat3s/openai/api/model/TextCompletionError.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api.model
2 |
3 | data class TextCompletionError(
4 | val message: String?,
5 | var type: String? = null,
6 | var code: String? = null,
7 | )
--------------------------------------------------------------------------------
/openai/src/main/java/com/yat3s/openai/api/model/TextCompletionResponse.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api.model
2 |
3 | data class TextCompletionResponse(
4 | val text: String? = null,
5 | val errorMessage: String? = null,
6 | val apiResponseBody: TextCompletionApiResponse?,
7 | )
--------------------------------------------------------------------------------
/openai/src/main/java/com/yat3s/openai/api/repository/OpenAIRepository.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api.repository
2 |
3 | import android.util.Log
4 | import com.google.gson.Gson
5 | import com.yat3s.openai.api.Config
6 | import com.yat3s.openai.api.model.*
7 | import com.yat3s.openai.api.service.OpenAIService
8 | import okhttp3.OkHttpClient
9 | import retrofit2.Response
10 | import retrofit2.Retrofit
11 | import retrofit2.converter.gson.GsonConverterFactory
12 | import java.util.concurrent.TimeUnit
13 |
14 | internal class OpenAIRepository {
15 |
16 | // TODO, replace with Hilt
17 | private var apiService: OpenAIService? = null
18 |
19 | suspend fun textCompletion(
20 | apiKey: String,
21 | requestBody: TextCompletionApiRequestBody,
22 | ): TextCompletionApiResponse? {
23 | getApiService()?.let {
24 | val response = it.textCompletion(
25 | "Bearer $apiKey",
26 | requestBody
27 | )
28 |
29 | if (!response.isSuccessful) {
30 | return handleError(response, TextCompletionApiResponse::class.java)
31 | }
32 |
33 | return response.body()
34 | }
35 |
36 | return null
37 | }
38 |
39 | suspend fun imageGeneration(
40 | apiKey: String,
41 | requestBody: ImageGenerationApiRequestBody,
42 | ): ImageGenerationApiResponse? {
43 | getApiService()?.let {
44 | val response = it.imageGeneration(
45 | "Bearer $apiKey",
46 | requestBody
47 | )
48 |
49 | if (!response.isSuccessful) {
50 | return handleError(response, ImageGenerationApiResponse::class.java)
51 | }
52 |
53 | return response.body()
54 | }
55 |
56 | return null
57 | }
58 |
59 | private fun handleError(response: Response, classOfT: Class): T? {
60 | return try {
61 | val errorString = response.errorBody()?.string()
62 | Gson().fromJson(errorString, classOfT)
63 | } catch (e: Exception) {
64 | Log.e(TAG, "textCompletion: unable to parse error body, $e")
65 | null
66 | }
67 | }
68 |
69 | // TODO, replace with Hilt
70 | private fun getApiService(): OpenAIService? {
71 | if (apiService == null) {
72 | val okHttpClient: OkHttpClient = OkHttpClient.Builder()
73 | .connectTimeout(60, TimeUnit.SECONDS)
74 | .readTimeout(60, TimeUnit.SECONDS)
75 | .writeTimeout(15, TimeUnit.SECONDS)
76 | .build()
77 | val retrofit: Retrofit = Retrofit.Builder()
78 | .baseUrl(Config.BASE_URL)
79 | .client(okHttpClient)
80 | .addConverterFactory(GsonConverterFactory.create())
81 | .build()
82 | apiService = retrofit.create(OpenAIService::class.java)
83 | }
84 |
85 | return apiService
86 | }
87 |
88 | companion object {
89 | private const val TAG = "OpenAIRepository"
90 | }
91 | }
--------------------------------------------------------------------------------
/openai/src/main/java/com/yat3s/openai/api/service/OpenAIService.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.api.service
2 |
3 | import com.yat3s.openai.api.Config
4 | import com.yat3s.openai.api.model.ImageGenerationApiRequestBody
5 | import com.yat3s.openai.api.model.ImageGenerationApiResponse
6 | import com.yat3s.openai.api.model.TextCompletionApiRequestBody
7 | import com.yat3s.openai.api.model.TextCompletionApiResponse
8 | import retrofit2.Response
9 | import retrofit2.http.Body
10 | import retrofit2.http.Header
11 | import retrofit2.http.Headers
12 | import retrofit2.http.POST
13 |
14 | internal interface OpenAIService {
15 | @Headers(
16 | "content-type: application/json",
17 | )
18 | @POST(Config.URL_TEXT_COMPLETION)
19 | suspend fun textCompletion(
20 | @Header("Authorization") authorization: String,
21 | @Body requestBody: TextCompletionApiRequestBody
22 | ): Response
23 |
24 | @Headers(
25 | "content-type: application/json",
26 | )
27 | @POST(Config.URL_IMAGE_GENERATION)
28 | suspend fun imageGeneration(
29 | @Header("Authorization") authorization: String,
30 | @Body requestBody: ImageGenerationApiRequestBody
31 | ): Response
32 | }
--------------------------------------------------------------------------------
/openai/src/test/java/com/yat3s/openai/openai/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.yat3s.openai.openai
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | google()
4 | mavenCentral()
5 | gradlePluginPortal()
6 | }
7 | }
8 | dependencyResolutionManagement {
9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
10 | repositories {
11 | google()
12 | mavenCentral()
13 | }
14 | }
15 | rootProject.name = "openai-api"
16 | include ':demo'
17 | include ':openai'
18 |
--------------------------------------------------------------------------------