File size: 6,663 Bytes
3b6afc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const buildDefaultConversation = ({
  conversation,
  endpoint,
  endpointsConfig = {},
  lastConversationSetup = {},
}) => {
  const lastSelectedModel = JSON.parse(localStorage.getItem('lastSelectedModel')) || {};
  const lastSelectedTools = JSON.parse(localStorage.getItem('lastSelectedTools')) || [];
  const lastBingSettings = JSON.parse(localStorage.getItem('lastBingSettings')) || [];

  if (endpoint === 'azureOpenAI' || endpoint === 'openAI') {
    conversation = {
      ...conversation,
      endpoint,
      model:
        lastConversationSetup?.model ??
        lastSelectedModel[endpoint] ??
        endpointsConfig[endpoint]?.availableModels?.[0] ??
        'gpt-3.5-turbo',
      chatGptLabel: lastConversationSetup?.chatGptLabel ?? null,
      promptPrefix: lastConversationSetup?.promptPrefix ?? null,
      temperature: lastConversationSetup?.temperature ?? 1,
      top_p: lastConversationSetup?.top_p ?? 1,
      presence_penalty: lastConversationSetup?.presence_penalty ?? 0,
      frequency_penalty: lastConversationSetup?.frequency_penalty ?? 0,
    };
  } else if (endpoint === 'google') {
    conversation = {
      ...conversation,
      endpoint,
      model:
        lastConversationSetup?.model ??
        lastSelectedModel[endpoint] ??
        endpointsConfig[endpoint]?.availableModels?.[0] ??
        'chat-bison',
      modelLabel: lastConversationSetup?.modelLabel ?? null,
      promptPrefix: lastConversationSetup?.promptPrefix ?? null,
      examples: lastConversationSetup?.examples ?? [
        { input: { content: '' }, output: { content: '' } },
      ],
      temperature: lastConversationSetup?.temperature ?? 0.2,
      maxOutputTokens: lastConversationSetup?.maxOutputTokens ?? 1024,
      topP: lastConversationSetup?.topP ?? 0.95,
      topK: lastConversationSetup?.topK ?? 40,
    };
  } else if (endpoint === 'bingAI') {
    const { jailbreak, toneStyle } = lastBingSettings;
    conversation = {
      ...conversation,
      endpoint,
      jailbreak: lastConversationSetup?.jailbreak ?? jailbreak ?? false,
      context: lastConversationSetup?.context ?? null,
      systemMessage: lastConversationSetup?.systemMessage ?? null,
      toneStyle: lastConversationSetup?.toneStyle ?? toneStyle ?? 'creative',
      jailbreakConversationId: lastConversationSetup?.jailbreakConversationId ?? null,
      conversationSignature: null,
      clientId: null,
      invocationId: 1,
    };
  } else if (endpoint === 'anthropic') {
    conversation = {
      ...conversation,
      endpoint,
      model:
        lastConversationSetup?.model ??
        lastSelectedModel[endpoint] ??
        endpointsConfig[endpoint]?.availableModels?.[0] ??
        'claude-1',
      modelLabel: lastConversationSetup?.modelLabel ?? null,
      promptPrefix: lastConversationSetup?.promptPrefix ?? null,
      temperature: lastConversationSetup?.temperature ?? 0.7,
      maxOutputTokens: lastConversationSetup?.maxOutputTokens ?? 1024,
      topP: lastConversationSetup?.topP ?? 0.7,
      topK: lastConversationSetup?.topK ?? 40,
    };
  } else if (endpoint === 'chatGPTBrowser') {
    conversation = {
      ...conversation,
      endpoint,
      model:
        lastConversationSetup?.model ??
        lastSelectedModel[endpoint] ??
        endpointsConfig[endpoint]?.availableModels?.[0] ??
        'text-davinci-002-render-sha',
    };
  } else if (endpoint === 'gptPlugins') {
    const agentOptions = lastConversationSetup?.agentOptions ?? {
      agent: 'functions',
      skipCompletion: true,
      model: 'gpt-3.5-turbo',
      temperature: 0,
      // top_p: 1,
      // presence_penalty: 0,
      // frequency_penalty: 0
    };
    conversation = {
      ...conversation,
      endpoint,
      tools: lastSelectedTools ?? lastConversationSetup?.tools ?? [],
      model:
        lastConversationSetup?.model ??
        lastSelectedModel[endpoint] ??
        endpointsConfig[endpoint]?.availableModels?.[0] ??
        'gpt-3.5-turbo',
      chatGptLabel: lastConversationSetup?.chatGptLabel ?? null,
      promptPrefix: lastConversationSetup?.promptPrefix ?? null,
      temperature: lastConversationSetup?.temperature ?? 0.8,
      top_p: lastConversationSetup?.top_p ?? 1,
      presence_penalty: lastConversationSetup?.presence_penalty ?? 0,
      frequency_penalty: lastConversationSetup?.frequency_penalty ?? 0,
      agentOptions,
    };
  } else if (endpoint === null) {
    conversation = {
      ...conversation,
      endpoint,
    };
  } else {
    console.error(`Unknown endpoint ${endpoint}`);
    conversation = {
      ...conversation,
      endpoint: null,
    };
  }

  return conversation;
};

const getDefaultConversation = ({ conversation, endpointsConfig, preset }) => {
  const { endpoint: targetEndpoint } = preset || {};

  if (targetEndpoint) {
    // try to use preset
    const endpoint = targetEndpoint;
    if (endpointsConfig?.[endpoint]) {
      conversation = buildDefaultConversation({
        conversation,
        endpoint,
        lastConversationSetup: preset,
        endpointsConfig,
      });
      return conversation;
    } else {
      console.log(endpoint);
      console.warn(`Illegal target endpoint ${targetEndpoint} ${endpointsConfig}`);
    }
  }

  // try {
  //   // try to use current model
  //   const { endpoint = null } = prevConversation || {};
  //   if (endpointsConfig?.[endpoint]) {
  //     conversation = buildDefaultConversation({
  //       conversation,
  //       endpoint,
  //       lastConversationSetup: prevConversation,
  //       endpointsConfig
  //     });
  //     return conversation;
  //   }
  // } catch (error) {}

  try {
    // try to read latest selected model from local storage
    const lastConversationSetup = JSON.parse(localStorage.getItem('lastConversationSetup'));
    let endpoint = null;
    if (lastConversationSetup) {
      ({ endpoint } = lastConversationSetup);
    }

    if (endpointsConfig?.[endpoint]) {
      conversation = buildDefaultConversation({ conversation, endpoint, endpointsConfig });
      return conversation;
    }
  } catch (error) {
    console.error(error);
  }

  // if anything happens, reset to default model

  const endpoint = [
    'openAI',
    'azureOpenAI',
    'bingAI',
    'chatGPTBrowser',
    'gptPlugins',
    'google',
    'anthropic',
  ].find((e) => endpointsConfig?.[e]);
  if (endpoint) {
    conversation = buildDefaultConversation({ conversation, endpoint, endpointsConfig });
    return conversation;
  } else {
    conversation = buildDefaultConversation({ conversation, endpoint: null, endpointsConfig });
    return conversation;
  }
};

export default getDefaultConversation;