├── .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 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 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 | ![image](https://user-images.githubusercontent.com/14801837/218364643-bc5990e1-5122-49a9-a7dc-38c860a0c0a9.png) 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 |