Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rdhillbb/util
Anthropic/Openai Utilities
https://github.com/rdhillbb/util
anthropic openai promp query queryrewrite rewrite
Last synced: 8 days ago
JSON representation
Anthropic/Openai Utilities
- Host: GitHub
- URL: https://github.com/rdhillbb/util
- Owner: rdhillbb
- Created: 2024-12-22T13:13:59.000Z (16 days ago)
- Default Branch: main
- Last Pushed: 2024-12-22T13:26:17.000Z (16 days ago)
- Last Synced: 2024-12-22T14:28:29.327Z (15 days ago)
- Topics: anthropic, openai, promp, query, queryrewrite, rewrite
- Language: Go
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Query Rewrite System
This system provides functionality to enhance and rewrite search queries using the Anthropic Claude API. It generates multiple variations of an input query while maintaining the original intent and adding contextual depth.
## Overview
The Query Rewrite System consists of two main components:
- A message template file (`messagefile.xml.txt`) containing the prompt structure
- A Go implementation (`util.go`) that handles the query rewriting logic## Message File Structure
The system uses an XML-based message file that contains templates for different operations. The primary template used is the `query_rewrite` template under `utilmessages`. This template provides instructions to Claude for generating query variations.
### Template Variables
The query rewrite template accepts two parameters:
- `%d`: Number of query rewrites to generate
- `%s`: Original query to be enhanced## Environment Variables
The following environment variables must be configured:
| Variable | Description | Default |
|----------|-------------|---------|
| `ANTHROPIC_API_KEY` | API key for Anthropic Claude service | Required |
| `REWRITENUM` | Number of query rewrites to generate | 3 |## Core Functions
### `ReWriteQR(query string) ([]string, error)`
Main function that processes a query and returns an array of rewritten queries.### `buildPrompt(query string) string`
Constructs the prompt using the message template and query parameters.### `makeAnthropicRequest(prompt, apiKey string) (*http.Response, error)`
Handles the API communication with Anthropic's Claude service.### `processResponse(resp *http.Response) ([]string, error)`
Processes the API response and extracts the rewritten queries.## Query Rewrite Guidelines
The system generates variations that include:
- Broader context queries
- More specific/detailed queries
- Alternative phrasings
- Related subtopics
- Different perspectivesEach rewritten query:
- Maintains the original intent
- Uses natural language
- Avoids redundancy
- Includes relevant context
- Varies in complexity
- Must not exceed 200 characters## Example Usage
```go
func main() {
results, err := ReWriteQR("what are the health benefits of garlic")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}for i, result := range results {
fmt.Printf("%d: %s\n", i+1, result)
}
}
```## API Configuration
The system uses the Claude 3 Sonnet model (`claude-3-sonnet-20240229`) with the following settings:
- Max tokens: 1000
- API Version: 2023-06-01## Dependencies
- github.com/rdhillbb/messagefile
- github.com/joho/godotenv## Error Handling
The system includes comprehensive error handling for:
- Missing environment variables
- API communication issues
- Response parsing errors
- Invalid JSON formatting
- Empty results## Installation
1. Clone the repository
2. Create a `.env` file with required environment variables
3. Install dependencies: `go get`
4. Build: `go build`## Contributing
When contributing to this project, please ensure that:
- All new message templates are added to the XML file
- Environment variables are documented
- Error handling follows the established pattern
- Tests are included for new functionality