Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Dec 14, 2024
1 parent 4952bbd commit 58e7db3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/src/tools/google_scholar.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SERPGoogleScholarAPITool } from "@langchain/community/tools/google_scholar";

const scholar = new SERPGoogleScholarAPITool({
apiKey: process.env.SERPAPI_API_KEY
apiKey: process.env.SERPAPI_API_KEY,
});

(async () => {
Expand All @@ -12,4 +12,4 @@ const scholar = new SERPGoogleScholarAPITool({
} catch (error) {
console.error("Error querying Google Scholar via SerpApi:", error);
}
})();
})();
19 changes: 10 additions & 9 deletions libs/langchain-community/src/tools/google_scholar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ export class SERPGoogleScholarAPITool extends Tool {
super(...arguments);

// Retrieve API key from fields or environment variables.
const apiKey =
fields?.apiKey ?? getEnvironmentVariable("SERPAPI_API_KEY");
const apiKey = fields?.apiKey ?? getEnvironmentVariable("SERPAPI_API_KEY");

// Throw an error if no API key is found.
if (!apiKey) {
Expand Down Expand Up @@ -105,13 +104,15 @@ export class SERPGoogleScholarAPITool extends Tool {
title: item.title, // Title of the article or paper.
link: item.link, // Direct link to the article or paper.
snippet: item.snippet, // Brief snippet or description.
publication_info: item.publication_info?.summary
?.split(" - ") // Split the summary at hyphens.
.slice(1) // Remove the authors from the start of the string.
.join(" - ") ?? "", // Rejoin remaining parts as publication info.
authors: item.publication_info?.authors
?.map((author: any) => author.name) // Extract the list of author names.
.join(", ") ?? "", // Join author names with a comma.
publication_info:
item.publication_info?.summary
?.split(" - ") // Split the summary at hyphens.
.slice(1) // Remove the authors from the start of the string.
.join(" - ") ?? "", // Rejoin remaining parts as publication info.
authors:
item.publication_info?.authors
?.map((author: any) => author.name) // Extract the list of author names.
.join(", ") ?? "", // Join author names with a comma.
total_citations: item.inline_links?.cited_by?.total ?? "", // Total number of citations.
})) ?? `No results found for ${input} on Google Scholar.`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,34 @@ import { SERPGoogleScholarAPITool } from "../google_scholar.js";
describe("SERPGoogleScholarAPITool", () => {
test("should be setup with correct parameters", async () => {
const instance = new SERPGoogleScholarAPITool({
apiKey: process.env.SERPAPI_API_KEY
apiKey: process.env.SERPAPI_API_KEY,

Check failure on line 7 in libs/langchain-community/src/tools/tests/google_scholar.int.test.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected use of process.env
});
expect(instance.name).toBe("serp_google_scholar");
});

test("SERPGoogleScholarAPITool returns a string for valid query", async () => {
const tool = new SERPGoogleScholarAPITool({
apiKey: process.env.SERPAPI_API_KEY
apiKey: process.env.SERPAPI_API_KEY,

Check failure on line 14 in libs/langchain-community/src/tools/tests/google_scholar.int.test.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected use of process.env
});
const result = await tool.invoke("Artificial Intelligence");
expect(typeof result).toBe("string");
});

test("SERPGoogleScholarAPITool returns non-empty string for valid query", async () => {
const tool = new SERPGoogleScholarAPITool({
apiKey: process.env.SERPAPI_API_KEY
apiKey: process.env.SERPAPI_API_KEY,

Check failure on line 22 in libs/langchain-community/src/tools/tests/google_scholar.int.test.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected use of process.env
});
const result = await tool.invoke("Artificial Intelligence");
expect(result.length).toBeGreaterThan(0);
});

test("SERPGoogleScholarAPITool returns 'No results found' for bad query", async () => {
const tool = new SERPGoogleScholarAPITool({
apiKey: process.env.SERPAPI_API_KEY
apiKey: process.env.SERPAPI_API_KEY,

Check failure on line 30 in libs/langchain-community/src/tools/tests/google_scholar.int.test.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected use of process.env
});
const result = await tool.invoke("dsalkfjsdlfjasdflasdl");
expect(result).toBe("\"No results found for dsalkfjsdlfjasdflasdl on Google Scholar.\"");
expect(result).toBe(
'"No results found for dsalkfjsdlfjasdflasdl on Google Scholar."'
);
});

});

0 comments on commit 58e7db3

Please sign in to comment.