Giter Site home page Giter Site logo

components's Introduction

my components

Here are some components that I made for my projects.

Current components:

  • Chatui (mod from @chat-ui/vue3, add some features)
  • Openai Chat (support openai api and stream transmition)

Support render markdown text.
Tips: openai chat only adapt for chatanywhere, deepseek and one api project, if you have another api provider, I don't guarantee it will work.

Openai Chat

usage

npm install @heyzayn/components

Normally, it will automatically install all the dependencies, if not, you must install them manually.

npm install @kangc/v-md-editor@next
# the addition `@next` is necessary, because @next version is for vue3
npm install highlight.js

Chatui

Type hints

export type ChatProps = {
  bgColorIcon?: string;
  margin?: string;
  fillColorIcon?: string;
  width?: string;
  height?: string;
  boxShadow?: string;
  headerHeight?: string;
  bgColorHeader?: string;
  textColorHeader?: string;
  offline?: boolean;
  colorOffline?: string;
  colorOnline?: string;
  bgColorChat?: string;
  bgColorMessageChatbot?: string;
  bgColorMessagePerson?: string;
  bgColorMessageTimestamp?: string;
  textColorMessageChatbot?: string;
  textColorMessagePerson?: string;
  textColorMessageTimestamp?: string;
  chat: message[];
  onSend: (message: string) => void;
  inputHeight?: string;
  bgColorInput?: string;
  textColorInput?: string;
  inputPlaceholder?: string;
  botName?: string;
};
<script setup lang="ts">
import { ref } from "vue";
import { Chat, type message } from "@heyzayn/components";
//如果样式不正确,导入样式文件
// import '@heyzayn/components/index.css'
import getResponse from "./utils/Boredapi.ts"; // import your own api

const chatData = ref<message[]>([
  {
    message: "你好,我是AI助手。",
    type: "chatbot",
    timestamp: formatAMPM(new Date()),
  },
]);
function formatAMPM(date: Date) {
  const hours = date.getHours();
  const minutes = date.getMinutes();
  const ampm = hours >= 12 ? "PM" : "AM";
  let r_hours = hours % 12;
  r_hours = hours ? hours : 12; // the hour '0' should be '12'
  const r_minutes = minutes < 10 ? "0" + minutes : minutes;
  const strTime = r_hours + ":" + r_minutes + " " + ampm;
  return strTime;
}
async function handleSend(input: string) {
  chatData.value.push({
    message: input,
    type: "person",
    timestamp: formatAMPM(new Date()),
  });
  const response = await getResponse();
  chatData.value.push(response);
}
</script>

<template>
  <div class="w-full h-full">
    <Chat :chat="chatData" bot-name="AI助手" :on-send="handleSend" />
  </div>
  <HelloWorld msg="Hello World" />
</template>

<style scoped>
div {
  width: auto;
}
</style>

Openai Chat

Type hints

export type OpenaiChatProps = Omit<ChatProps, "onSend" | "chat"> & {
  openaikey: string;
  proxyUrl?: string;
  systemMessage?: string;
  params?: Parameters<typeof setParams>[0];
  firstMessage?: string;
};

The params type should be like part of this(program will merge the params with default params):

let params = {
    model: "gpt-3.5-turbo",
    max_tokens: 4000,
    temperature: 0.2,
    stream: true
}

The proxyUrl is your openai api url, if you don't have one, you can use the default one(https://api.chatanywhere.com.cn/v1/chat/completions), you don't need to set it if you use the default one.

The key should match your openai api url, ask your openai api provider for it.

Recommend to use the chatanywhere to get a test key, then you only need to set the key in this component .

Example

You can just use it with your own openai key, it support stream transmition and system message(you should set props if you want to use it).

<script setup lang="ts">
import { ref } from "vue";
import { OpenaiChat, type message } from "@heyzayn/components";

const key = import.meta.env.VITE_OPENAI_KEY;
</script>

<template>
  <div class="w-full h-full">
    <OpenaiChat :openaikey="key" bot-name="AI助手" />
  </div>
</template>

<style scoped>
</style>

components's People

Contributors

dongsy-arch avatar

Watchers

东生于 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.