blinq/openai 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

blinq/openai

Composer 安装命令:

composer require blinq/openai

包简介

PHP library wrapper for the OpenAI API.

README 文档

README

(Written by ChatGPT)

This is a library for interacting with Language Learning Models (LLMs), built with a focus on AI-driven chat functionalities. The library currently supports OpenAI's GPT-3.5-turbo, but it can be extended to support other LLMs as well.

Features

  • Cross-API Compatibility: The library is designed to work with different language learning models, including OpenAI's GPT-3.5-turbo.

  • Ease of Use: With simple configuration and intuitive API, this library enables developers to easily interact with complex language learning models.

  • Streaming Support: The library offers streaming capabilities that allow real-time interactions with language learning models.

  • Chat History: The library maintains a history of the chat session which can be retrieved and analyzed anytime.

  • You can add support for other AI providers by extending the ApiClient class and implementing the required methods.

  • Error Handling: The library provides robust error handling to deal with potential issues during interactions with language learning models.

  • Documentation: Comprehensive documentation is available to guide developers in using the library.

  • Community Support: Issues and feature requests can be raised in the GitHub repository, and the active community is open to discussions and improvements.

  • License: The library is open source and is licensed under the MIT License.

  • Support for Future AI Models: The architecture of the library is designed to support any future language learning models seamlessly.

  • It is clean, well-structured, and includes comments for clarity.

Getting Started

Firstly, you need to install the library in your PHP project.

composer require blinq/openai

Usage

Chat with OpenAI's GPT-3.5-turbo

You need to initialize a client object using ApiClient configuration. The following example demonstrates a chat session with OpenAI's GPT-3.5-turbo.

<?php

use Blinq\LLM\Config\ApiConfig;
use Blinq\LLM\Entities\ChatMessage;
use Blinq\LLM\Client;

$config = new ApiConfig('openai', 'your-api-key');
$client = new Client($config);

// Set the system message. This is optional.
$client->setSystemMessage("You are a nice chatbot");

// User message
$client->chat("Hello, how are you?");

// Get the last message
$message = $client->getLastMessage();

echo $message->content; // Prints "I am fine, thank you. How are you?"

// Get the chat history
$history = $client->getHistory();

foreach ($history as $message) {
    echo $message->content;
}

The client by default holds a history of the chat session. You can retrieve the history using the getHistory() method. The history is an array of ChatMessage objects, which have the following properties:

  • content: The content of the message
  • role: The role of the message (user or system)

You can reset the chat history using the resetHistory() method.

Or use the getLastMessageAndClearHistory() method to get the last message and clear the history.

Chat with Stream

If the streaming option is used, the following example shows how to add a handler to deal with the stream of chat messages.

<?php

use Blinq\LLM\Config\ApiConfig;
use Blinq\LLM\Entities\ChatStream;
use Blinq\LLM\Client;

$config = new ApiConfig('openai', 'your-api-key');
$client = new Client($config);

$client->addStreamHandler(function (ChatStream $stream) {
    // Handle the stream data
    echo $stream->getMessage()?->content; // Prints the partial message content
    
    // The $stream object has a 'done' property to check if the stream is done
    if ($stream->done) {
        // Do something when the stream is done
    }
});

$client->chat("Hello, how are you?", "user", ['stream' => true]);

Customization

The ApiClient class is abstract and can be extended to create custom drivers for different APIs.

The current implementation is for OpenAI, but if you'd like to use a different AI provider, you can create a new class that extends ApiClient, and implement the required methods (chat(), completion(), etc.).

For any issues or additional features, feel free to open an issue in the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for details.

blinq/openai 适用场景与选型建议

blinq/openai 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.11k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2023 年 06 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 blinq/openai 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 blinq/openai 我们能提供哪些服务?
定制开发 / 二次开发

基于 blinq/openai 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 17.11k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-19