npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-gpt

v3.2.8

Published

react native gpt stream api

Downloads

11

Readme

Module Name: react-native-gpt

Description:

The react-native-gpt module enables convenient use of gpt models in React Native applications. This module provides asynchronous features for managing the connection to the OpenAI service, allowing you to easily generate and stream results from GPT-3, GPT-3.5-turbo, GPT-4 model.

Installation:

Using npm:

npm install react-native-gpt

Use:.

Import function from module:

import react_native_gpt from "react-native-gpt";

Then, you can call this function with the required arguments. For example:

react_native_gpt({
  API_KEY: "your-openai-api-key",
  model: "gpt-4",
  system: "hi, I am a chat assistant",
  stream: true,
  message: "how's the weather today?",
  setResult: (result) => console.log(result),
}).then(() => console.log("Done"));

API Documentation:

The react_native_gpt function accepts a configuration object with the following fields:

  • API_KEY (required) - API key for authorization with the OpenAI service.
  • model (optional) - The model to be used for generation. The default is "gpt-3.5-turbo".
  • system (optional) - The system message to be passed to the model. The default is "you are a voice assistant".
  • stream (optional) - Whether the results should be streamed. The default is true.
  • message (optional) - A message from the user to be passed to the model. Default is this message is empty.
  • messages (optional) - An array of messages to pass to the model. Each message is an object with two fields: role ("system" or "user") and content. By default, this array contains two messages: a system message ("you are a voice assistant") and a user message ("this message is empty").
  • setResult (required) - A function that will be called with each new part of the result from the model. This function should take one argument, which is the new part of the result.

Example of use with conversation (messages):

Below is an example of using the module in a React Native component:

import React, { useState, useEffect } from "react";
import { Button, Text, View, TextInput, StyleSheet } from "react-native";
import react_native_gpt from "react-native-gpt";

const API_KEY = "your-api-key";

const Chat = () => {
  const [text, setText] = useState(""); // State to store the entered text
  const [result, setResult] = useState(""); // State to store the assistant's response
  const [history, setHistory] = useState([
    {
      role: "system",
      content: "hi, I am a chat assistant",
    },
  ]); // State to store the message history

  useEffect(() => {
    processText("Hello assistant!"); // Calling the processText function on component mount
  }, []);

  const processText = async (msg) => {
    setText(""); // Clearing the text input
    const updatedHistory = [...history, { role: "user", content: msg }]; // Updating the message history with a new user message

    try {
      await react_native_gpt({
        API_KEY: API_KEY,
        model: "gpt-4",
        system: "hi, I am a chat assistant",
        stream: true,
        messages: updatedHistory,
        setResult: (result) => {
          setResult(result); // Updating the state with the assistant's response
          setHistory((prevHistory) => [
            ...prevHistory,
            { role: "assistant", content: result },
          ]); // Updating the message history with the assistant's response
        },
      });
    } catch (error) {
      console.error(error); // Error handling
      setResult("An error occurred. Please try again."); // Setting an error message as the response
    }
  };

  return (
    <View style={styles.container}>
      {/* Displaying the message history */}
      <View style={styles.chatbox}>
        {history.map((message, index) => (
          <Text key={index} style={styles[message.role]}>
            {`${message.role}: ${message.content}`}
          </Text>
        ))}
        {/* Displaying the assistant's response */}
        <Text style={styles.assistant}>{`assistant: ${result}`}</Text>
      </View>
      {/* Text input for entering messages */}
      <TextInput
        style={styles.input}
        onChangeText={(text) => setText(text)}
        value={text}
        placeholder="Enter something..."
      />
      {/* Button to send the message */}
      <Button title="Send" onPress={() => processText(text)} />
    </View>
  );
};

export default Chat;

video:

https://github.com/Mike-Csta/react-native-gpt/assets/92047998/93229938-dcba-4e52-aa7e-389884af57f9