diff --git a/ollama/build.gradle b/ollama/build.gradle index c5272da..4a162b5 100644 --- a/ollama/build.gradle +++ b/ollama/build.gradle @@ -46,6 +46,8 @@ android { dependencies { api fileTree(dir: 'libs', include: ['*.jar']) + api 'com.squareup.okhttp3:okhttp:4.4.1' + // 吐司类库 api 'com.github.getActivity:ToastUtils:10.5' diff --git a/ollama/build.properties b/ollama/build.properties index 24852bd..ae139c3 100644 --- a/ollama/build.properties +++ b/ollama/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Thu Mar 27 11:38:18 GMT 2025 +#Thu Mar 27 12:33:37 GMT 2025 stageCount=0 libraryProject= baseVersion=15.0 publishVersion=15.0.0 -buildCount=12 +buildCount=17 baseBetaVersion=15.0.1 diff --git a/ollama/src/main/AndroidManifest.xml b/ollama/src/main/AndroidManifest.xml index 491eadb..f9dfa9c 100644 --- a/ollama/src/main/AndroidManifest.xml +++ b/ollama/src/main/AndroidManifest.xml @@ -4,6 +4,9 @@ xmlns:tools="http://schemas.android.com/tools" package="cc.winboll.studio.ollama"> + + + - + \ No newline at end of file diff --git a/ollama/src/main/java/cc/winboll/studio/ollama/MainActivity.java b/ollama/src/main/java/cc/winboll/studio/ollama/MainActivity.java index 3876559..29a23b5 100644 --- a/ollama/src/main/java/cc/winboll/studio/ollama/MainActivity.java +++ b/ollama/src/main/java/cc/winboll/studio/ollama/MainActivity.java @@ -2,11 +2,18 @@ package cc.winboll.studio.ollama; import android.app.Activity; import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; import cc.winboll.studio.libappbase.LogView; public class MainActivity extends Activity { LogView mLogView; + TextView mtvMeaasge; + EditText metAsk; + Button mbtSend; @Override protected void onCreate(Bundle savedInstanceState) { @@ -15,6 +22,10 @@ public class MainActivity extends Activity { mLogView = findViewById(R.id.logview); mLogView.start(); + + mtvMeaasge = findViewById(R.id.message_tv); + metAsk = findViewById(R.id.ask_et); + mbtSend = findViewById(R.id.send_bt); } @Override @@ -22,4 +33,10 @@ public class MainActivity extends Activity { super.onResume(); mLogView.start(); } + + public void onSend(View view) { + OllamaClient.SyncAskThread thread = new OllamaClient.SyncAskThread(mtvMeaasge.getText().toString()); + thread.start(); + mtvMeaasge.setText(""); + } } diff --git a/ollama/src/main/java/cc/winboll/studio/ollama/OllamaClient.java b/ollama/src/main/java/cc/winboll/studio/ollama/OllamaClient.java new file mode 100644 index 0000000..f31a602 --- /dev/null +++ b/ollama/src/main/java/cc/winboll/studio/ollama/OllamaClient.java @@ -0,0 +1,108 @@ +package cc.winboll.studio.ollama; + +/** + * @Author ZhanGSKen@AliYun.Com + * @Date 2025/03/27 19:55:28 + * @Describe 简单Http协议访问客户端 + */ +import java.io.IOException; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONException; +import cc.winboll.studio.libappbase.LogUtils; + +public class OllamaClient { + + public static final String TAG = "OllamaClient"; + + //private static final String API_BASE_URL = "http://localhost:11434"; + private static final String API_BASE_URL = "https://ollama.winboll.cc"; + private static final OkHttpClient client = new OkHttpClient(); + + // 1. 生成文本示例 + static void generateText(String prompt, String model) { + String url = API_BASE_URL + "/api/generate"; + try { + JSONObject payload = new JSONObject() + .put("model", model) + .put("prompt", prompt) + .put("temperature", 0.7) + .put("max_tokens", 200); + + Request request = new Request.Builder() + .url(url) + .post(RequestBody.create(payload.toString(), MediaType.get("application/json"))) + .build(); + + Response response = client.newCall(request).execute(); + if (response.isSuccessful()) { + String result = response.body().string(); + JSONObject json = new JSONObject(result); + LogUtils.d(TAG, "生成结果: " + json.getString("response")); + //System.out.println("生成结果: " + json.getString("response")); + } else { + LogUtils.d(TAG, "请求失败: " + response.code()); + //System.out.println("请求失败: " + response.code() + " " + response.message()); + } + } catch (JSONException|IOException e) { + LogUtils.d(TAG, e, Thread.currentThread().getStackTrace()); + } + } + + // 2. 获取模型列表示例 + static void getModelList() { + String url = API_BASE_URL + "/api/models"; + LogUtils.d(TAG, "url : " + url); + Request request = new Request.Builder().url(url).build(); + + try { + Response response = client.newCall(request).execute(); + if (response.isSuccessful()) { + JSONArray models = new JSONArray(response.body().string()); + //System.out.println("可用模型列表:"); + LogUtils.d(TAG, "可用模型列表:"); + for (int i = 0; i < models.length(); i++) { + JSONObject model = models.getJSONObject(i); + LogUtils.d(TAG, "- " + model.getString("name") + " (" + model.getString("size") + ")"); + //System.out.println("- " + model.getString("name") + " (" + model.getString("size") + ")"); + } + } else { + LogUtils.d(TAG, "获取模型列表失败: " + response.code()); + //System.out.println("获取模型列表失败: " + response.code()); + } + } catch (JSONException | IOException e) { + LogUtils.d(TAG, e, Thread.currentThread().getStackTrace()); + } + } + + static void unittest(String ask) { + // 获取模型列表 + getModelList(); + + // 生成文本 + generateText(ask, "llama2"); + } + + public static class SyncAskThread extends Thread { + String ask; + public SyncAskThread(String ask) { + this.ask = ask; + } + + @Override + public void run() { + super.run(); + LogUtils.d(TAG, "run() start."); + unittest(this.ask); + LogUtils.d(TAG, "run() end."); + } + + } +} + + diff --git a/ollama/src/main/res/layout/activity_main.xml b/ollama/src/main/res/layout/activity_main.xml index 0f63aeb..39dd559 100644 --- a/ollama/src/main/res/layout/activity_main.xml +++ b/ollama/src/main/res/layout/activity_main.xml @@ -10,13 +10,45 @@ android:layout_width="match_parent" android:layout_height="0dp" android:gravity="center_vertical|center_horizontal" - android:layout_weight="1.0"> + android:layout_weight="1.0" + android:orientation="vertical"> - + + + + + + + android:gravity="center_vertical"> + + + +