searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

清华智谱清言接入例子

2024-06-11 08:55:26
8
0

前提条件

  • 申请ApiKey:- 注册申请开通,即可获得 ApiKey
  • 运行环境:JDK 1.8+
  • 支持模型:chatGLM_6b_SSE、chatglm_lite、chatglm_lite_32k、chatglm_std、chatglm_pro、chatglm_turbo、glm-3-turbo、glm-4、glm-4v、cogview-3
  • maven pom - 已发布到Maven仓库
<dependency>
    <groupId>cn.bugstack</groupId>
    <artifactId>chatglm-sdk-java</artifactId>
    <version>2.1</version>
</dependency>

build.gradle中的配置

dependencies {
    implementation("cn.bugstack:chatglm-sdk-java:2.1")
    implementation("com.alibaba:fastjson:1.2.62")
}
<uses-permission android:name="android.permission.INTERNET"/>

请求服务OpenAiService

class OpenAiService : Service() {

    private var handlerThread: HandlerThread? = null
    private var threadHandler: Handler? = null
    private var mainHandler: Handler? = null
    private val binder = MyBinder()
    private var openAiSession: OpenAiSession? = null

    inner class MyBinder : Binder() {
        fun getService(): OpenAiService {
            return this@OpenAiService
        }
    }

    override fun onBind(p0: Intent?): IBinder? {
        handlerThread = object : HandlerThread("openai") {
            override fun onLooperPrepared() {
                super.onLooperPrepared()
                threadHandler = handlerThread?.looper?.let { Handler(it) }
            }
        }
        handlerThread?.start()
        openAiSessionFactory() //openSession
        return binder
    }

    override fun unbindService(conn: ServiceConnection) {
        super.unbindService(conn)
    }

    public fun setMainHandler(mainHandler: Handler) {
        this.mainHandler = mainHandler
    }

    public fun chat(question: String) {
        threadHandler?.let {
            runnable.question = question
            it.post(runnable)
        }
    }

    private val runnable = object : Runnable {
        var question: String? = null
        override fun run() {
            question?.let { completions(it) }
        }
    }



    private fun completions(question: String) {
        val countDownLatch = CountDownLatch(1)
        val request = ChatCompletionRequest()
        request.model = Model.GLM_3_5_TURBO
        request.isIncremental = false
        request.isCompatible = true
        request.tools = object : ArrayList<ChatCompletionRequest.Tool>() {
            init {
                add(
                    ChatCompletionRequest.Tool.builder()
                        .type(ChatCompletionRequest.Tool.Type.web_search)
                        .webSearch(
                            ChatCompletionRequest.Tool.WebSearch.builder().enable(true)
                                .searchQuery("小傅哥").build()
                        )
                        .build()
                )
            }
        }
        request.prompt = object : ArrayList<ChatCompletionRequest.Prompt>() {
            init {
                add(
                    ChatCompletionRequest.Prompt.builder()
                        .role(Role.user.getCode())
                        .content(question)
                        .build()
                )
            }
        }

        // 请求
        openAiSession?.completions(request, object : EventSourceListener() {
            override fun onEvent(
                eventSource: EventSource,
                id: String?,
                type: String?,
                data: String
            ) {
                val response = JSON.parseObject(data, ChatCompletionResponse::class.java)
                //log.info("测试结果 onEvent:{}", response.data)
                val msg = Message()
                msg.data = Bundle()
                msg.data.putString("chat", response.data)
                mainHandler?.sendMessage(msg)

                // type 消息类型,add 增量,finish 结束,error 错误,interrupted 中断
                if (EventType.finish.getCode() == type) {
                    val meta =
                        JSON.parseObject(response.meta, ChatCompletionResponse.Meta::class.java)
                    //log.info("[输出结束] Tokens {}", JSON.toJSONString(meta))
                }
            }

            override fun onClosed(eventSource: EventSource) {
                //log.info("对话完成")
                countDownLatch.countDown()
            }

            override fun onFailure(eventSource: EventSource, t: Throwable?, response: Response?) {
                //log.info("对话异常")
                countDownLatch.countDown()
            }
        })
        // 等待
        try {
            countDownLatch.await()
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
    }

    private fun openAiSessionFactory() {
        val configuration = Configuration()
        configuration.apiHost = "httpz://open.bigmodel.cn/"
        configuration.setApiSecretKey("7632980a7b06462ef62488c6b774b1f8.Hr0p2jqjW1KZWVUx")
        configuration.level = HttpLoggingInterceptor.Level.BODY
        val factory = DefaultOpenAiSessionFactory(configuration)
        openAiSession = factory.openSession()
    }
}

UI界面

class MainActivity : AppCompatActivity() {

    private var mService: OpenAiService? = null
    private var isBound: Boolean = false
    private var contentTv: TextView? = null
    private lateinit var mainHandler: Handler
    private var sb: StringBuilder = java.lang.StringBuilder()

    private val connection = object : ServiceConnection {
        override fun onServiceConnected(className: ComponentName?, service: IBinder?) {
            val binder = service as OpenAiService.MyBinder
            mService = binder.getService()
            //set mainHandler
            mService?.setMainHandler(mainHandler)
            isBound = true
        }

        override fun onServiceDisconnected(className: ComponentName?) {
            isBound = false
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // 初始化 mainHandler
        mainHandler = object : Handler(Looper.getMainLooper()) {
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                val str = msg.data.getString("chat")
                sb.append(str)
                contentTv?.text = sb.toString()
            }
        }
        val intent = Intent(this, OpenAiService::class.java)
        bindService(intent, connection, Context.BIND_AUTO_CREATE)

        //Views
        contentTv = findViewById(R.id.response_content)
        val requestEd:EditText = findViewById(R.id.et_input)
        val btnAsk:Button = findViewById(btn_ask)
        btnAsk.setOnClickListener {
            val inputText = requestEd.text.toString()
            sb.clear()
            mService?.chat(inputText)
        }
    }
}

效果

img-20240315093253.png

0条评论
0 / 1000
hi_long
15文章数
0粉丝数
hi_long
15 文章 | 0 粉丝
hi_long
15文章数
0粉丝数
hi_long
15 文章 | 0 粉丝
原创

清华智谱清言接入例子

2024-06-11 08:55:26
8
0

前提条件

  • 申请ApiKey:- 注册申请开通,即可获得 ApiKey
  • 运行环境:JDK 1.8+
  • 支持模型:chatGLM_6b_SSE、chatglm_lite、chatglm_lite_32k、chatglm_std、chatglm_pro、chatglm_turbo、glm-3-turbo、glm-4、glm-4v、cogview-3
  • maven pom - 已发布到Maven仓库
<dependency>
    <groupId>cn.bugstack</groupId>
    <artifactId>chatglm-sdk-java</artifactId>
    <version>2.1</version>
</dependency>

build.gradle中的配置

dependencies {
    implementation("cn.bugstack:chatglm-sdk-java:2.1")
    implementation("com.alibaba:fastjson:1.2.62")
}
<uses-permission android:name="android.permission.INTERNET"/>

请求服务OpenAiService

class OpenAiService : Service() {

    private var handlerThread: HandlerThread? = null
    private var threadHandler: Handler? = null
    private var mainHandler: Handler? = null
    private val binder = MyBinder()
    private var openAiSession: OpenAiSession? = null

    inner class MyBinder : Binder() {
        fun getService(): OpenAiService {
            return this@OpenAiService
        }
    }

    override fun onBind(p0: Intent?): IBinder? {
        handlerThread = object : HandlerThread("openai") {
            override fun onLooperPrepared() {
                super.onLooperPrepared()
                threadHandler = handlerThread?.looper?.let { Handler(it) }
            }
        }
        handlerThread?.start()
        openAiSessionFactory() //openSession
        return binder
    }

    override fun unbindService(conn: ServiceConnection) {
        super.unbindService(conn)
    }

    public fun setMainHandler(mainHandler: Handler) {
        this.mainHandler = mainHandler
    }

    public fun chat(question: String) {
        threadHandler?.let {
            runnable.question = question
            it.post(runnable)
        }
    }

    private val runnable = object : Runnable {
        var question: String? = null
        override fun run() {
            question?.let { completions(it) }
        }
    }



    private fun completions(question: String) {
        val countDownLatch = CountDownLatch(1)
        val request = ChatCompletionRequest()
        request.model = Model.GLM_3_5_TURBO
        request.isIncremental = false
        request.isCompatible = true
        request.tools = object : ArrayList<ChatCompletionRequest.Tool>() {
            init {
                add(
                    ChatCompletionRequest.Tool.builder()
                        .type(ChatCompletionRequest.Tool.Type.web_search)
                        .webSearch(
                            ChatCompletionRequest.Tool.WebSearch.builder().enable(true)
                                .searchQuery("小傅哥").build()
                        )
                        .build()
                )
            }
        }
        request.prompt = object : ArrayList<ChatCompletionRequest.Prompt>() {
            init {
                add(
                    ChatCompletionRequest.Prompt.builder()
                        .role(Role.user.getCode())
                        .content(question)
                        .build()
                )
            }
        }

        // 请求
        openAiSession?.completions(request, object : EventSourceListener() {
            override fun onEvent(
                eventSource: EventSource,
                id: String?,
                type: String?,
                data: String
            ) {
                val response = JSON.parseObject(data, ChatCompletionResponse::class.java)
                //log.info("测试结果 onEvent:{}", response.data)
                val msg = Message()
                msg.data = Bundle()
                msg.data.putString("chat", response.data)
                mainHandler?.sendMessage(msg)

                // type 消息类型,add 增量,finish 结束,error 错误,interrupted 中断
                if (EventType.finish.getCode() == type) {
                    val meta =
                        JSON.parseObject(response.meta, ChatCompletionResponse.Meta::class.java)
                    //log.info("[输出结束] Tokens {}", JSON.toJSONString(meta))
                }
            }

            override fun onClosed(eventSource: EventSource) {
                //log.info("对话完成")
                countDownLatch.countDown()
            }

            override fun onFailure(eventSource: EventSource, t: Throwable?, response: Response?) {
                //log.info("对话异常")
                countDownLatch.countDown()
            }
        })
        // 等待
        try {
            countDownLatch.await()
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
    }

    private fun openAiSessionFactory() {
        val configuration = Configuration()
        configuration.apiHost = "httpz://open.bigmodel.cn/"
        configuration.setApiSecretKey("7632980a7b06462ef62488c6b774b1f8.Hr0p2jqjW1KZWVUx")
        configuration.level = HttpLoggingInterceptor.Level.BODY
        val factory = DefaultOpenAiSessionFactory(configuration)
        openAiSession = factory.openSession()
    }
}

UI界面

class MainActivity : AppCompatActivity() {

    private var mService: OpenAiService? = null
    private var isBound: Boolean = false
    private var contentTv: TextView? = null
    private lateinit var mainHandler: Handler
    private var sb: StringBuilder = java.lang.StringBuilder()

    private val connection = object : ServiceConnection {
        override fun onServiceConnected(className: ComponentName?, service: IBinder?) {
            val binder = service as OpenAiService.MyBinder
            mService = binder.getService()
            //set mainHandler
            mService?.setMainHandler(mainHandler)
            isBound = true
        }

        override fun onServiceDisconnected(className: ComponentName?) {
            isBound = false
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // 初始化 mainHandler
        mainHandler = object : Handler(Looper.getMainLooper()) {
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                val str = msg.data.getString("chat")
                sb.append(str)
                contentTv?.text = sb.toString()
            }
        }
        val intent = Intent(this, OpenAiService::class.java)
        bindService(intent, connection, Context.BIND_AUTO_CREATE)

        //Views
        contentTv = findViewById(R.id.response_content)
        val requestEd:EditText = findViewById(R.id.et_input)
        val btnAsk:Button = findViewById(btn_ask)
        btnAsk.setOnClickListener {
            val inputText = requestEd.text.toString()
            sb.clear()
            mService?.chat(inputText)
        }
    }
}

效果

img-20240315093253.png

文章来自个人专栏
Android
15 文章 | 1 订阅
0条评论
0 / 1000
请输入你的评论
0
0