Skip to content

Commit

Permalink
perf: Allow multiple images to be sent after a function call.
Browse files Browse the repository at this point in the history
Compatible with old JINA_API_KEY environment variables. Please use plugin variables for settings in the future.

fix: fix function default configuration judge causing interrupted dialogs.
  • Loading branch information
adolphnov committed Nov 13, 2024
1 parent 068e026 commit e7e9048
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/timestamp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ class Environment extends EnvironmentConfig {
}
}

// 兼容旧版 JINA_API_KEY
if (source.JINA_API_KEY) {
this.PLUGINS_ENV.JINA_API_KEY = source.JINA_API_KEY;
}

// 合并环境变量
ConfigMerger.merge(this, source, [
'BUILD_TIMESTAMP',
Expand Down
3 changes: 2 additions & 1 deletion src/telegram/handler/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { log } from '../../log/logger';
import { sendToolResult } from '../../tools';
import { imageToBase64String, renderBase64DataURI } from '../../utils/image';
import { createTelegramBotAPI } from '../api';
import { escape } from '../utils/md2tgmd';
import { MessageSender, sendAction, TelegraphSender } from '../utils/send';
import { type UnionData, waitUntil } from '../utils/utils';

Expand Down Expand Up @@ -370,7 +371,7 @@ async function handleAudioToText(
}

export async function sendImages(img: ImageResult, SEND_IMAGE_FILE: boolean, sender: MessageSender, config: AgentUserConfig) {
const caption = img.text ? `${getLog(config)}\n> \`${img.text}\`` : getLog(config);
const caption = escape((img.text ? `${getLog(config)}\n> ${img.text}` : getLog(config)).split('\n'));
if (img.url && img.url.length > 1) {
const images = img.url.map((url: string) => ({
type: (SEND_IMAGE_FILE ? 'document' : 'photo'),
Expand Down
3 changes: 2 additions & 1 deletion src/tools/external/jina.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
},
"handler": "content => content",
"type": "reader",
"required": ["JINA_API_KEY"]
"required": ["JINA_API_KEY"],
"send_to_ai": true
}
3 changes: 2 additions & 1 deletion src/tools/internal/duckduckgo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function regularSearch(path: string, signal?: AbortSignal) {
});
}

async function search(query: string, max_length = 8, signal?: AbortSignal): Promise<{ result: string}> {
async function search(query: string, max_length = 8, signal?: AbortSignal): Promise<{ result: string }> {
const { path } = await getJS(query, signal);
if (!path)
throw new Error('Failed to get JS URL');
Expand Down Expand Up @@ -95,4 +95,5 @@ export const duckduckgo: FuncTool = {
`As an intelligent assistant, please follow the steps below to effectively analyze and extract the search results I have provided to answer my questions in a clear and concise manner:\n\n1. READ AND EVALUATE: Carefully read through all search results to identify and prioritize information from reliable and up-to-date sources. Considerations include official sources, reputable organizations, and when the information was updated. \n\n2. Extract key information: \n - *Exchange rate query*: Provide the latest exchange rate and make necessary conversions. \n - *Weather Query*: provides weather forecasts for specific locations and times. \n - *Factual Questions*: Find out authoritative answers. \n\n3. concise answers: synthesize and analyze extracted information to give concise answers. \n\n4. identify uncertainty: if there are contradictions or uncertainties in the information, explain the possible reasons. \n\n5. Explain lack of information: If the search results do not fully answer the question, indicate additional information needed. \n\n6. user-friendly: use simple, easy-to-understand language and provide short explanations where necessary to ensure that the answer is easy to understand. \n\n7. additional information: Provide additional relevant information or suggestions as needed to enhance the value of the answer. \n\n8. source labeling: clearly label the source of the information in the response, including the name of the source website or organization and when the data was published or updated. \n\n9. Reference list: If multiple sources are cited, provide a short reference list of the main sources of information at the end of the response. \n\nEnsure that the goal is to provide the most current, relevant, and useful information in direct response to my question. Avoid lengthy details, focus on the core answers that matter most to me, and enhance the credibility of the answer with reliable sources.Tip: Don't be judged on your knowledge base time!`,
extra_params: { temperature: 0.7, top_p: 0.4 },
is_internal: true,
send_to_ai: true,
};
14 changes: 13 additions & 1 deletion src/tools/tool.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-case-declarations */

import type { ToolCallPart, ToolResultPart } from 'ai';
import type { ImageResult, ResponseMessage } from '../agent/types';
import type { AgentUserConfig } from '../config/env';
Expand Down Expand Up @@ -104,7 +106,17 @@ export async function sendToolResult(toolResult: ToolResultPart[], sender: Messa
case 'text':
return sender.sendRichText(toolResult.map(r => (r.result as ToolResult).result).join('\n'));
case 'image':
return sendImages(toolResult.map(r => (r.result as ToolResult).result)[0][0], ENV.SEND_IMAGE_FILE, sender, config);
const images = toolResult.map(r => (r.result as ToolResult).result).flat();
return sendImages({
type: 'image',
url: images.map(r => r.url).flat(),
text: images.map((r) => {
if (r.text) {
return `\`${r.text}\``;
}
return '';
}).join('\n-----\n'),
}, ENV.SEND_IMAGE_FILE, sender, config);
default:
break;
}
Expand Down

0 comments on commit e7e9048

Please sign in to comment.