Commit e240a407 authored by JinyuanSun's avatar JinyuanSun
Browse files

add python package claude is supported in chatmol packge

parent 5f7c6d9f
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -128,3 +128,5 @@ dmypy.json

# Pyre type checker
.pyre/

test*
+10 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ Started from a PyMol plugin.
- [ChatMol](#chatmol)
  - [Table of contents](#table-of-contents)
  - [Overview](#overview)
  - [ChatMol python package](#chatmol-python-package)
  - [Copilot](#copilot)
  - [ChatMol Website](#chatmol-website)
  - [Requirements \& Installation](#requirements--installation)
@@ -18,7 +19,15 @@ Started from a PyMol plugin.
  - [License](#license)

## Overview
The PyMOL ChatGPT Plugin seamlessly integrates OpenAI's large language models (GPT-3.5-turbo and text-davinci-003) into PyMOL, enabling users to interact with PyMOL using natural language instructions. This robust tool simplifies PyMOL tasks and offers suggestions, explanations, and guidance on a wide range of PyMOL-related topics. ChatMol provides various interaction modes with PyMOL, including the PyMOL command line, miniGUI chatbot, and web browsers.
The PyMOL ChatGPT Plugin seamlessly integrates LLMs into PyMOL, enabling users to interact with PyMOL using natural language instructions. This tool simplifies PyMOL tasks and offers suggestions, explanations, and guidance on a wide range of PyMOL-related topics. ChatMol provides various interaction modes with PyMOL, including the PyMOL command line, miniGUI chatbot, python and web browsers.

## ChatMol python package

See this [README](./chatmol_pkg/README.md) for more details.

```bash
pip install chatmol
```

## Copilot
This is ChatMol copilot, just like other copilot, it is designed to help your work.  

chatmol_pkg/README.md

0 → 100644
+44 −0
Original line number Diff line number Diff line
# ChatMol Python Package

ChatMol is a Python package that provides a seamless integration of large language models into PyMOL, enabling users to interact with PyMOL using natural language instructions. This robust tool simplifies PyMOL tasks and offers suggestions, explanations, and guidance on a wide range of PyMOL-related topics. ChatMol provides various interaction modes with PyMOL, including the PyMOL command line, Python, miniGUI chatbot, and web browsers.

## Installation

```bash
pip install chatmol
```

## Usage

Here are some examples of how to use the package:

```python
import chatmol as cm
output_chatmol_llm = cm.chatlite("download chain A of 3wzm and color it by secondary structure") # use the chatmol llm, free and no API key required
print(output_chatmol_llm)
```

```python
print(cm.defaul_client.gpt_model) # check the current ChatGPT model
output_chatgpt = cm.chat_with_gpt("download 4eb0 and highlight residue number 208") # use the GPT-3.5-turbo llm, API key required
print(output_chatgpt)
```

```python
print(cm.defaul_client.claude_model) # check the current Claude model
output_claude = cm.chat_with_claude("download 1pga from rcsb and show a transprant surface") # use the claude llm, API key required
print(output_claude)
```

You can send results to PyMOL:

```python
import chatmol as cm
ps = cm.start_pymol() # open a PyMOL session with XML-RPC server
ps.chatlite("download 1pga")

# send commands to PyMOL
ps.server.do("esmfold MTYKLILNGKTLKGETTTEAVDAATAEKVFKQYANDNGVDGEWTYDDATKTFTVTE, 1pga_esmfold") # make sure you have pymolfold plugin installed
```

enjoy!
 No newline at end of file
+24 −0
Original line number Diff line number Diff line
from .utils import ChatMol
from .pymol_server import PymolServer

defaul_client = ChatMol()

def chatlite(question):
    return defaul_client.chatlite(question)

def chat_with_gpt(message):
    return defaul_client.chat_with_gpt(message)

def chat_with_claude(message):
    return defaul_client.chat_with_claude(message)

def clear_stashed_commands():
    return defaul_client.clear_stashed_commands()

def clear_chat_history():
    return defaul_client.clear_chat_history()

def start_pymol():
    pymolserver = PymolServer(defaul_client)
    return pymolserver
+114 −0
Original line number Diff line number Diff line
import subprocess
from xmlrpc import client
from .utils import ChatMol

class PymolServer():
    def __init__(self, default_client:ChatMol):
        self.cm = default_client
        self.start_pymol()

    def start_pymol(self):
        # os.system("nohup pymol -R /dev/null 2>&1")
        subprocess.Popen(["pymol", "-R"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        self.server = client.ServerProxy(uri="http://localhost:9123/RPC2")
        return 0

    def chatlite(self, question):
        answer = self.cm.chatlite(question)
        commands = answer.split('\n')
        print("Answers from ChatMol-Lite: ")
        for command in commands:
            if command == '':
                continue
            else:
                print(command)
                self.server.do(command)
        return commands

    def chatgpt(self, message, execute:bool=True, lite:bool=False):
        if lite:
            self.cm.chatlite(message)
        message = message.strip()
        if message == "e" or message == 'execute':
            if len(self.cm.stashed_commands) == 0:
                print("There is no stashed commands")
            else:
                for command in stashed_commands:
                    self.server.do(command)
                self.cm.clear_stashed_commands()
            return 0
        
        if message == "new":
            self.cm.clear_chat_history()
            self.cm.clear_stashed_commands()
            return 0
        
        if message.endswith('?'):
            execute = False
        
        response = self.cm.chat_with_gpt(message)  # Using the chat_with_gpt method
        print("ChatGPT:", response)

        try:
            command_blocks = []
            self.cm.clear_stashed_commands()
            for i, block in enumerate(response.split("```")):
                if i % 2 == 1:
                    command_blocks.append(block)
            for command_block in command_blocks:
                for command in command_block.split("\n"):
                    if command.strip() and not command.strip().startswith("#"):
                        if command.strip() == "python" or command.strip() == "bash":
                            continue  # Skipping python commands
                        if "#" in command:
                            command, comment = command.split("#")
                        if execute:
                            print(command)
                            self.server.do(command)
                        else:
                            self.cm.stashed_commands.append(command)
        except Exception as e:
            print(f"Error during command execution: {e}")

    def claude(self, message, execute:bool=True):
        message = message.strip()
        if message == "e" or message == 'execute':
            if len(self.cm.stashed_commands) == 0:
                print("There is no stashed commands")
            else:
                for command in stashed_commands:
                    self.server.do(command)
                self.cm.clear_stashed_commands()
            return 0
        
        if message == "new":
            self.cm.clear_chat_history()
            self.cm.clear_stashed_commands()
            return 0
        
        if message.endswith('?'):
            execute = False
        
        response = self.cm.chat_with_claude(message)  # Using the chat_with_gpt method
        print(self.cm.claude_model, response)

        try:
            command_blocks = []
            self.cm.clear_stashed_commands()
            for i, block in enumerate(response.split("```")):
                if i % 2 == 1:
                    command_blocks.append(block)
            for command_block in command_blocks:
                for command in command_block.split("\n"):
                    if command.strip() and not command.strip().startswith("#"):
                        if command.strip() == "python" or command.strip() == "bash":
                            continue  # Skipping python commands
                        if "#" in command:
                            command, comment = command.split("#")
                        if execute:
                            print(command)
                            self.server.do(command)
                        else:
                            self.cm.stashed_commands.append(command)
        except Exception as e:
            print(f"Error during command execution: {e}")
 No newline at end of file
Loading