Commit 3d531b8f authored by JinyuanSun's avatar JinyuanSun
Browse files

init

parent 8ecaf1ae
Loading
Loading
Loading
Loading
+36 −2
Original line number Diff line number Diff line
# ChatMol
ChatGPT in PyMOL now!
# PyMOL ChatGPT Plugin
![img](./assets/img.png)
## Overview
The PyMOL ChatGPT Plugin is a simple yet powerful tool that integrates OpenAI's GPT-3.5-turbo model into PyMOL, allowing users to interact with the model and get relevant suggestions, explanations, and guidance on various PyMOL-related topics.

## Requirements
- PyMOL
- OpenAI Python package: `pip install openai`
## Installation
1. Download the plugin script `chatgpt_plugin.py` and save it to a convenient location on your computer.
2. Open PyMOL.
3. In the PyMOL command line, enter `run /path/to/chatgpt_plugin.py` (replace `/path/to` with the actual path to the script).
4. The plugin is now installed and ready to use.

Or:

```
load https://raw.githubusercontent.com/ChatMol/ChatMol/main/ChatMol.py
```

## Usage
1. Set your OpenAI API key by entering the following command in the PyMOL command line: `set_api_key your_api_key_here` (replace `your_api_key_here` with your actual API key). The API key will be saved in the same directory as the plugin script for future use.
2. To interact with ChatGPT, simply use the `chatgpt` command followed by your question or message in the PyMOL command line, e.g., `chatgpt "How do I align two proteins?"`.
## Features
- Simple integration with PyMOL.
- Convenient command-line interface.
- Persistent API key storage, ensuring you only need to set the API key once.
- Utilizes OpenAI's GPT-3.5-turbo model for powerful, context-aware suggestions and guidance.
## Limitations
- The plugin relies on the OpenAI API, so an internet connection and API key are required for usage.
- The ChatGPT model's knowledge is based on the training data available up to September 2021.
## Support
For any questions or issues related to the PyMOL ChatGPT Plugin, please refer to the official PyMOL mailing list or OpenAI's documentation and support resources.

## License
This project is released under the MIT License.
 No newline at end of file

assets/img.png

0 → 100644
+777 KiB
Loading image diff...

chatmol.py

0 → 100644
+66 −0
Original line number Diff line number Diff line
from pymol import cmd

import openai
import os

conversation_history = " "    

PLUGIN_DIR = os.path.dirname(os.path.abspath(__file__))
API_KEY_FILE = os.path.join(PLUGIN_DIR, "apikey.txt")

def set_api_key(api_key):
    api_key = api_key.strip()
    openai.api_key = api_key
    with open(API_KEY_FILE, "w") as api_key_file:
        api_key_file.write(api_key)
    print("API key set and saved to file successfully.")

def load_api_key():
    try:
        with open(API_KEY_FILE, "r") as api_key_file:
            api_key = api_key_file.read().strip()
            openai.api_key = api_key
            print("API key loaded from file.")
    except FileNotFoundError:
        print("API key file not found. Please set your API key using 'set_api_key your_api_key_here' command.")

load_api_key()

def chat_with_gpt(message):
    global conversation_history

    conversation_history += f"User: {message}\nChatGPT:"

    try:
        messages = [
            {"role": "system", "content": "Pymol, commands, simple,"}
        ]

        message_parts = conversation_history.strip().split("\n")
        for i, part in enumerate(message_parts):
            role = "user" if i % 2 == 0 else "assistant"
            messages.append({"role": role, "content": part})

        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=messages,
            max_tokens=5000,
            n=1,
            temperature=0.1,
        )
        answer = response.choices[0].message['content'].strip()

        conversation_history += f"{answer}\n"

        return answer
    except Exception as e:
        print(f"Error: {e}")
        return ""

def start_chatgpt_cmd(message):
    response = chat_with_gpt(message)
    print("ChatGPT: " + response.strip())

cmd.extend("set_api_key", set_api_key)
cmd.extend("chatgpt", start_chatgpt_cmd)