Skip to content

Commit

Permalink
Initial prototype phase of WA AI Dart
Browse files Browse the repository at this point in the history
  • Loading branch information
validcube authored Feb 24, 2024
1 parent 9e0d655 commit 6da8d84
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/

# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## 0.0.1
Prototype stage of Wolfram|Alpha AI Dart client.
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
# 🚀 Template for New Project
This repository only contains a template that can be used to create a repository
for new punExperiment project.
# 🌀 WolframAlpha API (Dart)

Settings for this template:
* License: Apache 2.0
* Code of Conduct: Contributor Covenant 2.1
* CI
- [addlicense v1.1.1](https://github.com/google/addlicense/releases/tag/v1.1.1)
> [!CAUTION]
> This project is in Prototype phase, the implementation is all using the demo API.
30 changes: 30 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
18 changes: 18 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# `> WA_Dart // Contributing`
Thanks you for your interest in contributing. Please check out the [Code of Conduct](CODE_OF_CONDUCT.md) before contributing, thank you!

> [!IMPORTANT]
> This guide assume that you already installed Dart 3.3.0 or higher.
Building the script:
1. Clone the repository: `[email protected]:BoringExperiments/WolframAlpha_ai_dart.git`
2. Install the required dependencies: `dart pub get`
3. Build the script: `dart build`

Development tools:
- Analysis check: `dart analyze`
- Test: `dart test`

Here are some tip when contributing:
- [Signing commits](https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits) are highly recommended.
- This repository follows the [Effective Dart](https://dart.dev/effective-dart) guide **but not strictly enforced**.
147 changes: 147 additions & 0 deletions lib/src/api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import 'package:dio/dio.dart';
import 'error.dart';

final options = BaseOptions(
baseUrl: 'https://api.wolframalpha.com',
headers: {'User-Agent': 'WA-Dart-client/0.0.1'},
);

final dio = Dio(options);

// Return simplified information using image data.
dynamic simple({
required String appId,
required String input,
// String? units,
// int? width,
// int? fontSize,
// String? background,
// String? foreground,
// String? layout,
int? timeout = 5,
}) async {
Response response;
response = await dio.request(
'/v1/simple',
queryParameters: {
'appid': appId,
'i': input,
// 'units': units,
// 'width': width,
// 'fontSize': fontSize,
// 'background': background,
// 'foreground': fontSize,
// 'layout': layout,
'timeout': timeout,
},
);
if (response.statusCode == 501) {
return Exception(UnknownPrompt);
}
if (response.statusCode == 400) {
return Exception(MissingRequiredParameter);
}
if (response.data == 'Error 1: Invalid appid') {
return Exception(InvalidAppId);
}
return response.data;
}

dynamic shortAnswers({
required String appId,
required String input,
// String? units,
int? timeout = 5,
}) async {
Response response;
response = await dio.request('/v1/result', queryParameters: {
'appid': appId,
'i': input,
// 'units': units,
'timeout': timeout,
});
if (response.statusCode == 501) {
return Exception(UnknownPrompt);
}
if (response.statusCode == 400) {
return Exception(MissingRequiredParameter);
}
if (response.data == 'Error 1: Invalid appid') {
return Exception(InvalidAppId);
}
return response.data;
}

dynamic spokenAnswers({
required String appId,
required String input,
// String? units,
int? timeout = 5,
}) async {
Response response;
response = await dio.request('/v1/spoken', queryParameters: {
'appid': appId,
'i': input,
'timeout': timeout,
});
if (response.statusCode == 501) {
return Exception(UnknownPrompt);
}
if (response.statusCode == 400) {
return Exception(MissingRequiredParameter);
}
if (response.data == 'Error 1: Invalid appid') {
return Exception(InvalidAppId);
}
return response.data;
}

dynamic fastQueryRecognizer({
required String appId,
required String input,
String? mode,
String? output,
}) async {
Response response;
response = await dio.request('/queryrecognizer/query.jsp', queryParameters: {
'appid': appId,
'i': input,
// 'mode': mode,
// 'output: output,
});
if (response.statusCode == 501) {
return Exception(UnknownPrompt);
}
if (response.statusCode == 400) {
return Exception(MissingRequiredParameter);
}
if (response.data == 'Error 1: Invalid appid') {
return Exception(InvalidAppId);
}
return response.data;
}

dynamic fullResult({
required String appId,
required String input,
String? format,
String? output,
}) async {
Response response;
response = await dio.request('/v2/query', queryParameters: {
'appid': appId,
'input': input,
// 'mode': mode,
// 'output: output,
});
if (response.statusCode == 501) {
return Exception(UnknownPrompt);
}
if (response.statusCode == 400) {
return Exception(MissingRequiredParameter);
}
if (response.data == 'Error 1: Invalid appid') {
return Exception(InvalidAppId);
}
return response.data;
}
21 changes: 21 additions & 0 deletions lib/src/error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

/*
Thrown when the server rejects the API key
Commonly caused by expired/invalid/non-existent API key
*/
final class InvalidAppId implements Exception {
final String message;
InvalidAppId(this.message);
}

// Input cannot be interpreted by the API
final class UnknownPrompt implements Exception {
final String message;
UnknownPrompt(this.message);
}

// API did not find the required input parameter.
final class MissingRequiredParameter implements Exception {
final String message;
MissingRequiredParameter(this.message);
}
5 changes: 5 additions & 0 deletions lib/wolframalpha.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library;

export 'src/api.dart' show simple, shortAnswers, spokenAnswers;
export 'src/error.dart'
show InvalidAppId, UnknownPrompt, MissingRequiredParameter;
22 changes: 22 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: wolframalpha_ai_dart
description: Dart client for interacting with WolframAlpha API, a engine for providing answers and knowledge.
version: 0.0.1
repository: https://github.com/BoringExperiments/WolframAlpha_ai_dart

environment:
sdk: ^3.3.0

platforms:
android:
ios:
linux:
macos:
web:
windows:

dependencies:
dio: ^5.4.1

dev_dependencies:
lints: ^3.0.0
test: ^1.24.0
28 changes: 28 additions & 0 deletions test/api_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:wolframalpha_ai_dart/wolframalpha.dart';
import 'package:test/test.dart';

void main() {
group('API', () {
test('Simple', () async {
final value = await simple(
appId: 'DEMO',
input: 'What is the price of gold?',
);
expect(value.toString().isNotEmpty, true);
});
test('Short Answers', () async {
final value = await shortAnswers(
appId: 'DEMO',
input: 'How many ounces are in a gallon?',
);
expect(value.toString().isNotEmpty, true);
});
test('Spoken Answers', () async {
final value = await spokenAnswers(
appId: 'DEMO',
input: 'Convert 42 mi to km',
);
expect(value.toString().isNotEmpty, true);
});
});
}

0 comments on commit 6da8d84

Please sign in to comment.