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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-native-chatgpt-wrapper

v1.0.4

Published

react-native-chatgpt-wrapper is a TypeScript library designed to integrate OpenAI's API seamlessly into React Native applications. It provides a convenient way to interact with OpenAI's conversational assistant and handle real-time streaming responses usi

Downloads

16

Readme

React Native ChatGPT Wrapper

react-native-chatgpt-wrapper is a TypeScript library designed to integrate OpenAI's API seamlessly into React Native applications. It provides a convenient way to interact with OpenAI's conversational assistant and handle real-time streaming responses using Server-Sent Events (SSE).

This wrapper is ideal for building AI-powered chatbots and virtual assistants with real-time updates.

Features

  • Create Threads: Initialize conversational threads with OpenAI.
  • Add Messages: Send user queries to OpenAI's assistant.
  • Real-Time Streaming: Handle live, word-by-word responses from OpenAI using SSE.
  • Preprocess Messages: Extract links, process Markdown, and generate clickable content.

Installation

Install the package using npm:

bash npm install react-native-chatgpt-wrapper

Setup

Prerequisites

  • OpenAI API Key: Obtain an API key from OpenAI.
  • React Native Environment: Ensure your React Native app is set up.
  • Dependencies: This package requires axios and react-native-sse. Install required dependencies:

npm install axios react-native-sse

Usage

  • Import and Configure the Wrapper Set up constants to define your OpenAI API credentials:
import {
  createThread,
  addMessageToThread,
  runThread,
  preprocessMessage,
} from 'react-native-chatgpt-wrapper';

// Example: Define OpenAI constants
export const OPENAI_API_BASE_URL = 'https://api.openai.com/v1';
export const OPENAI_API_KEY = 'your-openai-api-key'; // Replace with your API key
export const ASSISTANT_ID = 'your-assistant-id'; // Replace with your Assistant ID
  • Create a Thread Initialize a new thread to start a conversation with the assistant.
const threadId = await createThread();
console.log('Thread created:', threadId);
  • Add Messages to the Thread Send user messages to the assistant.
await addMessageToThread(threadId, "Hello, how can I plan my retirement?");
  • Stream Responses Use the runThread function to handle real-time streaming responses from OpenAI.
const stream = runThread(threadId);

for await (const chunk of stream) {
  console.log('Received chunk:', chunk); // Handle each word or sentence as it's streamed
}
  • Preprocess Messages (Optional) The preprocessMessage utility extracts links and processes Markdown content:
const result = preprocessMessage("Visit [our site](https://example.com) for more info.");
console.log('Processed Message:', result);

Example Chat Component

Here’s a complete example of using react-native-chatgpt-wrapper to build a chat interface with real-time responses:

import React, { useEffect, useState } from 'react';
import { GiftedChat } from 'react-native-gifted-chat';
import { createThread, addMessageToThread, runThread } from 'react-native-chatgpt-wrapper';

const ChatGpt = () => {
  const [messages, setMessages] = useState([]);
  const [threadId, setThreadId] = useState<string | null>(null);

  useEffect(() => {
    const initializeThread = async () => {
      const newThreadId = await createThread();
      setThreadId(newThreadId);
    };
    initializeThread();
  }, []);

  const handleSend = async (newMessages = []) => {
    const userMessage = newMessages[0].text;
    setMessages(GiftedChat.append(messages, newMessages));

    if (!threadId) return;

    await addMessageToThread(threadId, userMessage);

    const botMessage = {
      _id: Date.now(),
      text: '',
      createdAt: new Date(),
      user: {
        _id: 2,
        name: 'Chatbot',
        avatar: 'https://shorturl.at/r9U',
      },
    };

    setMessages((prevMessages) => GiftedChat.append(prevMessages, [botMessage]));

    for await (const chunk of runThread(threadId)) {
      botMessage.text += chunk;
      setMessages((prevMessages) => {
        const updatedMessages = [...prevMessages];
        updatedMessages[0] = botMessage;
        return updatedMessages;
      });
    }
  };

  return (
    <GiftedChat
      messages={messages}
      onSend={(newMessages) => handleSend(newMessages)}
      user={{ _id: 1 }}
    />
  );
};

export default ChatGpt;

Contributing

Contributions are welcome! Please follow these steps:

  • Fork the repository.
  • Create a new branch for your feature or bugfix.
  • Commit your changes and submit a pull request.

License

This project is licensed under the MIT License.