Skip to content

Commit

Permalink
♻️Refactoring: The classes contained in AIProvider.ts were made into …
Browse files Browse the repository at this point in the history
…separate files.

🔧 chore(.codeclimate.yml): Update Code Climate configuration to enhance quality checks
Several changes have been made to the Code Climate configuration file. These include adding new checks, enabling CSSLint, removing unnecessary languages, and adding Markdownlint. These changes aim to strengthen code quality checks and ensure a more consistent codebase. Additionally, the settings for excluding certain files and folders have been updated to ensure auto-generated files and folders are excluded from checks.

🔤 docs(README.md): Correct "GOGGLE_API_KEY" to "GOOGLE_API_KEY" for accurate spelling
Correcting the typo improves the accuracy of the documentation. This ensures that the correct environment variable name for the API key is clearly communicated to users, avoiding confusion when configuring.
  • Loading branch information
takuya-o committed Apr 15, 2024
1 parent a7d3848 commit 96370a7
Show file tree
Hide file tree
Showing 11 changed files with 513 additions and 294 deletions.
71 changes: 57 additions & 14 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,79 @@
# enabled: false
# # ... CONFIG CONTENT ...
---
# https://docs.codeclimate.com/docs/advanced-configuration
version: "2"
checks:
# commenct is default
# argument-count:
# config:
# threshold: 4
# complex-logic:
# config:
# threshold: 4
# file-lines:
# config:
# threshold: 250
# method-complexity:
# config:
# threshold: 5
# method-count:
# config:
# threshold: 20
# method-lines:
# config:
# threshold: 25
# nested-control-flow:
# config:
# threshold: 4
# return-statements:
# config:
# threshold: 4
# similar-code:
# config:
# threshold: # language-specific defaults. an override will affect all languages.
# identical-code:
# config:
# threshold: # language-specific defaults. an override will affect all languages.
plugins:
# csslint:
# enabled: true
# https://docs.codeclimate.com/docs/list-of-engines
# coffeelint:
# enabled: true
csslint:
enabled: true
duplication:
enabled: true
config:
# https://docs.codeclimate.com/docs/duplication
languages:
# https://docs.codeclimate.com/docs/duplication
- typescript
- ruby
# - ruby
- javascript
- python
- php
# https://docs.codeclimate.com/docs/list-of-engines
# https://docs.codeclimate.com/docs/tslint
# $ ./node_modules/.bin/tslint src/**/*.ts
# tslint:
# enabled: true
# config: tslint.json
# - python
# - php
eslint:
# https://docs.codeclimate.com/docs/eslint
enabled: true
channel: "stable"
config: .eslintrc.json
# config: .eslintrc.json
fixme:
enabled: true
rubocop:
#rubocop:
# enabled: true
##
## Community-Supported Plugins
##
markdownlint:
enabled: true
# semgrep:
# enabled: true
# tslint:
# https://docs.codeclimate.com/docs/tslint
# $ ./node_modules/.bin/tslint src/**/*.ts
# enabled: true
# config: tslint.json
exclude_patterns:
# https://docs.codeclimate.com/docs/excluding-files-and-folders#section-auto-generated-file-and-folder-exclusions
- config/
- db/
- dist/
Expand All @@ -59,6 +101,7 @@ exclude_patterns:
- "**/vendor/"
- "**/*_test.go"
- "**/*.d.ts"
# add
- "**/*.min.js"
- "**/*.min.css"
- "**/__tests__/"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ or when [running the docker image](#using-the-ready-made-docker-image) or when c
| AZURE_OPENAI_API_IMAGE_INSTANCE_NAME | no | `example-name` | The instance name on the Azure OpenAI Service for Image API if specially needed |
| AZURE_OPENAI_API_IMAGE_DEPLOYMENT_NAME | no | `Dalle3` | The name of the deployed model on the Azure OpenAI Service for Iamge API if specially needed. DALL-E 3 require 2023-12-01-preview API Version at least |
| COHERE_API_KEY | no | `0123456789abcdefghijklmno` | The Cohere API key to authoenticate. If OPENAI_API_KEY is also set, the original OpenAI is used for vision or image generation. |
| GOGGLE_API_KEY | no | `0123456789abcdefghijklmno` | The Gemini API key to authoenticate. If OPENAI_API_KEY is also set, the original OpenAI is used for vision or image generation. Tested model is only 'gemini-1.5-pro-latest' |
| GOOGLE_API_KEY | no | `0123456789abcdefghijklmno` | The Gemini API key to authoenticate. If OPENAI_API_KEY is also set, the original OpenAI is used for vision or image generation. Tested model is only 'gemini-1.5-pro-latest' |
| YFILES_SERVER_URL | no | `http://localhost:3835` | The URL to the yFiles graph service for embedding auto-generated diagrams. |
| NODE_EXTRA_CA_CERTS | no | `/file/to/cert.crt` | a link to a certificate file to pass to node.js for authenticating self-signed certificates |
| MATTERMOST_BOTNAME | no | `"@chatgpt"` | the name of the bot user in Mattermost, defaults to '@chatgpt' |
Expand Down
98 changes: 55 additions & 43 deletions dist/botservice.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ import FormData3 from "form-data";
import FormData from "form-data";

// src/AIProvider.ts
import { CohereClient } from "cohere-ai";
import Anthropic from "@anthropic-ai/sdk";
import { Log as Log2 } from "debug-level";
import OpenAI from "openai";
Log2.options({ json: true, colors: true });
var log = new Log2("AIAdapter");
var AIAdapter = class {
Expand Down Expand Up @@ -128,27 +125,18 @@ var AIAdapter = class {
return message;
}
};
var OpenAIAdapter = class {
openai;
baseURL;
constructor(openaiArgs) {
this.openai = new OpenAI(openaiArgs);
this.baseURL = this.openai.baseURL;
}
async createMessage(options) {
try {
return this.openai.chat.completions.create(options);
} catch (error) {
if (error instanceof OpenAI.APIError) {
log.error(`OpenAI API Error: ${error.status} ${error.name}`, error);
}
throw error;
}
function shortenString(text) {
if (!text) {
return text;
}
async imagesGenerate(imageGeneratePrams) {
return this.openai.images.generate(imageGeneratePrams);
if (text.length < 1024) {
return text;
}
};
return text.substring(0, 1023) + "...";
}

// src/adapters/AnthropicAdapter.ts
import Anthropic from "@anthropic-ai/sdk";
var AnthropicAdapter = class {
anthropic;
baseURL;
Expand Down Expand Up @@ -184,6 +172,12 @@ var AnthropicAdapter = class {
throw new Error("Anthropic does not support image generation.");
}
};

// src/adapters/CohereAdapter.ts
import { CohereClient } from "cohere-ai";
import Log3 from "debug-level";
Log3.options({ json: true, colors: true });
var log2 = new Log3("Cohere");
var CohereAdapter = class extends AIAdapter {
cohere;
baseURL;
Expand All @@ -194,7 +188,7 @@ var CohereAdapter = class extends AIAdapter {
}
async createMessage(options) {
const chat = await this.cohere.chat(this.mapOpenAIOptionsToCohereOptions(options));
log.debug("Cohere chat() response: ", chat);
log2.debug("Cohere chat() response: ", chat);
return this.mapOpenAICompletion(chat, options.model);
}
mapOpenAICompletion(chat, model2) {
Expand Down Expand Up @@ -264,7 +258,7 @@ var CohereAdapter = class extends AIAdapter {
parameterDefinitions
});
});
log.debug("Cohere tools", cohereTools);
log2.debug("Cohere tools", cohereTools);
return cohereTools;
}
getChatHistory(messages) {
Expand All @@ -289,31 +283,22 @@ var CohereAdapter = class extends AIAdapter {
message: message.content ?? ""
});
} else {
log.debug(`getChatHistory(): ${message.role} not yet support.`, message);
log2.debug(`getChatHistory(): ${message.role} not yet support.`, message);
}
});
log.debug("Cohere chat history", chatHistory);
log2.debug("Cohere chat history", chatHistory);
return chatHistory;
}
async imagesGenerate(_imageGeneratePrams) {
throw new Error("Cohere does not support image generation.");
}
};
function shortenString(text) {
if (!text) {
return text;
}
if (text.length < 1024) {
return text;
}
return text.substring(0, 1023) + "...";
}

// src/adapers/GoogleGeminiAdapter.ts
// src/adapters/GoogleGeminiAdapter.ts
import { GoogleGenerativeAI } from "@google/generative-ai";
import { Log as Log3 } from "debug-level";
Log3.options({ json: true, colors: true });
var log2 = new Log3("Gemini");
import { Log as Log4 } from "debug-level";
Log4.options({ json: true, colors: true });
var log3 = new Log4("Gemini");
var GoogleGeminiAdapter = class extends AIAdapter {
generativeModel;
baseURL;
Expand Down Expand Up @@ -354,7 +339,7 @@ var GoogleGeminiAdapter = class extends AIAdapter {
//tools?: Tool[];
//toolConfig?: ToolConfig;
});
log2.trace("GenerateContentResult", generateContentResult);
log3.trace("GenerateContentResult", generateContentResult);
const responseText = generateContentResult.response.text();
const choices = this.createChoice(responseText);
const usage = await this.getUsage(currentMessages, prompt, responseText);
Expand Down Expand Up @@ -385,11 +370,11 @@ var GoogleGeminiAdapter = class extends AIAdapter {
case "tool":
case "function":
default:
log2.error(`getChatHistory(): ${message.role} not yet support.`, message);
log3.error(`getChatHistory(): ${message.role} not yet support.`, message);
break;
}
});
log2.trace("currentMessages():", currentMessages);
log3.trace("currentMessages():", currentMessages);
return currentMessages;
}
createChoice(responseText) {
Expand All @@ -416,7 +401,7 @@ var GoogleGeminiAdapter = class extends AIAdapter {
outputTokens = (await this.generativeModel.countTokens(responseText)).totalTokens;
} catch (error) {
if (error.message.indexOf("GoogleGenerativeAI Error") >= 0) {
log2.info("Gemini 1.5 not support countTokens()?", error);
log3.info("Gemini 1.5 not support countTokens()?", error);
} else {
throw error;
}
Expand All @@ -432,6 +417,33 @@ var GoogleGeminiAdapter = class extends AIAdapter {
}
};

// src/adapters/OpenAIAdapter.ts
import Log5 from "debug-level";
import OpenAI from "openai";
Log5.options({ json: true, colors: true });
var log4 = new Log5("OpenAI");
var OpenAIAdapter = class {
openai;
baseURL;
constructor(openaiArgs) {
this.openai = new OpenAI(openaiArgs);
this.baseURL = this.openai.baseURL;
}
async createMessage(options) {
try {
return this.openai.chat.completions.create(options);
} catch (error) {
if (error instanceof OpenAI.APIError) {
log4.error(`OpenAI API Error: ${error.status} ${error.name}`, error);
}
throw error;
}
}
async imagesGenerate(imageGeneratePrams) {
return this.openai.images.generate(imageGeneratePrams);
}
};

// src/openai-wrapper.ts
var apiKey = process.env["OPENAI_API_KEY"];
var azureOpenAiApiKey = process.env["AZURE_OPENAI_API_KEY"];
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default tseslint.config({
/** 命名規則 */
"@typescript-eslint/naming-convention": [
"error",
{ // classやtypeなどは頭大文
{ // classやtypeなどは頭大文字
"selector": "typeLike",
"format": ["PascalCase"]
},
Expand Down
Loading

0 comments on commit 96370a7

Please sign in to comment.