Write your own MyGPT app

After chatGPT became popular, many players at home and abroad rolled up their sleeves and started working in full swing.

With the help of open source GPT, you can easily have your own exclusive GPT. It is still very useful to pretend to be cool, and it can also be used to catch up with the trend of chatGPT.

Use ANYGPT here to create your own GPT, AnyGPT API developer documentation · Yuque

The AnyGPT server code is open source, and they also provide their own API for everyone to use. Friends who have a server can deploy it themselves, those who do not have a server can use their API, and those who need more advanced services can buy their membership.

Once you have a GPT open, you can just make an APP interface. used here

implementation 'me.himanshusoni.chatmessageview:chat-message-view:1.0.2'

To create a WeChat chat window, you can write it yourself. There is a framework and it is very fast to use. Simple layout, a list for displaying messages, a button for recording, providing voice chat, an input box for entering chat content, and a send button to send:

<RelativeLayout
                android:id="@+id/widget_layout_meihua"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_above="@+id/send_message_layout"
                    android:layout_marginStart="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="10dp"
                    android:clipToPadding="false"
                    android:divider="@null"
                    android:paddingTop="8dp" />

                <LinearLayout
                    android:id="@+id/send_message_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:background="#ffdddddd"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <ImageView
                        android:id="@+id/iv_image"
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:alpha=".5"
                        android:background="?selectableItemBackground"
                        android:contentDescription="@string/app_name"
                        android:padding="2dp"
                        android:src="@android:drawable/presence_audio_online" />

                    <EditText
                        android:id="@+id/et_message"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:inputType="text" />

                    <Button
                        android:id="@+id/btn_send"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:drawable/editbox_background_normal"
                        android:text="发送" />
                </LinearLayout>

            </RelativeLayout>

The layout display is roughly as follows. GPT sometimes loses its temper and speaks in foreign languages. I don’t know which language it is, so it cannot be displayed, haha:

 

Two important classes are used to display messages, one is the message structure and the other is the adapter:

public class ChatMessageAdapter extends RecyclerView.Adapter<ChatMessageAdapter.MessageHolder> {
    private final String TAG = "ChatMessageAdapter";
    private static final int MY_MESSAGE = 0, OTHER_MESSAGE = 1;

    private List<ChatMessage> mMessages;
    private Context mContext;

    public interface OnClickListener {
        void onClick(int position, String message);
    }

    public interface OnLongClickListener {
        void onLongClick(int position, String message);
    }

    private OnClickListener clickListener;
    private OnLongClickListener longClickListener;


    public ChatMessageAdapter(Context context, List<ChatMessage> data) {
        mContext = context;
        mMessages = data;
    }
    public ChatMessageAdapter(Context context, List<ChatMessage> data, OnClickListener onClickListener, OnLongClickListener onLongClickListener) {
        mContext = context;
        mMessages = data;
        clickListener = onClickListener;
        longClickListener = onLongClickListener;
    }

    @Override
    public int getItemCount() {
        return mMessages == null ? 0 : mMessages.size();
    }

    @Override
    public int getItemViewType(int position) {
        ChatMessage item = mMessages.get(position);

        if (item.isMine()) return MY_MESSAGE;
        else return OTHER_MESSAGE;
    }

    @Override
    public MessageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == MY_MESSAGE) {
            return new MessageHolder(LayoutInflater.from(mContext).inflate(R.layout.item_mine_message, parent, false));
        } else {
            return new MessageHolder(LayoutInflater.from(mContext).inflate(R.layout.item_other_message, parent, false));
        }
    }

    public void add(ChatMessage message) {
        mMessages.add(message);
        notifyItemInserted(mMessages.size() - 1);
    }

    @Override
    public void onBindViewHolder(final MessageHolder holder, final int position) {
        ChatMessage chatMessage = mMessages.get(position);
        if (chatMessage.isImage()) {
            holder.ivImage.setVisibility(View.VISIBLE);
            holder.tvMessage.setVisibility(View.GONE);

            holder.ivImage.setImageResource(R.drawable.ic_launcher_background);
        } else {
            holder.ivImage.setVisibility(View.GONE);
            holder.tvMessage.setVisibility(View.VISIBLE);

            holder.tvMessage.setText(chatMessage.getContent());
        }

        String date = new SimpleDateFormat("hh:mm aa", Locale.getDefault()).format(new Date());
        holder.tvTime.setText("[单击复制]"+date);

        int index = position;
        if (clickListener != null){
            holder.chatMessageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.onClick(index, chatMessage.getContent());

                }
            });
        }

        if (longClickListener != null){
            holder.chatMessageView.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View view) {
                    longClickListener.onLongClick(index, chatMessage.getContent());
                    return false;
                }
            });
        }
    }

    class MessageHolder extends RecyclerView.ViewHolder {
        TextView tvMessage, tvTime;
        ImageView ivImage;
        ChatMessageView chatMessageView;

        MessageHolder(View itemView) {
            super(itemView);
            chatMessageView = (ChatMessageView) itemView.findViewById(R.id.chatMessageView);
            tvMessage = (TextView) itemView.findViewById(R.id.tv_message);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            ivImage = (ImageView) itemView.findViewById(R.id.iv_image);
        }
    }
}
public class ChatMessage {
    private boolean isImage, isMine;
    private String content;

    public ChatMessage(String message, boolean mine, boolean image) {
        content = message;
        isMine = mine;
        isImage = image;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public boolean isMine() {
        return isMine;
    }

    public void setIsMine(boolean isMine) {
        this.isMine = isMine;
    }

    public boolean isImage() {
        return isImage;
    }

    public void setIsImage(boolean isImage) {
        this.isImage = isImage;
    }
}

The main code is finished, use the artifact retrofit to connect to the api, and you can have fun showing off.

 

 

Guess you like

Origin blog.csdn.net/blogercn/article/details/130818030