File size: 2,575 Bytes
f967329
 
 
60bf579
f967329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7d23c7
 
 
f967329
 
 
 
 
2b412dc
f967329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60bf579
 
 
 
 
 
 
 
f967329
 
60bf579
f967329
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import gradio as gr
import os

from helper import load_world, save_world, get_mistral_api_key, load_env
from mistralai import Mistral



demo = None
client = Mistral(get_mistral_api_key())
world = load_world("Terra Bestia.json")
kingdom = world['kingdoms']['Ventara']
town = kingdom['towns']['Aeris']
character = town['npcs']['Kael']


def start_game(main_loop, share=False):
    global demo
    if demo is not None:
        demo.close()

    demo = gr.ChatInterface(
        main_loop,
        chatbot=gr.Chatbot(height=250, placeholder="Type 'start game' to begin"),
        textbox=gr.Textbox(placeholder="What do you do next?", container=False, scale=7),
        title="AI RPG",
        # description="Ask Yes Man any question",
        theme="soft",
        examples=["Look around", "Continue the story"],
        cache_examples=False,
        #retry_btn="Retry",
        #undo_btn="Undo",
        #clear_btn="Clear",
                           )
    demo.launch(share=share, server_name="0.0.0.0")

def run_action(message, history, game_state):
    
    if(message.strip().lower() == 'start game'):
        return game_state['start']

    system_prompt = """
    Vous êtes un maître du jeu IA. Votre travail consiste à écrire ce qui se passe ensuite dans le jeu d'aventure d'un joueur.\
    Instructions : \
    Vous ne devez écrire qu'une à trois phrases en réponse. \
    Écrivez toujours à la deuxième personne du présent. \
    Ex. (Vous regardez vers le nord et voyez...)
    """
    
    world_info = f"""
    World: {game_state['world']}
    Kingdom: {game_state['kingdom']}
    Town: {game_state['town']}
    Your Character:  {game_state['character']}"""

    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": world_info}
    ]
    for action in history:
        messages.append({"role": "assistant", "content": action[0]})
        messages.append({"role": "user", "content": action[1]})

    try:
        model_output = client.chat.complete(
            model="mistral-large-latest",
            messages=messages
        )
        result = model_output.choices[0].message.content
    except Exception as e:
        result = f"An error occurred: {str(e)}"
    
    return result
    

game_state = {
    "world": world['description'],
    "kingdom": kingdom['description'],
    "town": town['description'],
    "character": character['description'],
    "start": world['start']
}

def main_loop(message, history):
    return run_action(message, history, game_state)


start_game(main_loop, True)