Combining Geo‑IP and Prompt Engineering with Higress AI Gateway: Implementation and Usage
This article explains Prompt Engineering, introduces the AI Gateway concept, and demonstrates how to integrate a Geo‑IP plugin with an AI prompt‑modifying plugin in Higress using Go and Wasm, providing configuration examples, implementation details, and sample request‑response scenarios.
Prompt Engineering (also called In‑Context Prompting) structures prompts to guide large language models (LLMs) to produce desired outputs without changing model weights, improving answer accuracy by optimizing prompts.
The AI Gateway is an AI‑native API Gateway that extends traditional API Gateway capabilities, such as token‑based rate limiting, multi‑model vendor support, and enhanced observability like A/B testing and tracing.
Higress, an open‑source AI Gateway from Alibaba Cloud, offers a plugin ecosystem (AI, traffic management, security) and supports Wasm plugins written in multiple languages, with hot‑swap updates.
Geo‑IP Plugin Configuration
Enable the Geo‑IP plugin (IPv4 only) and set:
ipProtocal: "ipv4"AI Prompt‑Modifying Plugin Configuration
Enable the plugin and inject geographic information via variables:
prepend: - content: "提问用户当前的地理位置信息是,国家:${geo-country},省份:${geo-province}, 城市:${geo-city}" role: "system" append: - content: "每次回答完问题,尝试进行反问" role: "user"Example Requests and Responses
Request 1:
curl 'http://<这里换成网关公网IP>/api/openai/v1/chat/completions' \ -H 'Accept: application/json, text/event-stream' \ -H 'Content-Type: application/json' \ --data-raw '{ "model":"qwen-long", "frequency_penalty":0, "max_tokens":800, "stream":false, "presence_penalty":0, "temperature":0.7, "top_p":0.95, "messages":[{"role":"user","content":"附近有什么旅游景点?"}] }'Response 1 (excerpt):
{..."content": "附近热门的旅游景点有故宫、天安门广场、恭王府等。您对哪个景点更感兴趣呢?"...}Request 2 (travel to Hong Kong):
curl 'http://<这里换成网关公网IP>/api/openai/v1/chat/completions' \ -H 'Accept: application/json, text/event-stream' \ -H 'Content-Type: application/json' \ --data-raw '{ "model":"qwen-long", "frequency_penalty":0, "max_tokens":800, "stream":false, "presence_penalty":0, "temperature":0.7, "top_p":0.95, "messages":[{"role":"user","content":"怎么去香港?"}] }'Response 2 (excerpt):
{..."content": "从您所在的位置(北京市)前往香港,您可以选择乘坐飞机、火车或者长途汽车等多种交通方式。..."}Request 3 (local snacks):
curl 'http://<这里换成网关公网IP>/api/openai/v1/chat/completions' \ -H 'Accept: application/json, text/event-stream' \ -H 'Content-Type: application/json' \ --data-raw '{ "model":"qwen-long", "frequency_penalty":0, "max_tokens":800, "stream":false, "presence_penalty":0, "temperature":0.7, "top_p":0.95, "messages":[{"role":"user","content":"特色小吃有哪些?"}] }'Response 3 (excerpt):
{..."content": "北京的特色小吃包括豆汁儿配焦圈、炸酱面、卤煮火烧、炒肝、艾窝窝、糖葫芦等。"...}Implementation Details
The Geo‑IP plugin extracts the client IP from remote_address or the X‑Forwarded‑For header, then looks up location data using an offline IP database (processed into a radix‑tree format to reduce memory usage).
Data is passed between plugins via setProperty and getProperty on the request metadata, as context cannot cross plugin boundaries.
The AI prompt‑modifying plugin uses the configured template to prepend geographic context as a system message and optionally append follow‑up prompts, then forwards the enriched request to the LLM.
Sample transformed request after plugin processing shows the added system message with location details followed by the original user message.
Finally, the article summarizes the benefits of combining Geo‑IP information with Prompt Engineering to achieve more personalized and accurate LLM responses, and provides links to the PRs implementing both plugins in the Higress repository.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.