We connect ChatGPT to the application

Do you want to connect ChatGPT to your app?
No problem :cowboy_hat_face:, see how to do it using a low-code editor :point_down: :point_down: :point_down:

  1. Register on the OpenAI platform and get API keys
  2. In the low-code editor, add the following code:

Custom TypeScript

Import section

export const gptModels = [
  {
      'model':'text-davinci-003',
      'description':'Most capable GPT-3 model. Can do any task the other models can do, often with higher quality, longer output and better instruction-following. Also supports inserting completions within text.',
      'maxRequest':'4,000 tokens'
  },
  {
      'model':'text-curie-001',
      'description':'Very capable, but faster and lower cost than Davinci.',
      'maxRequest':'2,048 tokens'
  },
  {
      'model':'text-babbage-001',
      'description':'Capable of straightforward tasks, very fast, and lower cost.',
      'maxRequest':'2,048 tokens'
  },
  {
      'model':'text-ada-001',
      'description':'Capable of very simple tasks, usually the fastest model in the GPT-3 series, and lowest cost.',
      'maxRequest':'2,048 tokens'
  }
];


export interface ResponseModel  {
  id:string;
  object: string;
  created:number;
  model:string;
  choices:choice[];
  modelUsage:usage;
}

export interface choice {
  text:string;
  index:number;
  logprobs:any;
  finish_reason:string;
}

export interface usage {
  prompt_tokens:number;
  completion_tokens: number;
  total_tokens:number;
}

export interface ChatWithBot {
  person: string;
  response: string;
  cssClass:string;
}
Variables

chatConversation: ChatWithBot[]=[];
  response!: ResponseModel | undefined;
  gptModels = gptModels
  promptText = '';
  promptText2 = '';
  showSpinner = false;
User functions

checkResponse() {
    this.promptText = this.promptText2
    this.pushChatContent(this.promptText,'You','person');
    this.promptText2 = '';
    this.invokeGPT();
  }


  pushChatContent(content:string, person:string, cssClass:string) {
    const chatToPush: ChatWithBot = { person:person, response:content, cssClass:cssClass};
    this.chatConversation.push(chatToPush);
  }


  getText(data:string) {
    return data.split('\n').filter(f=>f.length>0);
  }

  async invokeGPT() {
   

    if(this.promptText.length<2)
    return;

    

    try{
      this.response = undefined;
      let configuration = new Configuration({apiKey: 'YOUR_API_KEY'});
      let openai = new OpenAIApi(configuration);

      let requestData={
        model: 'text-davinci-003',//'text-davinci-003',//"text-curie-001",
        prompt: this.promptText,//this.generatePrompt(animal),
        temperature: 0.95,
        max_tokens: 150,
        top_p: 1.0,
        frequency_penalty: 0.0,
        presence_penalty: 0.0,
      };
      this.showSpinner = true;
      let apiResponse =  await openai.createCompletion(requestData);

      this.response = apiResponse.data as ResponseModel;
      this.pushChatContent(this.response.choices[0].text.trim(),'Mr Bot','bot');

      this.showSpinner = false;
    }catch(error:any) {
      this.showSpinner = false;
      // Consider adjusting the error handling logic for your use case
      if (error.response) {
        console.error(error.response.status, error.response.data);
        
      } else {
        console.error(`Error with OpenAI API request: ${error.message}`);
        
      }
    }
  }

Note that you need to replace YOUR_API_KEY with your own API key, which you can get by signing up for a free account at OpenAI.

Content

Header
<ion-header mode="ios">
  <ion-toolbar>
    <ion-title>ChatGPT</ion-title>
  </ion-toolbar>
</ion-header>
Content before
<div class="chat-wrapper">
    <div *ngFor="let item of chatConversation">
      <ion-card [ngClass]="item.cssClass" mode="ios" [color]="item.cssClass=='bot'?'color45300':'color6792'">
        <ion-card-header>
          <ion-card-title>{{item.person}}</ion-card-title>
        </ion-card-header>
      
        <ion-card-content>
          {{getText(item.response)}}
        </ion-card-content>
      </ion-card>
    </div>
    <ion-spinner name="dots" *ngIf="showSpinner"></ion-spinner>
  </div>
Footer
<ion-footer>
  <ion-toolbar>
    <ion-item lines="none">
      <ion-input type="text" placeholder="Message" autocomplete="on" autocorrect="on"  [(ngModel)]="promptText2" (keyup.enter)="checkResponse()"></ion-input>
      <ion-button fill="clear" slot="end" (click)="checkResponse()">
        <ion-icon icononly name="send-outline"></ion-icon>
      </ion-button>
    </ion-item>
  </ion-toolbar>
</ion-footer>

Done, save and generate the app build

2 Likes

This is awesome and works. But I think this is the right time we get more detailed documentation on what all these sections in low- code mean and how to use them. That is what holding us back from using the full potential of Moxly.

1 Like

Hi,

initially, the low-code editor is designed for people who have programming skills. The idea was to provide open source code to quickly develop your own functions.

Each editor window has a split screen (application), it is easy for a programmer to navigate this, so we did not expect to write any documentation.

But there is good news, we will simplify the editor a bit, add more examples so that you can also write your code without programmers, we already have thoughts on how to do this and we will implement it soon :cowboy_hat_face:

1 Like

Great looking forward to it.

great, looking forward, tq

Is it a possibility that you may integrate ChatGPT/Open AI Codex as a tool that users can type in and it write code for them to use in their apps? Users or SaaS owners just enter their api key and voila its now usable, similar to google sheets or airtable integration?

we plan to add this

integration is not a problem, you need to create forms and link them to certain functions where users can use this code. It doesn’t have to be just an editor with answers

1 Like